NSLocalizedString - Force country specific string file - ios

We are facing a strange behaviour with the handling of NSLoaclizedString with our bundle strings file.
We have maintained our strings file in:
de_AT (german in Austria)
de_DE (german in Germany)
We didn't setup any base/default strings file since we usually wan't to define per region which strings file should be used. So
Region Poland should use de_DE
Region Italy should use de_AT
During our tests we just recognized that NSLocalizedString just takes the first bundle (de_AT) independant of any Locale if the locale doesn't match.
Is there any way to define/control for which Locale we want to see the translations?
We only found a parameter "tableName" for the language, but no language & country specific locale... we don't want to create Localizable.strings for every available combination of language & country.
Thanks!

Ok we researched a lot, our current solution is to create independant Localizable.strings file and name them per locale:
de_AT.strings
de_DE.strings
Based on our mapping logic we do then set the right bundleName...

Related

NSLocalizedString is returning the key

In my framework app, I have some localization files added and have a method to get
localised strings as follow
-(NSString *)getCurrentLocale{
return NSLocalizedString(#"mykey", nil);
}
and I have installed this pod into one demo app, and trying to get the locale but it always returns the key, (it returns 'mykey')
I have double checked the format and name (Localizable.strings) within the string files
all files has proper format
but I do have locale strings as values in my Localizable.strings
ex: "mykey" = "ar_SA";
any Idea where I am going wrong?
Usually, this happens when you don't have the Localizable.strings file in the appropriate language folder (e.g. de.lproj). Also, ensure you haven't put your Localizable.strings file in the Base.lproj folder, as the same problem will occur.
To summarize, make sure your project Localizations have their Localizable.strings files in their respective language folders to provide the correct translations.
Finally, if the above are true (and this may be obvious), the device must have its locale set to actually pull strings from a given language.
Reference: Internationalization and Localization Guide

Specify base localization for Localized formatted APNS strings

My base localizations are in storyboards, but also in a struct like this:
struct Strings {
struct Restaurant {
static let makeCall = NSLocalizedString("Restaurant-makeCall", value: "Call %#", comment: "Restaurant-makeCall: title for button that allows you to call a place")
}
//...many more
}
This works pretty well because it keeps them in one place, defines a key that is separate from the base translation value, and gives me autocomplete when I'm using them: Strings.Restaurant.makeCall
To enter translations, I use the xliff import/export process: editor > export for localization. I found genstrings had trouble with the longhand form of NSLocalizedString.
This all works well until I get to Localized APNS Messages. Given an loc-key and optional loc-args, They search for a matching localization key.
For the translations, this works, because there's a Localizable.strings built for each translation when I import the translated xliff file.
There is no Localizable.strings file for my base translation. I attempted to make one, but the xliff export does not notice those fields to add to other translations, so I think that's not the right way to do it.
How do I add base translations in a way that will work for APNS?
One functional, but duplicative approach is to define them in both places:
Define keys (and unused values) in the Strings struct. The xliff export notices these and this allows other languages to provide translations
Define keys and values in a Localizable.strings file you create for your base translation. This allows APNS to find the values when you're in the base language, but xliff export can't see these.
This works, but if somebody ever changes one without keeping the other file up-to-date, differences in translation will occur that will be hard to detect, so I don't like it very much.

Best way to utilize strings in iOS

I am new to coding in Objective-C, but am coming from the Java world of mobile development. In Android, we use a strings resource, and then point to those in the Java class. Is there an analogous process in iOS that we should be using, or do we "hard-code" the strings into the implementation files? I haven't yet found a good tutorial using the localizable string (and .strings) file.
You can use the NSLocalizedString macro from Foundation to retrieve a localized string. Read here:
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
Example: build a bilingual (English-French) app.
Whenever you encounter a string to be displayed to the user, use NSLocalizedString instead of the actual string constant. For example:
instead
self.title = #"Welcome";
use
self.title = NSLocalizedString(#"Welcome");
Create two directories inside your app bundle as follows:
MyApp.app
English.lproj
Localizable.strings
French.lproj
Localizable.strings
In the first Localizable.strings file you can write key-value pairs like this:
"Welcome" = "Welcome";
In the second one:
"Welcome" = "Bienvenus";
When you add these files and folders to your application, an recompile it using the NSLocalizedString macro, you'll be able to use your app in French also when the system language is French.
Sure, there's a way to do that.
Take a look at the Apple doc here - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
There's not much more to it than that really. Although I tend to just hard code a lot of strings but it's definitely worth taking the time to learn about string resources because it's the basis of internationalisation.

Localizing strings from the Settings.bundle using InAppSettingsKit

I am attempting to use InAppSettingsKit to manage my settings. This uses the Settings.bundle with a .plist file and the .strings files for each of the languages being translated.
I can confirm that the translation of my strings is working properly outside of my application, using the Setting application. But when I am in my application, the translation is not occurring.
I think it comes down to code like this, from the InAppSettingsKit class IASKSettingsReader, with a couple logging statements that I thought my be helpful:
- (NSString*)titleForStringId:(NSString*)stringId {
NSLog(#"%#",[_bundle localizedStringForKey:stringId value:stringId table:self.localizationTable]);
NSLog(#"%#",[_bundle localizedInfoDictionary]);
return [_bundle localizedStringForKey:stringId value:stringId table:self.localizationTable];
}
If I understand correctly, this should be using a table with the name self.localizationTable as the source of the translation. This value is simply "Root". It's not a path to the Root.strings file in the selected language, so I am guessing that the method localizedStringForKey:value:table: must be using some global system reference that points to the correct path.
I have confirmed that the strings file name is "Root.strings" all around, with a capital R, including in the Root.plist file.
[_bundle localizedInfoDictionary] returns (null); It is doing this for two language settings of English and French.
I'm not sure how to debug this. Thanks for any help you can give.
I'm using InAppSettingsKit with localized text with no problems. Two things I can think of that you could check: are your Root.strings files located in the correct subdirectories of Settings.bundle (en.lproj and fr.lproj for English and French?
Is there a "Strings Filename" entry in your Root.plist? It should simply contain a string with value "Root"
It has been quite some time since I resolved this, and at the time, I didn't fully understand it. But, in the interest of closing out this question, I'll point to the following documentation for future reference:
NSBundle Class Reference
which refers to the following:
Resource Programming Guide
In the second document, refer to the section "String REsources -> Loading String Resources Into Your Code"
The solution contains a properly configured Root.strings file, which shows up in the file list like this:

How do I create more than 1 translation for a string in poEdit?

I followed this tutorial http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/ where Hello World is translated into German. If I want to translate Hello World to other language. Do I have to create another message.po and messge.mo file or I can add another translation in the same file or there's another way?
Normally you create one .po file per language, and split them up in different directories based on the locale name (like locale/de_DE/ in the tutorial), then you just use the specific file for the chosen locale as text domain in your implementation.

Resources