List application available language - ios

I'm trying to print the list of languages supported by my app.
Let me explain a bit more : I have an app in two language (let's say english and french), and I'm looking for a way to print "en, fr" programmatically, so that if I added a third language, such as dutch, my function would then print "en, fr, nl"

You have to use NSBundle's localizations method
The code should be as follows:
NSArray *array = [[NSBundle mainBundle] localizations];
NSLog(#"Supported localizations : %#", array);

Related

Why [NSBundle localizedStringForKey] does not return the iPhone language localised string?

Why localizedStringForKey returns the localization of the development language first instead of the language of the iPhone? The "Bundle Search Pattern" in the documentation says:
If a .lproj folder exists for the preferred language, that localization is used. Otherwise, the bundle searches for a .lproj folder matching the next preferred language, and so on, until one is found. If there is no localization for a preferred language, the bundle chooses the development language localization.
Then the bundle searches for the resource in the following order [...]
But it does not make sense to me.
The test iPhone is in Chinese, french keyboard, the bundle contains 32 languages and the development language localization in English. I don't understand why it makes sense to return the English localization first. Why not is the Chinese one as this the iPhone language?
NSArray* arr = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
// ( zh-Hant-FR, en-FR, fr-FR )
NSArray* localizations = [bundle localizations];
// ar,bg,ca,cs,da,de,el,en,es,fr,he,hu,id,it,ja,ko,ms,nb,nl,nn,pl,pt,pt_BR,ro,ru,sv,th,tr,uk,vi,zh-Hans,zh-Hant
NSArray* preferredLocalizations = [bundle localizations];
// same output
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
NSString* str = [bundle localizedStringForKey:#"hello" value:#"" table:#"mytable"];
// hello
How should I fix this? I would like localizedStringForKeyto always return the iPhone language localization whatever the development language.
Use this
NSLocalizedStringWithDefaultValue("requiredTranslationKey", #"StringFileName", [NSBundle mainBundle], #"", #"");

How to translate app in to another language in iOS?

I am developing an ecommerce app. Initially the app is in the English language and now I want to convert it into the Chinese and French language. I referred to this link
So what I have understood is that every text we need to convert into French and Chinese, in the respective string files for the static data, but I am getting data from the backend. So then how do I convert that text dynamically?
You can use this code. And in Project you can add localisations in info.plist.Hope it will help you.Thank You
NSLocale* curentLocale = [NSLocale currentLocale];
namearray=[NSMutableArray arrayWithObjects:NSLocalizedString(#"Hellokey1",#""),NSLocalizedString(#"Hellokey2",#""),NSLocalizedString(#"Hellokey3",#""),NSLocalizedString(#"Hellokey4",#""),NSLocalizedString (#"Hellokey5",#""),NSLocalizedString(#"Hellokey6",#""),NSLocalizedString(#"Hellokey7",#""),NSLocalizedString(#"Hellokey8",#""),NSLocalizedString(#"Hellokey9",#""),NSLocalizedString(#"Hellokey10",#""),NSLocalizedString(#"Hellokey11",#""),NSLocalizedString(#"Hellokey12",#""),NSLocalizedString(#"Hellokey13",#""),NSLocalizedString(#"Hellokey14",#""),NSLocalizedString(#"Hellokey15",#""),NSLocalizedString(#"Hellokey16",#""),NSLocalizedString(#"Hellokey17",#""),NSLocalizedString(#"Hellokey18",#""),NSLocalizedString(#"Hellokey19",#""),NSLocalizedString(#"Hellokey20",#""),NSLocalizedString(#"Hellokey21",#""),NSLocalizedString(#"Hellokey22",#""),NSLocalizedString(#"Hellokey23",#""),NSLocalizedString(#"Hellokey24",#""),NSLocalizedString(#"Hellokey25",#""),NSLocalizedString(#"Hellokey26",#""),NSLocalizedString(#"Hellokey27",#""),nil];
[curentLocale displayNameForKey:NSLocaleIdentifier
value:[curentLocale localeIdentifier]];
// NSString *path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
// NSLog(#"path:%#",path);
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
If the text received is truly dynamic (i.e. there's no way to know ahead of time what the possible variations are), there's no way to do it. If you know what the possibilities are, you embed the translations just as you do for static text, and you have the server send the localization key. This is the scheme used by iOS for translating notification alerts.
(You could theoretically use a translation API, such as Google Translate, but that has many downsides, and few upsides.)

localizedStringForKey:value:table ignores Base localization

My application need to be localised in 2 languages: German and English. German should be Base language. It means that app should always localize to German except the language on device is english (in this case it should be on english)
I have custom *.string files for localisation and use localizedStringForKey:value:table to localise strings.
When I have only base localisation everything works fine. But in case if I add english localisation, in some reason localizedStringForKey:value:table just ignore Base localisation and always use English (for all languages)
Here how it looks like after I've added english:
and here is how I localise strings:
[[NSBundle mainBundle] localizedStringForKey:#"key" value:#"" table:#"Shared"]
I'm testing on simulator and here is my language screen:
Does anybody know what could be a problem? Thanks in advance!
As I mentioned in the comments, you need to set the Localization native development region (CFBundleDevelopmentRegion) in the Info.plist to your language code. Xcode seems to set it to a en_GB or de_DE style region code, setting it to de (no region) will fix it. Note that by default it is en, but selecting United Kingdom or Germany will change it to use the longer codes.
I have another solution, I think it works for you:
NSString* NSCustomLocalizedString( NSString *key , NSString *comment)
{
NSString *rs = nil;
if( [[NSUserDefaults standardUserDefaults] integerForKey:KEY_LANGUAGE ] == e_language_japanese)
{
rs = NSLocalizedStringFromTable(key,#"Localizable.strings-ja",nil);
}
else
{
rs = NSLocalizedStringFromTable(key,#"Localizable.strings-en",nil);
}
return rs;
}

Localization fallback language

I want to localize a couple of strings in my iPhone app and it works so far for the 5 languages I've selected. However, I have one issue, if I haven't defined a key for a specific language I want it to choose english as a fallback because I know that I have defined every string for sure in english. The thing is that it's not very important that all keys are translated why it's okay to show english even if the iPhone is set to spanish, for instance.
Example:
en:
"HELLO" = "hello" ;
"PERSON" = "my friend" ;
es:
"HELLO" = "hola" ;
Expected Outcome:
If the iPhone is set to english I want it to say: hello, my friend and if the languange is set to spanish I want it to say hola, my friend. Currently the app will output hola, PERSON. (I use the NSLocalizedString method).
There is probably a really easy solution but I couldn't find any good ones.
Thanks for the help,
Mattias
The other answers were definitely correct but it wasn't exactly what I wanted. Instead I found another way to do it and wrote a static method that works perfectly for me in my environment and I wanted to add it here if anybody else wants to acheive the same thing:
+ (NSString *)localizedStringForKey:(NSString *)key table:(NSString *)table
{
NSString *str = [[NSBundle mainBundle] localizedStringForKey:key value:#"NA" table:table];
if ([str isEqualToString:#"NA"])
{
NSString *enPath = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
return [[NSBundle bundleWithPath:enPath] localizedStringForKey:key value:#"NA" table:table];
}
return str;
}
the documentation of the function NSLocalizedString says:
Return Value
The result of invoking localizedStringForKey:value:table: on the main bundle and a nil table.
and for localizedStringForKey:value:table: says:
Return Value
A localized version of the string designated by key in table tableName. If value is nil or an empty string, and a localized string is not found in the table, returns key. If key and value are both nil, returns the empty string.
so you have localize the string for all your languages or change the value of the key you are using
The easy solution is to use the English text as the key. For example:
[NSString stringWithFormat:NSLocalizedString(#"%#, %#", "format for greeting"),
NSLocalizedString(#"Hello", "salutation"),
NSLocalizedString(#"my friend", "person")]
If there's no localized version of the string, the English key is used.

iOS - NSLocalizedString is only returning the key string

I'm having some trouble debugging my NSLocalizedString implementation. Should be simple, but whatever I do, the function only returns the KEY string.
I'm using XCode 4.5 and iOS6, so I:
Added a new file called File.strings.
In my project settings I added English and Spanish as language settings.
Clicked "Make Localized" in the file inspector, and made sure that both English and Spanish options were selected, and also that the Target membership to my target was selected.
Added "KEY" = "TestEnglish"; to my english File.strings
Added "KEY" = "TestSpanish"; to my spanish File.strings
Added NSLog(#"Localization: %#\n", NSLocalizedString(#"KEY", nil)); to my .m file.
When I run the app, the value "KEY" is always displayed printed in the NSLog.
To jump into this a bit more, I tried this as well:
NSString *path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSString *str = [[NSBundle bundleWithPath:path] localizedStringForKey:#"KEY" value:#"" table:nil];
NSLog(#"Localization: %#\n", str);
and still the value "KEY" is printed, yet, path is a valid path.
Does anyone have any clue how to debug this? I feel like I've read every SO question/answer out there, but none of the suggestions help.
I realize that NSLocalizedString returns the KEY string when it cannot match a key, but I don't see how I can debug why my app might not be matching the KEY.
I've also deleted/cleaned the app about a dozen times.
If you specify table:nil, then NSBundle will try to fetch the localization from the default table (the one in SOMELANG.lproj/Localizable.strings). If you have the localization elsewhere, you should explicitly specify the table using table:#"File" (or use the NSLocalizedStringFromTable() macro in a similar manner:
NSString *value = NSLocalizedStringFromTable(#"key", #"File", nil);
Rename the InfoPlist.strings file to Localizable.strings (double clic) and then you will get the correct string for that key.
In my case the issue was with the case of the string:
"bla.bla.blabla.BookSlot" whereas the Localizable.strings had it defined as "bla.bla.blabla.Bookslot"
So, double-check that the key string is in the correct case. Better yet, copy-paste.

Resources