Issue with localization in iOS - ios

I have been stuck in a mysterious problem related to localization in the last two days.
I am trying to add languages to my app, but it seems to me that something is going wrong.
To make things simple, I added the following lines to my current app, to see if the word test will translate or not :
- (void)viewDidLoad
{ [super viewDidLoad];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,100,20)];
lbl1.textColor=[UIColor blackColor];
[self.view addSubview:lbl1];
lbl1.text= NSLocalizedString(#"test", nil);
}
After adding the language in the section of localization in "Project info" section, and checking "Use based internationalization". In addition to creating "Localizable.strings" file and clicking on Localize to choose the language, the word test does not translate.
BTW, here is Localizable.strings file :
"test" = "test translated";
I've done clean, and deleted the app form my phone and build it again, but nothing changed !
I took the same code, and done the same steps by creating a new project on xCode, surprisingly it translates !
Anyone knows why the translation does NOT happen in my original app ?

Firstly, Base Internationalization is for storyboards, it is not for NSLocalizedString.
Secondly, the file should be Localizable.strings, not Localization.strings.

It happpened to me recently. I did those things:
1) Deleted app from simulator/iPhone
2) Cleaned app (Product -> Clean)
3) Deleted derived data (Window -> Organizer -> Projects (tab) -> (Select own project from left) -> Click "Delete..." on right of Derived data)
It helped me

Related

How to get App name in UITesting swift 4 iOS

I tried to get my app name in UITesting. I tried the code below:
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
But it gives me the nil value result.
When I was trying to find out if the 'debugDescription' provides any information about the actual app (and not the test app) I stumbled across a label that looked exactly like the app's display name.
I don't know if it helps you, but the following simple line of code helped me:
XCUIApplication().label
Are you sure, that you've set Display name in your Info.plist file?
Just set this name in Project settings or in Info.plist:
Try to go to File > Workspace Settings if you are in a workspace environment or File > Project Settings for a regular project environment.
Then click over the little grey arrow under Derived data section and select your project folder to delete it.
And Product > Clean.
It helps in many situations.
I am getting app name in UI test like this:
let app = XCUIApplication()
// ...
let appName = app.label

Xcode UI Testing - can't find element that was just recorded

