iphone multilanguage support - ios

I'm developing an Iphone app that has to support different languages.
I saw that the language has to be set within my app and not within iphone settings. So, do I have to force the language instead to take the current one? I didn't find examples over the internet. All examples need the current language of the application. I would like that the user choose his language when the application starts, then I will set a cookie and (in a way that iI don't know) the app refers automatically to my .lproj folders with different languages.

This is not possible using the default localization mechanisms in iOS. By default, the system chooses the .lproj folder according to the user's system language and does the localization automatically if you have localized NIBs and use NSLocalizedString() etc..
If you really want to change that behavior and "override" the system language, you have to implement your own version of NSLocalizedString that manually accesses the strings file in the .lproj folder you want. Be aware though that NIBs don't use your custom NSLocalizedString function. So either don't use NIBs at all or do the localizing of the NIBs in code instead of using different NIBs.

Your question isn't really very clear. There is nothing saying that "the language has to be set within my app and not within iphone settings" — it is in fact quite the opposite!
Cocoa has a pretty neat localization system that is quite easy to use (grumbles something about rotten localization workflows). Here's the full skinny on it — basically, have files in lproj folders, then use the NSBundle resource APIs to locate them (NIB loading and other subsystems use it automatically, so you don't even have to do work there!).

IIRC, you want NSLocalizedStringFromTable.
You create a .strings file for each language: eg "EN.strings", "JP.strings", etc...
These files will be loadable from the default bundle with the table parameter to NSLocalizedStringFromTable.
When the user picks a language, you switch which table (.strings file) to load the strings from.
One problem tho is that iOS strings will still be localized to the user's settings, or to whatever you have your app localized to in the Info.plist. So you might end up with a mix of languages.

