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.)
Related
IOS application provide the support of localization through the Localizable.strings file. If I want to change the file name for some obvious reasons where would I have to put that reference.
Can anyone please help.
How iOS localization works:
As you would already know, iOS provides a nice API for getting localized string as following.
NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:key
value:#""
table:nil];
And it also provides a macro for quick access as:
#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:#"" table:nil]
iOS, by default, looks for strings in Localizable.strings file. However, we can also provide a custom file for iOS to look for strings into. And this is where things get interesting.
To provide a custom file, we can use the API as mentioned above in following manner.
NSString *localizedString = [[NSBundle mainBundle] localizedStringForKey:key
value:#""
table:#"AnotherLocalizableStringsFile"];
The table parameter takes a string argument AnotherLocalizableStringsFile which is the file containing the strings.
Another interesting parameter is the value parameter that takes in a string that should be returned in case no string is found matching the given key.
So following piece of code would return Invalid Key assuming the provided key does not exist in the file.
NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:#"Wrong_key_passed"
value:#"Invalid Key"
table:#"TargetLocalizable"];
The solution:
By using the combination of these two interesting parameters, we can devise a method to suit our requirements. We will ask iOS to look for strings specific to target in target specific strings file and fall back to Localizablestrings file when it comes to loading generic strings common to all targets.
Here’s how the piece of code looks like.
NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:#"Key"
value:NSLocalizedString(#"Key", nil)
table:#"TargetLocalizable"];
So, iOS looks for the string first in the given TargetLocalizable.strings file. If it doesn’t find it there, it would search in the base Localizable.strings file.
So all I had to do was to place all the strings common to all targets in Localizable.strings file and put the additional and overridden strings specific to the target in TargetLocalizable.strings file.
The name is 'fixed' - a localizable is named Localizable. you can only decide to not use NSBundle localization and roll your own stuff
In my app I have a link in the menu that takes the user to our FAQ page online. I'm using a webview to load the page and so far so good.
But the problem is, my online page auto detects the user language and loads the correct localized version of the page.
If I open safari on the simulator and load my url, it gets redirected to the correct localized version of it, but using UIWebview on the app it loads the english (default) language.
I've done some research online, but couldn't find anything related to this matter.
Is there something I can/need to pass in order to the correct localized version to be loaded?
Thanks
EDIT:
I'm currently getting the phone language and passing in the URL, but what I'm looking for is for the UIWebview to behave like safari does, by calling the main URL and having the website choose the version based on your language.
You can get the app supported languages as an array of strings with [[NSBundle mainBundle] localizations]; and compare it with the system language like this:
+ (NSString *)preferredLanguage {
NSString *res = #"en";
NSArray *appSupportedLanguages = [[NSBundle mainBundle] localizations];
NSArray *appleLanguages = [NSLocale preferredLanguages];
if (appleLanguages && appleLanguages.count > 0) {
NSString *shortLangId = [appleLanguages.firstObject substringToIndex:2];
if ([appSupportedLanguages containsObject:shortLangId]) {
res = shortLangId;
}
}
return res;
}
The reason i'm using the shortened version here is that iOS 9 changed some languages' ids and it breaks my app — you should probably make some minor changes here. Nevertheless, this method allows you to get the language id you can use to open the proper FAQ page either like https://.../faq_fr.htm or with some server-side processing like https://...?lang=fr.
I made an ios / cocoa app a few months ago and now I'm trying to localize it. I have already localized XIBs, Strings, etc. successfully.
The problem that I am facing now is that I am parsing an XML (XML-SAX) file to build a list and retrieve more information over the itens, like a tree of information.
The parser simply extract data from the lines.xml file. I tried to localize the file and change every string inside the the just created versions of lines.xml, but I can only see the original strings being displayed.
Please, let me know if you need any other information.
A little help here would be really appreciated.
Thanks.
I found some not so clever way to do so..... it works but I think it is ugly:
if([language isEqual: #"en"]){
s = [[NSString alloc] initWithFormat:#"lines-en"];
}else{
s = [[NSString alloc] initWithFormat:#"lines"];
}
NSString *path = [[NSBundle mainBundle] pathForResource:s ofType:#"xml"];
In the app I'm making I have a lot of huge strings. I don't want to hardcode these into my code because it makes the code unbearably messy. When I made a similar android app, it was a simple matter of declaring the string in strings.xml as
<string name="hello_world">Hello World!!</string>
and accessing it in the java file with
getString(R.string.hello_world);
How can I do something like this with iOS?
You could put them into a .plist file and load them using the following code (which assumes a file called strings.plist which has been copied into the app bundle):
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"strings" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSString *string1 = [dict objectForKey:#"string1"];
However if you want to internationalize your app, then using Apple's Internationalization and Localization technology might be something to look at instead.
EDIT: you'll want to load that file/dictionary only once and keep it around the entire app lifetime; don't use the above code for every string access!
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);