How to localize server-side data that i get in my app - ios

I am using Italian language in my app. I have made a Localizable.string file in English and one in Italian.
It's working OK, and everything is working fine with static data, but there are mostly string responses from the server side. How can I convert them in Italian?
If I get data in different-different any string variable then how I will compare it from localizable.string(Italian).
And I also have server side data in English and Italian language. But in app it comes from server.
Thank you..

you can get your application current language like this :
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *appleLanguagesArray = [userDefaults objectForKey:#"AppleLanguages"];
// Get Current language of an application...
NSString *currentLanguage = [appleLanguagesArray objectAtIndex:0];
if(currentLanguage==#"en") {
// English language.
}
The other option is if you have two buttons for two languages, then you can check currently which language is selected.

Related

iOS: Localizable.strings doesn't work

This has been bothering for days now.
I've set up Arabic localisation in my project and I've set my device Language to Arabic.
When I run my app from Xcode with the run scheme option set to use Arabic Localization, localisation works fine.
When I run the app without Xcode, the app uses English.
I've read the answers to similar questions but none of them have worked for me thus far.
I am using NSLocalizableString(#"login",#""); to load the strings from the Localizable.strings file. This works fine as long as I set the Run scheme localization option to Arabic.
I've tried uninstalling the app, cleaning the project and then re-installing. Now the app uses the Localizabe.strings key names instead of their arabic values.
The Localizable.strings file is named correctly, and is listed under "Copy Bundle Resources".
The Localizable.strings is perfectly formatter. I've verified this using plutil.
What else could I be missing?
Example:
-(void) viewDidLoad
{
[super viewDidLoad];
// ...
[self setupLocalization];
}
-(void) setupLocalization
{
self.mailAddress.placeholder = NSLocalizedString(#"email_address", #"");
self.password.placeholder = NSLocalizedString(#"password", #"");
}
I found out what the issue is:
AppDelegate's application:didFinishLoadingWithOptions: contained this snippet of code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#[#"en"] forKey:#"AppleLanguages"];
[defaults synchronize];
which forces NSLocalizedString() to return English values.
The project was outsourced and I had no idea that the snippet was there.
Are you calling NSLocalizedString(_:,comment:) to convert your strings to localized strings?
If you are not doing that, that is the reason it is not working.

how to force an ios app to use a certain localization?

i have an app that have two localizations (hebrew and english), and i've been asked to cancel the english localization so that whatever your device's language, the app will be the same (but save the localization for future use).
the localization is via Localizable.strings and also Xib localization (allot of them).
is there a way to tell the app to always use a certain localization and ignore the device language?
thanks !
I have adapted this to Swift and use this to test localizations with ease. When placed in AppDelegate->didFinishLaunchingWithOptions the simulator needs to run twice to get the correct settings, but maybe there is a better way.
In the example Danish localization is used and more can be found here: Locale codes for iPhone lproj folders
let langCultureCode: String = "da"
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject([langCultureCode], forKey: "AppleLanguages")
defaults.synchronize()
And to remove the defaults once again:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("AppleLanguages")
defaults.synchronize()
in that case just set the defaults key AppleLanguages EARLY at startup to override IOS settings
(EARLY = before a xib/storyboard or NSLocalizedString is used)
NSString *langCultureCode = #"he";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#[langCultureCode] forKey:#"AppleLanguages"];
[defaults synchronize];

iOS: changing system language doesn't affect the app

I've got a real pain with my current project which has few Localizable.strings files and the problem is that the language doesn't change when user change it in system settings - NSLocalizedString macros continues to return previous language strings. I can't really undestand what wrong, I've been doing everyhing 'like in manual'. Neigher app restarting nor device restarting helps. Only reinstall does.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
this always returns the language being set on application first launch.

How to select an storyboard programmatically?

I have an iOS 5 app that supports 3 different languages. Localizing an storyboard creates a copy in each language and each one must be translated.
The language selection depends on system settings, so if my iPhone has 'English' as a system language the choosen storyboard in my application will be the English one. If I change to Catalan, my app will show Catalan words.
This approach has several drawbacks in my opinion:
An app cannot be translated to languages not supported by Apple. This could be important for moniroty languages ( Catalan has not been supported until iOS 5 ).
If a user wants to have my app in Catalan but the rest of the system in English ?
So my question is how can I select the storyboard language at app startup time ? Is it possible at all ? Also it will work for strings localized using NSLocalizedString ?
Regards,
JoanBa
It seems like you can split the storyboard into localized versions just like localized.strings as to how to do it?
This question has been answered before although I haven't tried from experience.
After reading the Jacob answer and testing a little bit, this is the code that has worked for me:
int main(int argc, char *argv[])
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Reset system defaults to get the complete language list.
[defaults removeObjectForKey:#"AppleLanguages"];
// Default language choosen by user.
NSString *defLanguage = [defaults objectForKey:#"Language"];
NSArray *sysLangugages = [defaults arrayForKey:#"AppleLanguages"];
// System default language: first element of array.
NSString *sysLanguage = [sysLangugages objectAtIndex:0];
NSArray *array = [NSArray arrayWithObjects:defLanguage, sysLanguage, nil];
[defaults setObject:array forKey:#"AppleLanguages"];
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Just restarting the app after changing the default language, all localized resources are changed: strings and storyboards.
Also tried this code in AppDelegate.m, in method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
but it doesn't work. Only in main.m works fine. I don't know why.
Note that with this code you can have an application with a language not supported by iOS.
Regards,
JoanBa

According to the selected language either from application or from device can I convert the language in application?

I have to make my application multi language supported on the selection of particular radio buttons(i.e. English, French, German etc...). For that what I have to do? any external framework or API is required?
To do so from where I have to start & if any inbuilt method is there then please suggest me.
I want to add another thing, can it be possible according to the selected language of the device we can get all the effect in application same as the selected device language?
In code you can access current locale and language that user set in Settings:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(#"Current Locale: %#", [[NSLocale currentLocale] localeIdentifier]);
NSLog(#"Current language: %#", currentLanguage);
You need to support Localization in the app for all the hardcoded strings.
The below code returns the code for the device language selected. eg: en, zh, etc..
let langStr = Locale.current.languageCode
Based on the code returned, the repective Localizable.strings file displays the value for the strings.

Resources