I'm trying out Xcode UI testing. I've just recorded a simple test and replayed it and it's failing on the first step. The code is:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElementQuery *scrollViewsQuery = app.scrollViews;
[[scrollViewsQuery.otherElements containingType:XCUIElementTypeStaticText identifier:#"First Page"].element tap];
The line that's failing is the last one, and the error message is 'UI Testing Failure - No matches found for ScrollView'
Why is this failing? How can I interact with this element in this view?
Try referencing the static text directly, instead of through the otherElements accessor.
[scrollViewsQuery.staticTexts[#"First Page"] tap];
Is your application a menu bar app by chance? I've found that XCUITest has bugs when dealing with finding elements belonging to menu bar apps specifically.
Try making sure that in your "MYAPP-Info.plist" file, the entry "Application is agent (UIElement)" is set to "NO".

localize UIAlertView & offline notifications in project with no storyboard

I'm working on a project that doesn't have any storyboards. So I can't use "base internationalization"
I've created localizable.string then turned on localization for each required languages.
Then tried doing
- UIAlertView * messageBox = [[UIAlertView alloc] initWithTitle:
NSLocalizedString(#"title", nil)
message: NSLocalizedString(#"msg", nil)
delegate: self
cancelButtonTitle: NSLocalizedString(#"ok", nil)
otherButtonTitles: nil];
And for notification i'm using nsdictionary and store notifications as:
- [alertMessages setObject:NSLocalizedString(#"test_notification", nil)
forKey:#"6"];
but it doesn't do any changes when changing language of the device. Is there any step i'm missing or if any better method is there? any working references would be greatly appreciated!
SOLUTION 1:
Do the following steps.
Clean Project
Delete App From Device/Simulator
If Simulator - Reset it.
Delete Derived Files (XCode > Window > Projects > Derived Data Delete)
Install App, and it will work.
I think its simply Cache Issue.
SOLUTION 2:
NSLocalizedString uses Localizable.strings file by default. Change your File.strings name and try again.
NSLocalizedStringFromTable(#"Message", #"File");

IOS 8.1 localization not working,only show key not value

First of all,I did this follow this site http://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014
I create a new Localizable.strings file,and localize it ,then add English and Japanese support.
I edit my Localizable.strings(en) file as follow:
"APPTITLE" = "Calibrate";
I edit my Localizable.strings(ja) file as follow:
"APPTITLE" = "こんにちわ";
At the source code,the early code :
self.title = #"PianoDisc Calibrate";
instead of :
self.title = NSLocalizedString(#"APPTITLE", nil);
Then I run it,But I get "APPTITLE" not "こんにちわ";
My environment is iPad mini,ios 8.1 system,Xcode 7 or 6.4. I'm also searched long time on the StackOverflow,like as NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS) ,I have tried the all answer,But it still not work.
There are some info,I use follow code,
NSString *path = [[NSBundle mainBundle] pathForResource:#"ja" ofType:#"lproj"];
NSLog(#"!!!!!!!!!!!!!!!!%#",path);
sometimes get null,sometimes get the true path.
What I'm missing? Thanks anyway!
I resolved it. Some guy remind that "check you haven't somehow messed up with the Build Rules (Xcode 4 -> select your target -> tab "Build Rules") There, check if you don't have too many "CopyStringsFile" rules that aren't using "CopyStringsFile". If so you can remove them all but one."
This step just resolve my problem,thanks god.

How to use different fonts for different languages in an iOS application?

I am in a scenario where I want to use different fonts for the different languages in my iOS application.
At first, I thought of using localized versions of .ttf files. I tried adding the localized .ttf files in the localized project folders for different languages with the same filename and font-name installed in them. It didn't work.
I then looked into the InfoPlist.strings files for different languages. It appeared to me as the key value combination for the 'key' strings found in the main Info.plist. I found the font entry against the key 'UIAppFonts' which was an array. I am unable to figure out how to put the localized font file names in the InfoPlist.strings as an array.
As a last resort, I am thinking of adding localized font names in the Localizable.stings files to pick the font name according to the current locale and replace all the occurrences in my project wherever I have used fonts. (Obviously it is a lot cumbersome). Like:
UIFont *font = [UIFont fontWithName:NSLocalizedString(#"font-name", nil) size:16];
Please assist if anybody has done this before. It would be great if somehow one of the first two ideas can be implemented.
I actually have done this by adding different font files for different languages in the project and in the plist against UIAppFonts. Then I introduced:
"fontName" = "English Font";
"fontName" = "Russian Font";
in Localizable.strings for each supported language
I then accessed it using:
UIFont *font = [UIFont fontWithName:NSLocalizedString(#"fontName", nil) size:16];
As I mentioned in my question that the other two approaches seem to be more elegant. Any solution regarding those will be highly helpful.
To use specific UIFonts inside an app I created an extension of UIButton and UILabel. Because I had the same problem, choosing different UIFonts for different languages, I also inserted a check into my extension. What looked like:
#implementation MyButton{
NSString *_language;
}
- (void)awakeFromNib{
_language = [[NSLocale preferredLanguages] objectAtIndex:0];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.titleLabel setFont:[self whichFontShouldBeUsed]];
}
- (UIFont *)whichFontShouldBeUsed{
UIFont *fontToUse = [UIFont fontWithName:#"Basic Font"
size:self.titleLabel.font.pointSize];
if( [_language isEqualToString:#"tr"] &&
[_language isEqualToString:#"pl"] ){
fontToUse = [UIFont fontWithName:#"Another Font"
size:self.titleLabel.font.pointSize];
} else if( [_language isEqualToString:#"ja"] &&
[_language isEqualToString:#"tr"] ){
fontToUse = [UIFont boldSystemFontOfSize:self.titleLabel.font.pointSize];
} else if( [_language isEqualToString:#"ru"] ){
fontToUse = [UIFont fontWithName:#"Another Font"
size:self.titleLabel.font.pointSize + 2.0f];
}
return fontToUse;
}
This way you can always switch UIFonts automatically. Next, you just have to add this class inside the Interface Builder. Select the UIButton you want and add your custom UIButton class:
For UILabel you would use setText instead of setTitleLabel" and setTextColorinstead ofsetTitleColor`.
For further information to preferredLanguages look at this website and of course don't forget to add your fonts to the projects Target and also to the plist.
You are on the right track with UIAppFonts. First, add the .ttf files to your project under the Resources folder. Then, you need to add a new line to the Info.plist (not InfoPlist.strings?). You do this by clicking on the Plus sign (+) at the far right of the last line in the plist, or I think You can Control-click any other property and select New. Select UIAppFonts from the drop-down in the first column, and that should put an arrow (>) to the left of it. Click on this arrow to expand the property and you should see item0, type in the name of your .ttf file there. Control-click to add more items as necessary.
This will add your custom fonts to the application. It is important to note that when you load them using fontWithName, the name you supply in #"font-name" may not (and usually IS NOT) the exact same name as the .ttf file. Rather, it is the whole name as it appears in the Font Book under Preview --> Show Font Info.
All this is fine, but it doesn't localize anything. You could use something like the line below to query the language code and then decide which font to load based on that:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

Resources