1

I have made an ios app that is localized to two languages (english and danish, english being default).
I have made a Localizable.strings with two subfiles, en and da.
Everything should be made correctly, and i see the english texts load fine from the strings file. But the danish ones do not.

I have tried to check the preferred language via below code and it is danish.

[[NSLocale preferredLanguages] objectAtIndex:0];

I have tried clean + delete and rebuild with no luck.

I know that the Localizable.strings file is working since it is getting the english texts. and i know that it is seeing the danish localization via the above line of code. What am i missing?

Just to add a couple of examples:
from the EN:

"YesButton" = "Done";
"NoButton" = "Not yet!";
"NextButton" = "Next";
"BackButton" = "Back";
"EditButton" = "Edit";
"DoneButton" = "Done";  

and the DANISH:

"YesButton" = "Færdig";
"NoButton" = "Ikke endnu!";
"NextButton" = "Næste";
"BackButton" = "Tilbage";
"EditButton" = "Redigér";
"DoneButton" = "Færdig";

and the code for getting the text would be:

[yesButton setTitle:NSLocalizedString(@"YesButton", nil) forState:UIControlStateNormal];

which is returning "Done" even when preferredLang is da (danish)!

Hope somebody has an idea? :)

Thanks in advance.

EDIT:::
Something was wrong with the actual DANISH-DENMARK localization. i dont know if apple updated it or what they did but it went from being called "Danish-Denmark" to just "Danish". After making a whole new Danish localization and deleting the old one it worked! crazy stuff. keeps you scratching your head!

B-Man
  • 2,209
  • 3
  • 22
  • 35
  • Are you using the same key in both English and Danish files? – sixthcent Apr 02 '13 at 01:08
  • yeah of course... the key is the same, different text of course. – B-Man Apr 02 '13 at 01:09
  • You're sure that you're getting the English version, and not simply the default text? – Hot Licks Apr 02 '13 at 01:36
  • Yeah, since i have not added any default text anywhere. the only source of text is the Localizable.strings file. For example: self.navigationItem.title = NSLocalizedString(@"CalendarVCTitle", nil); gives "BLABLA" , and bla bla is what i am seeing. – B-Man Apr 02 '13 at 01:38
  • the storyboard is empty of texts. – B-Man Apr 02 '13 at 01:40
  • How are you switching languages? You should be using Settings > General > International > Language on the device or in the simulator. – matt Apr 02 '13 at 02:05
  • that is how i am doing it. or else the nslog would not show da (instead of en) – B-Man Apr 02 '13 at 02:06

3 Answers3

1

I had similar thing happened to me before. All I did to fix the problem was to change the string encoding from:

encoding:NSASCIIStringEncoding

to

encoding:NSUTF8StringEncoding

~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

Now assume the problem is not string encoding. You can also work around it and do something like:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

NSString yesButtonStr=@"";
NSString noButtonStr=@"";
NSString nextButtonStr=@"";

if([@"en"  caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = @"Done";
noButtonStr= @"Not yet!";
nextButtonStr = @"Next";
//...
}
else if if([@"da"  caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = @"Færdig";
noButtonStr = @"Ikke endnu!";
nextButtonStr = @"Næste";
//...
}

Then:

[yesButton setTitle:NSLocalizedString(yesButtonStr, nil) forState:UIControlStateNormal];
  • In the attributes inspector is it set to UTF16 which I understand it should be (also tried with 8 with no luck). I made the strings file directly in xcode (did not copy it from external). But thanks for the advice. – B-Man Apr 02 '13 at 01:33
  • Have you tried to print out those strings on the debug screen? –  Apr 02 '13 at 01:40
  • Just tried. the nslog of preferredLang is DANISH, and the text is coming from the english Localizable.strings. Boggles me! – B-Man Apr 02 '13 at 01:47
  • If you temporarily delete the English entry and print out only the Danish one, what do you see on the debug log? –  Apr 02 '13 at 01:59
  • Good point. Then the key comes as default. so it is not actually reading the danish Localizable.strings. is there any way to set that as default and see if it will read it then? – B-Man Apr 02 '13 at 02:04
  • Thanks for your help but i got it figured out. see answer / edit above. – B-Man Apr 02 '13 at 02:25
1

Try this,

NSString *translatedString = [self languageSelectedStringForKey:@"YesButton"]; // Give your key here

-(NSString*) languageSelectedStringForKey:(NSString*) key
{
    NSString *path;
    path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; // give your language type in pathForResource, i.e. "en" for english, "da" for Danish
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
    return str;
}

Hope this helps, Thanks. Happy coding

Dhruvik
  • 984
  • 4
  • 18
  • Hello, thanks for your answer. I figured it out, and answered it below (cannot accept self-answer yet). but thanks for your method, good diagonistic tool. There was something wrong with the actual danish localization. When i started making the localization it was called "Danish-Denmark". After an update of ios and/or xcode, it is now only "Danish". So had to make a whole new localization. Thank you apple...... NOT! Anyway, Thanks for your help :) – B-Man Apr 02 '13 at 10:55
0

There was something wrong with the actual danish localization.
When i started making the localization it was called "Danish-Denmark". After an update of ios and/or xcode, it is now only "Danish".
So had to make a whole new localization.

Thank you apple...... NOT!

B-Man
  • 2,209
  • 3
  • 22
  • 35