[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"it",#"en", nil] forKey:#"AppleLanguages"];
then if the iPhone language is set to english, my iPhone app will works always with "it". the first of the array.

Related

Cannot automate localization in my iOS generated App

I am generating an iOS app via some tools. No need to concern about them, I have to do things that way.
Localization
The app is generated just fine, however I need to have the app be available for a certain set of languages. My app is actually a cross-platform app, and i am handling translation at JavaScript level. So my app actually picks the proper strings.
I just need to have my app be recognized in the Store as supporting those languages
Manual approach
I used to handle this by automatically generating language folders in Resources. So my automation scripts, would basically take the list of supported languages (say it, en and fr) and generate the following folders and files in my project:
MyApp
|
+-MyApp-Info.plist
+-Resources
|
+-en_US
|
+-Localizable.strings
+-it_IT
|
+-Localizable.strings
+-fr_FR
|
+-Localizable.strings
And Localizable.strings is just a placeholder file with a C-style comment in it.
Not working anymore
This did the trick so far, but now it is not really working anymore. The store recognizes my app to be only an English app, it does not list French and Italian anymore.
I am targeting iOS 8.0+ and developing on latest version of XCode.
How can I fix this?
Try renaming the localization folders so they have the ".lproj" suffix. Use the naming scheme as defined in the Apple Internationalization and Localization Guide under Managing Strings Files Yourself.
I would also add at least one string resource in the Localizable.strings file for at least one of the languages. At least one localized string resource may be needed to flip the bits and mark the app as supporting the additional languages.

How can i set English as default language for my app? [duplicate]

I am developing an app for a client in Europe. I am an English-speaker in the US. Our app is going to support a number of languages, but not English. I have all the strings in our app in translated Localized.strings files, set up correctly for the different languages, and they all work fine when the device is set to the correct language (device's language is German = app is correctly localized for German).
There is a problem when the device is not set to one of the languages we support, for example, on my phone which is set to English. We want the phone to fall back to German in cases like this, but that is not happening. What we are seeing is that the phone is using the language that appears highest on the Language list in the International section of Settings.app. For my phone, the highest non-English language on the list is French, so when I run the app, it is localized for French. If I change my phone to German and then back to English (which changes the order in the Languages list), the app then localizes to German.
How can I ensure the app defaults to German for non-supported languages? I used this tutorial to set up the locales for the project. This includes removing the default "English" locale that is created when you first localize a file. In the project file, I've added:
developmentRegion = de;
Also, in the Info.plist, I have
<key>CFBundleDevelopmentRegion</key>
<string>de</string>
With no success.
Any ideas would be appreciated!
Defaulting to a particular language is not what you should strive for. Suppose a japanese user living in France and fluently speaking French has his phone set to Japanese. Your App would default to German, despite it having a French translation; I guess that's not what you want and it's certainly not what the user wants.
For this reason I suggest to respect the language prioritization as set by the user.
Since you don't want to support English but have developed the App in English, the easiest thing you can do is to just get rid of en.lproj right after compiling. This will leave the app with only the languages you plan to support, and the iPhone will chose the language best suited for the user, as set in iPhone's defaults.
There is a pretty straightforward solution to deleting a specific localization:
Let Xcode build the App with all the present localizations and just before code-signing kicks in delete the en.lproj folder and all localized files for that language are gone. You can easily do this by adding a Run Script build phase to the target, containing this one line of code (it's a Bash Script):
rm -r "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.app/en.lproj"
Code Signing always kicks in after all build phases have completed, so just put that build phase at the end of your current phases.
Note that this increases the time to build because Xcode recreates all English files, so you might want to disable this step during testing.
I hope that's a viable solution for you.
Concerning "the app must fall back to German in the case that an appropriate localization doesn't exist":
Question is what is an appropriate localization? If the user's third choice is French (after 2 unsupported languages), this solution will make the app fall back to French, which is an appropriate localization. More appropriate than setting the user's fifth choice, German, by hand.
What happens when an app launches is simple: The OS descends the users language list, as set in Preferences, until it finds a matching localization, and uses this language. The reason many apps default to English and not German is simply because English appears on most user language lists above German. There is no inherent language preference to the system. If you delete the English translation, only the languages you want to support are there, and of these languages the one higher on a user's list is taken.
I don't like posting this answer since it runs against Apples Human Interface Guidelines. Localizations are something the user should set, not something management should decide, so managers requiring a certain default language don't understand nothing and should change business.
That being said, there is a way to force iOS using certain languages in a certain order. I mainly use this to debug localizations without the need to set the device language to something else, whether it is possible to use this to solve the nonexistent problem in this question I can't tell.
There is a key AppleLanguages in the user defaults that dictates the order of languages to be used, normally set to the order the user sets in iOS's preferences. If you change this order, the app will try to find localizations in that order first. What you could do is read the current language preferences and decide to default to German as follows:
NSArray *langOrder = [NSArray arrayWithObjects:#"de", nil];
[[NSUserDefaults standardUserDefaults] setObject:langOrder forKey:#"AppleLanguages"];
Note that you need to do this in main.m before UIApplicationMain, otherwise you need to restart the app for this change to take effect.
I cannot state enough that nobody should do this in a real app, unless you go all the way and implement a preference setting that allows the user to change the language manually. If you just default to a given language, the user has no option to change your choice.
Try this in your didFinishLaunchingWithOptions() function
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de_DE", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);
To make the default language as a particular one this following code works like a charm which is already described in this page. Here is the swift version: (Swift 4.2)
let arr = NSArray(objects: "ja")
UserDefaults.standard.set(arr, forKey: "AppleLanguages")

Does my Base Internationalization storyboard have to correspond to a fallback language for all unlocalized languages and strings?

I've looked through some internationalization documentation and videos on apple developer, but I never found an explicit answer to that question. In Apple's tutorials I see the Base.lproj folder alongside en.lproj and zh.lproj -- the example translation (localization) is from English to Chinese. But they tell me that there's a file en.lproj/myStoryboard.strings, and that is confusing. I can't see the point in creating an English localization for the storyboard (that is already in English).
So my questions are if the user will ever see the strings in the Base.lproj/myStoryboard.storyboard?
Do the strings in that file have to be the default strings that are shown to the user if the system cannot find the user's preferred language folder in my bundle?
Can I explicitly say "never use Base.lproj/myStoryboard.storyboard, always fall back on en.lproj/myStoryboard.strings"?
In other words:
Let's say I want my app to display in English whenever the user's language isn't available, but that my Base.lproj/myStoryboard.storyboard is in Swedish. Do I then have to localize the Base Storyboard to sv.lproj/myStoryboard.strings and translate all strings in the Base storyboard to English to accomplish this?
In my case, XCode 5 didn't create a en.lproj/myStoryboard.strings, there's only the original storyboard in the Base.lproj folder. Maybe it's an old XCode issue?
Try to remove that file and see if your app gets along with Base.lproj for default, English strings.
Your Info.plist file should have an entry Localization native development region, which points to the lproj folder to be used in case the required string doesn't exist in the preferred language.
I'm not sure about this, but I think you have to use en and UI with a Base.lproj would automatically fallback to Base.lproj.
Does that help you in any way?
So my questions are if the user will ever see the strings in the Base.lproj/myStoryboard.storyboard?
It is the default to use those strings for the development region localization, which is typically English. If there was a matching strings file for english, it would supersede the strings in the storyboard. This is typically redundant, so it isn't normally used. If you really want all of your languages to be handled identically, you can do this.
Do the strings in that file have to be the default strings that are shown to the user if the system cannot find the user's preferred language folder in my bundle?
These are unrelated concepts. The strings embedded in the storyboard are equivalent to having ones in en.lproj. After that, language fallback works the same as without Base.lproj.
Can I explicitly say "never use Base.lproj/myStoryboard.storyboard, always fall back on en.lproj/myStoryboard.strings"?
If you have en.lproj/myStoryboard.strings, and english is the development language, they will always superseded the strings embedded in Base.lproj/myStoryboard.storyboard for English. Including when English isn't the preferred language, but ends up being chosen as the fallback language.

How to localize iOS (iPhone/iPad) app using only single storyboard?

I was wondering if somebody could explain in the details how I can localize an iOS app using only 1 storyboard. I know how to localize by creating several storyboards(one storyboard for each used language), however I'd like to find out another solution.
Thanks.
The tutorial on raywenderlich.com uses two forms of localization together. The reason is he also checks the storyboard when turning on localisation in image 3:
.
So then what?
NSLocalizedString is very useful, any text set programatically can (and should) be localised using this method. Ray's tutorial is still very useful.
However, it's not very desirable to create a clone of the storyboard(s) to do translation, it's just not very maintainable in the long run.
And what about storyboards?
The magic happens using the Base Localization feature. Base localization uses the generated objectId's to 'map' different languages to the storyboard.
Do note that this feature is only available using SDK 6.0 and above. Running the app on a lower iOS version will not cause any errors, it'll simply won't work.
Go to your project, and select Base Localization, then add any other language.
Open your storyboard file and check the new language as well. Note the icon is not a storyboard icon, but a simple text file icon. That's a good thing; it won't copy the whole storyboard this time :).
Note how there is now another file 'under' the storyboard. Xcode automatically generates a file for every language you select.
You can se Localization feature for this. You need to add the Localizable.strings file for each supported language and use it for locaization.
You need to use : NSLocalizedString() for fetcing data corresponding to each key.
Check this tutorial for details.

How to detect localization on basis of Settings.bundle selection (Story board app)

I am working on an iOS app which is having combination of story board and Localization (based on two languages, i.e. English and Slovenian).
I have created the two folder (en.lproj and sl.lproj) for both the languages. Both are having InfoPlist.string and MainStoryboard.storyboard files. I also need to implement language selection through Settings.bundle.
I have created Settings.bundle and able to select language from my app settings inside device setting option.
Now my trouble points are :
How can I get the selected value on AppliactionDidBecomeActive which is being recently set by user from setting?
After getting this value, how can I load selected language folder (say en.lproj etc)
How can I get the Slovenian language option in device language settings?
My application will work on either by selecting device language or on selecting my NSBundle setting.
Please provide me suggestion on the basis of some code part or help me on any other way.
Thank you and waiting for good response.
1.
NSString * lan = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(#"language :%#",lan);
Not sure I understand your question (should be automatic).
If Slovenian is not yet supported by iOs, there is no way you'll see in the language settings.
In short you'll probably need to add a language selection option in your app. and change the language manually (including for the storyboard) using the NSLocalizedStringFromTable function.
Hope this help

Resources