iPhone App Localization - English problems? - iphone

I have an app that I am translating to a bunch of different languages. The problem is that the app will have a few different values in Australia than will in New Zealand, which are both English speaking countries.
I have created an en_AU and an en_NZ language file, but they're both using the standard English file. I deleted the English language file, but it continues to happen...
Any ideas on how I can get this to work?
Thank you,
--d

iPhone localisations (or is that localizations?) do not take any notice of the Region the user sets (ie, UK, Aus, NZ). There is only one "English" language translation available by default. However, you can hack around with things to force it to use a different translation setting - I've just done this for choosing between "English" (US) and "en_GB" (British english).
In your main.m file, alter it so it looks something like below (put in your own tests for NZ or AU)
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Set up the locale jiggery pokery
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *locale = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
if ([language isEqualToString:#"en"] && [locale isEqualToString:#"GB"]) {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en_GB", #"en", nil] forKey:#"AppleLanguages"];
}
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
This pops the users language (eg "en") into the language NSString, and the users locale (eg, NZ, GB, AU) into the locale NSString. If they (in my case) match en and GB, then I set the users default language preference settings to be "en_GB", then "en".
Then, in your application delegates application:didFinishLaunchingWithOptions method you want to remove that NSUserDefaults setting you just set with the code
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
It's safe to remove at this point because all the bundle initialisation has been completed. Your app should now be using a Localization.strings file within the en_GB.lproj directory.
It's a bit of a horrible, hacky solution, but it works for me.

I've come up with what I think is a slightly improved version of rickerbh's accepted answer. The first thing to realize is that user defaults are organized into domains, and the #"AppleLanguages" key comes not from the app's domain, but from some domain higher up the hierarchy of domains. This means it is completely safe to remove it from user defaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"AppleLanguages"];
After calling this code, you'll notice that calling [defaults objectForKey:#"AppleLanguages"] still returns a value. So, rather than deleting #"AppleLanguages" at some point later, which could be problematic depending upon the complexity of your app, you want to do the opposite: delete #"AppleLanguages" immediately. Essentially, this resets it back to its default value and captures any changes to it the system has made, if, for instance, the user has changed her preferred language.
Here's what I do:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"AppleLanguages"];
NSMutableArray *appleLanguages = [[defaults objectForKey:#"AppleLanguages"] mutableCopy];
NSString *region = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
NSArray *languages = [appleLanguages filteredArrayUsingPredicateFormat:#"not (self contains '-')"];
for (NSString *language in languages) {
NSString *languageAndRegion = [NSString stringWithFormat:#"%#-%#", language, region];
[appleLanguages removeObject:languageAndRegion];
}
for (NSString *language in languages) {
NSString *languageAndRegion = [NSString stringWithFormat:#"%#-%#", language, region];
NSInteger index = [appleLanguages indexOfObject:language];
[appleLanguages insertObject:languageAndRegion atIndex:index];
}
[defaults setObject:appleLanguages forKey:#"AppleLanguages"];
(Note that filteredArrayUsingPredicateFormat: is an extension method I wrote. It's not rocket science to figure out what it does or how it works.)
This creates localizations for every language in the list combined with the user's region. E.g., if the original list was es en en-GB and the user's region is AU, we'll get es-AU es en-AU en en-GB. Note that es-AU doesn't exist, but it makes no difference. Since the app finds no associated localizations or resources, it just ignores it.

Apple documents this missing iOS feature here.
Important: In iOS, the bundle
interfaces do not take dialect or
script information into account when
looking for localized resources; only
the language designator code is
considered. Therefore if your project
includes language-specific project
directories with both a language and
region designator, those directories
are ignored. The bundle interfaces in
Mac OS X do support region designators
in language-specific project
directories.

I had the same problems with German and think I found the "right" solution. The mistake was, that originally my base language was only "German (de)", when adding "German/Austria (de_AT)" localizations, the files were ignored. When changing the base language to "German/Germany (de_DE)" the austrian translations were not ignored.

Worth noting here that XCode is very misleading - you can go to Project (not Target), Info, Localizations, press the + button that appears under the list of languages, then scroll down to the bottom of the list of languages that appear in the popup until you get to "Other" (with right arrow next to it), this will open a nice big list which includes regional variants for languages. However, these regional variants do not work on the iPhone - you don't get anything (as documented by Apple and referenced in another answer that appears here). Evidently one of the code-based solutions listed above is necessary.

Related

Save (Private) Application Settings on iOS?

I'm aware of NSUserDefaults for saving/restoring user preferences. What is the equivalent class for an application? For example, the application may have a "last run" field; or it may have a field for a unique identification of the device for use at the application level.
My intention is to keep the application's settings (not user's settings) out of the Settings Application, and not backup those settings in iTunes, Time Machine, {whatever}.
I'm getting a lot of noise for Java and C#, but not much for iOS/iPhone/iPad.
NSUserDefaults can be used for what you're asking.
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"shownPrompt"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"shownPrompt"];
// Show your prompt or whatever
}
That's a working code snippet. If the key is false, it sets it to true and shows the prompt. The next time this code runs, the key will already by true, so the prompt won't be shown.
NSUserDefaults is specific to the current app on the current device, and is similar to an NSMutableDictionary in that it's a key-value system, with the difference that instead of instantiating your own, there's a universal shared instance for your whole app, that doesn't get erased when the app exits.
NSUserDefaults is perfect for saving things like whether something has been shown, the date of last run, etc. Read the docs here: https://developer.apple.com/documentation/foundation/userdefaults
Don't be put off by the 'user preferences' part. You can use it to save anything you want (as long as it is or can be converted to an NSObject which implements <NSCoding>, which basically means NSString, NSDictionary, NSArray, NSNumber, UITextField, int, float, bool, etc.).
Just to clarify, stuff you put in NSUserDefaults will not, under any circumstances, automagically turn up in the Settings app. It will be kept completely private and hidden. For something to appear in Settings, you need to add a Settings bundle to your app, and manually add keys to it for each and every value that you want to be visible in the Settings app.
if you can store value by NSUserDefaults, then it is good to store application preferences too.
or add settings.plist on your project and read that (what you are not changing later)
and you can use like.,
+ (NSDictionary*)getBundlePlist:(NSString *)plistName
{
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
return temp;
}
+ (id) getPropValue:(NSString *)PropertyName
{ // I am supposing you had add your app preferences on settings.plist.
return [[Property getBundlePlist:#"settings"] objectForKey:PropertyName];
//here Property is my class name, then you can use value by
//NSString *value = [Property getPropValue:#"setting1"];
}
It's hard to tell what you're asking. It sounds like NSUserDefaults is what your looking for, but you claim that you're already aware of it. So what's your problem?

Store localised string data on iPhone

Let's suppose that I want to store (and later update via internet) a simple database of words with their definition :
- label
- definition
According to you, what would be the best way to store them in different languages ?
there's a label and a definition in both French and English, and later we could add other languages.
EDIT: for the moment I can only think about a 3rd property : language
label
definition
language
I would define the assessor to return the language according to the current settings, or english by default.
Coredata doesn't support the localization of data? it supports localization of property names
assuming you have a large set of words, you can save them as an array (s)
as described here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/uid/20000172-CIAEAHFJ
example:
NSString *errString;
NSData *serialized =[NSPropertyListSerialization dataFromPropertyList:imgsData
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&errString];
[serialized writeToFile:dataFilePath atomically:YES];
if (errString)
{
NSLog(#"%#" errString);
[errString release]; // exception to the rules
}
To read it back in, use
NSString *errString;
NSData *serialized = [NSData dataWithContentsOfFile:data
FilePath];
imgsData =
[NSPropertyListSerialization propertyListFromData:serialized
mutabilityOption:NSPropertyListMutableContainers
format:NULL
errorDescription:&errString];
if (errString)
{
NSLog(#"%#" errString);
[errString release]; // exception to the rules
}
You have to localise your text strings and support translations for the language you want to use. There are many tutorials on how this can be achieved. Its more like mapping enums to text. And text will be returned for the language you ask for. Here is a good start point for your requirement.
Follow the steps there and you can modify them as per your needs once you get hold of localisation of strings.

How to get the language used by an iPhone app

How can I get which language an app is running on? And I don't mean the preference set into settings!
Let me explain it further...
I localised my app with three languages: English, Italian and Spanish. If the iPhone is set on Italian or Spanish, the app will use those two... it falls back to English otherwise.
For example: a French user gets the English version... so even if French is the language set, my app automatically use English. Now... how can I return this "value" in my code?
Thanks!
You can get the user's preferred language using:
NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
The [NSLocale preferredLanguages] array actually contains all the user's preferred languages in descending order, so you can just loop through it until you find one that your app supports.
Dont really undertstand what this And I don't mean the preference set into settings! means?
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
You just need to get the current language of the device (as explained above by other users) and compare it with the languages supported by your app, something like:
currentLangue = //get current language with code in other answers
languageToUse = ""
for each language in languagesSupportedbyMyApp
if currentLanguage == language
languagetoUse = language
//if no language is found then set it to "en"
if languageToUse == ""
languageToUse = "en"
regards,
Fernando

Is it possible to read the structure of plist file?

I know that NSUserDefaults can read the key and value from plist file.
Is it possible to read the structure of a key?
For example:
Key 'Count' is an integer and has option
1,2,3,4,5
the codes below can get the value 'Count'
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"Count"]!=nil)
{
NSString *s=[[NSString alloc] initWithString: [defaults objectForKey:#"Count"]];
NSInteger v=[s intValue];
[s release];
}
But I prefer to get all options and store to a NSArray or a better storage structure.
Is it Possible?
I read bundle settings the following way:
// Get path to Root.plist file in settings bundle and retrieving its contents
NSString* tPath = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"bundle"];
settingsBundle = [[NSBundle bundleWithPath:tPath] retain];
NSDictionary* tSetDict = [NSDictionary dictionaryWithContentsOfFile:[tPath stringByAppendingPathComponent:#"Root.plist"]];
// Get array of preference dictionaries
NSArray* prefs = [tSetDict objectForKey:#"PreferenceSpecifiers"];
// Iterate through dictionaries to find required value
for (NSDictionary* setDict in prefs){
NSString* type = [setDict objectForKey:#"Type"];
if (![type isEqualToString:#"PSMultiValueSpecifier"]){
// Get possible preference values for PSMultiValueSpecifier case
// You may need to know value type in advance - not sure about that
NSArray* values = [setDict objectForKey:#"Values"];
}
}
I'm not exactly sure I understand your question but you can't directly access the user defaults system save through its defined methods. You can't read them out in one big chunk.
The defaults system isn't actually a means of reading plist files, its an more of an API for accessing a database maintained by the OS itself. Although, you don't see it much on the iPhone, its actually a very large and complex system for managing preferences not only for individual apps but also for users, groups of users, computers and networks. It seems trivial on iOS because you don't have the flexibility of configuration and use that you have on MacOS proper.
It would be impossible to read out the entire defaults because they are huge and much larger than you would expect even on iOS.
Instead of jumping through the hoops in the code you showed in the answer, you should access the data in the key using one of the dedicated methods for the type of data stored. In this case:
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSInteger v=[defaults integerForKey:#"Count"];
Even if you did read the defaults out in one chunk, you would just find yourself using the same type of calls and code to access the data in the alternate data structure as you would in using the defaults in the first place. You might as well use the defaults system.

Changing the language for NSLocalizedString() in run time

I have an iPhone application which has a button to change display language in run time. I have looked at NSLocalizedString() which will return the appropriate strings according to system preference. What are my options rather than hard coding all the display strings and return according to user language selection in run time? Any pointers will be much appreciated.
Based on the post by the user "object2.0", I've put together some sample code you can use in your application to change the UI language on the fly.
The main localization class that does the hard work:
-(NSString *) localized:(NSString *) key
{
GameInfo *gameInfo = [GameInfo sharedInstance];
// langCode should be set as a global variable somewhere
NSString *path = [[NSBundle mainBundle] pathForResource:langCode ofType:#"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
return [languageBundle localizedStringForKey:key value:#"" table:nil];
}
Assuming you have this function in a global class called utils, call this function with the following code (for example to output the word "Settings".
NSLog( [utils localized:#"Settings"] );
To change the language:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:langCode, nil] forKey:#"AppleLanguages"];
Use to set language order by force
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en",#"de",..., nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
then use
NSLocalizedString();
to show localized string...
The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language ,
here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html
and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps
The correct "User experience" is for the user to select their language via the system preference panel; not your app (or your app's settings panel, etc.). There is no way to override this per-app and you wouldn't want any app changing the system wide setting.