Localization

I want to localize a couple of lines in my iPhone application, and it still works for the 5 languages ​​that I have selected. However, I have one problem, if I have not defined a key for a particular language, I want it to select English as a reserve, because I know that I have precisely defined each line in English. The fact is that it is not very important that all keys are translated, why it is good to show English, even if the iPhone is installed in Spanish, for example.

Example:

en:

"HELLO" = "hello" ; "PERSON" = "my friend" ; 

Question:

 "HELLO" = "hola" ; 

Expected Result:

If the iPhone is installed in English, I want it to say: hello, my friend , and if the language value is set to Spanish, I want to say hola, my friend . Currently, the application will display hola, PERSON . (I use the NSLocalizedString method).

This is probably a really simple solution, but I have not found a good one. Thanks for the help,

Matthias

+4
source share
3 answers

The other answers were definitely correct, but that was not quite what I wanted. Instead, I found another way to do this and wrote a static method that works fine for me in my environment, and I wanted to add it here if someone else wants to achieve the same:

 + (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; } 
+7
source

The NSLocalizedString function NSLocalizedString says:

Return value

The result of calling localizedStringForKey: value: table: in the main package and table is nil.

and for localizedStringForKey:value:table: says :

Return value

The localized version of the row indicated by the key in the tableName table. If the value is nil or an empty row, and the localized row is not found in the table, returns the key . If the key and value are nil , returns an empty string.

to localize a string for all your languages ​​or change the key value that you use

+4
source

A simple solution is to use English text as a key. For instance:

 [NSString stringWithFormat:NSLocalizedString(@"%@, %@", "format for greeting"), NSLocalizedString(@"Hello", "salutation"), NSLocalizedString(@"my friend", "person")] 

If there is no localized version of the string, the English key is used.

+1
source

All Articles