ios localizable strings are not working - ios

I have made an ios app that is localized to two languages (english and danish, english being default).
I have made a Localizable.strings with two subfiles, en and da.
Everything should be made correctly, and i see the english texts load fine from the strings file. But the danish ones do not.
I have tried to check the preferred language via below code and it is danish.
[[NSLocale preferredLanguages] objectAtIndex:0];
I have tried clean + delete and rebuild with no luck.
I know that the Localizable.strings file is working since it is getting the english texts. and i know that it is seeing the danish localization via the above line of code.
What am i missing?
Just to add a couple of examples:
from the EN:
"YesButton" = "Done";
"NoButton" = "Not yet!";
"NextButton" = "Next";
"BackButton" = "Back";
"EditButton" = "Edit";
"DoneButton" = "Done";
and the DANISH:
"YesButton" = "Færdig";
"NoButton" = "Ikke endnu!";
"NextButton" = "Næste";
"BackButton" = "Tilbage";
"EditButton" = "Redigér";
"DoneButton" = "Færdig";
and the code for getting the text would be:
[yesButton setTitle:NSLocalizedString(#"YesButton", nil) forState:UIControlStateNormal];
which is returning "Done" even when preferredLang is da (danish)!
Hope somebody has an idea? :)
Thanks in advance.
EDIT:::
Something was wrong with the actual DANISH-DENMARK localization. i dont know if apple updated it or what they did but it went from being called "Danish-Denmark" to just "Danish".
After making a whole new Danish localization and deleting the old one it worked! crazy stuff. keeps you scratching your head!

I had similar thing happened to me before. All I did to fix the problem was to change the string encoding from:
encoding:NSASCIIStringEncoding
to
encoding:NSUTF8StringEncoding
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
Now assume the problem is not string encoding. You can also work around it and do something like:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString yesButtonStr=#"";
NSString noButtonStr=#"";
NSString nextButtonStr=#"";
if([#"en" caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = #"Done";
noButtonStr= #"Not yet!";
nextButtonStr = #"Next";
//...
}
else if if([#"da" caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = #"Færdig";
noButtonStr = #"Ikke endnu!";
nextButtonStr = #"Næste";
//...
}
Then:
[yesButton setTitle:NSLocalizedString(yesButtonStr, nil) forState:UIControlStateNormal];

Try this,
NSString *translatedString = [self languageSelectedStringForKey:#"YesButton"]; // Give your key here
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"]; // give your language type in pathForResource, i.e. "en" for english, "da" for Danish
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:nil];
return str;
}
Hope this helps, Thanks. Happy coding

There was something wrong with the actual danish localization.
When i started making the localization it was called "Danish-Denmark". After an update of ios and/or xcode, it is now only "Danish".
So had to make a whole new localization.
Thank you apple...... NOT!

Related

Manually get specific localized string

Let say I already put everything on Localizable.strings for several language. As for now I use NSLocalizedString and it return the text based on [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"] setting. Now I need to get specific string for each language.
Let say I want to put it on select language setting and show it when user press Submit button. So it will show a dialog with a message "Select this language?" on respective language that user select.
You can get any translation from any language file by loading it manually:
- (NSString *)localizedString:(NSString *)string forCountry:(NSString *)countryCode {
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"Localizable" ofType:#"strings" inDirectory:nil forLocalization:countryCode];
NSBundle *dataBundle = [[NSBundle alloc] initWithPath:[bundlePath stringByDeletingLastPathComponent]];
return NSLocalizedStringFromTableInBundle(string, #"Localizable", dataBundle, nil); }
Does this help?

Localizing ios App

I have a working ios app in my hand. My company wants me to provide a "Translate" button in the app that would make it Arabic. Can I do this with iOS Localisation? The app is live in app store and I have to start with Localisation work now
You can access to localization strings in specific language, but in this case you need to implement your own function instead of NSLocalizedString. Here is an example:
NSString *Localized(NSString *key) {
static NSBundle *localizationBundle = nil;
static dispatch_once_t onceToken1;
dispatch_once(&onceToken1, ^
{
NSString *languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]; //The language code you need
localizationBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:languageCode ofType:#"lproj"]];
});
NSString *string = [localizationBundle localizedStringForKey:key value:nil table:nil];
return string; }
Just reload localizationBundle every time user switches language by translate button

Speech Synthezis API : which locale is used by NSLocalizedString?

I want to use iOS 7 new speech synthezis API, and my application is localized in french & english.
For this to work, 2 things have to be localized :
speech text : I put it in usual localizable.string file, and retrieve it in code using NSLocalizedString macro.
speech language : AVSpeechSynthesisVoice has to be chosen for corresponding language.
Class instanciation method is AVSpeechSynthesisVoice voiceWithLanguage:(NSString *)lang.
I'm currently using [NSLocale currentLocale].localeIdentifier as parameter for this method.
Problem : if user's device language is Portuguese, [NSLocale currentLocale] select portuguese prononciation, while text resolved by NSLocalizedString is english.
How can I know which locale is currently read by NSLocalizedString ?
Ok, I finally managed to make sense of Apple APIs :
[NSLocale currentLocale] : DOESN'T return current language picked by User in Settings > General > international, but returns the region code selected by user in same screen.
[NSLocale preferredLanguages] : This list DOES give device language, it's the first string in this list
[[NSBundle mainBundle] preferredLocalizations] return language bundle resolved by application. I guess this is what NSLocalizedString uses. It only has 1 object in my case, but I wonder in which cases it can have more than one.
[AVSpeechSynthesisVoice currentLanguageCode] returns the system predefined language code.
[AVSpeechSynthesisVoice voiceWithLanguage:] class instanciation method needs complete language code : with language AND region. (e.g. : passing #"en" to it will return nil object, it needs #"en-US", or #"en-GB"... )
[AVSpeechSynthesisVoice currentLanguageCode] gives default voice, determined by OS.
So this is what my final code looks like
// current user locale (language & region)
NSString *voiceLangCode = [AVSpeechSynthesisVoice currentLanguageCode];
NSString *defaultAppLang = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
// nil voice will use default system voice
AVSpeechSynthesisVoice *voice = nil;
// is default voice language compatible with our application language ?
if ([voiceLangCode rangeOfString:defaultAppLang].location == NSNotFound) {
// if not, select voice from application language
NSString *pickedVoiceLang = nil;
if ([defaultAppLang isEqualToString:#"en"]) {
pickedVoiceLang = #"en-US";
} else {
pickedVoiceLang = #"fr-FR";
}
voice = [AVSpeechSynthesisVoice voiceWithLanguage:pickedVoiceLang];
}
AVSpeechUtterance *mySpeech = [[AVSpeechUtterance alloc] initWithString:NSLocalizedString(#"MY_SPEECH_LOCALIZED_KEY", nil)];
frontPicUtterance.voice = voice;
This way, a user from NewZealand, Australien, GreatBritain, or Canada will get the voice that correspond most to his usual settings.
Vinzzz's answer was a great start -- I've generalised it to work with any language:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
NSString *voiceLangCode = [AVSpeechSynthesisVoice currentLanguageCode];
if (![voiceLangCode hasPrefix:language]) {
// the default voice can't speak the language the text is localized to;
// switch to a compatible voice:
NSArray *speechVoices = [AVSpeechSynthesisVoice speechVoices];
for (AVSpeechSynthesisVoice *speechVoice in speechVoices) {
if ([speechVoice.language hasPrefix:language]) {
self.voice = speechVoice;
break;
}
}
}

Localized resources in ios static library

I followed this Tutorial to use resources in my ios static library.
Now, I can use images, xib files, plist files... into my static library but I can't use localized resources..
In my bundle resources I can see all (for example) images into correct folders (es: english image into en.lproj folder)
But .. when I load the image, I see always the english image also when my language device is different from english.
I have same problem about localized strings: always I see only english localized strings.
* EDIT **
I could not find a better solution for this problem ..
I was in a hurry to fix this so I duvuto apply this method to solve the problem with the localized images:
+ (NSString*) getLocalizedPathImageWithImageName:(NSString*) aImageName
{
NSString *currentDeviceLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:BUNDLE_RESOURCESNAME ofType:#"bundle"];
NSString *completeLocalizedImageName = [aImageName stringByAppendingFormat:#"_%#",currentDeviceLanguage];
NSString *imagePath = [[NSBundle bundleWithPath:resourceBundlePath] pathForResource:completeLocalizedImageName ofType:USERBUTTON_PNG_FILEEXTENSION];
if (imagePath == nil) {
completeLocalizedImageName = [aImageName stringByAppendingFormat:#"_%#",DEFAULT_LANGUAGE];
imagePath = [[NSBundle bundleWithPath:resourceBundlePath] pathForResource:completeLocalizedImageName ofType:USERBUTTON_PNG_FILEEXTENSION];
}
return imagePath;
}
a better solution is welcome

iOS: How to Get the Device Current Language Setting?

There are some features within my application that are supposed to be based on the language settings of the device where it's running.
I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...
I'm familiar with the NSLocale object, but it seems to relate to the Region Format setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier] I get things like English (United States) instead of English; also, I think that what I need is the Language data and not the Region Format (am I right?).
Can anyone direct me to how to retrieve the language setting?
User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
If you want language in more readable form then use displayNameForKey:value: method of NSLocale:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
Try this:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:#"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
Getting language and region in Swift:
LF.log("language", NSLocale.preferredLanguages())
LF.log("locale", NSBundle.mainBundle().preferredLocalizations)
In my case I'm getting:
language: '(
"zh-Hans"
)'
locale: '(
en
)'
In Swift 4:
let currentLanguage = Locale.current.languageCode
It will give you just the language code, no country code.
Swift:
let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
Working solution:
let language = NSLocale.preferredLanguages()[0]
let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
//let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
print(languageCode)
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Find the solution in XCode's helper document, it wrote:
Getting the Current Language
To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
Use below code to fetch Localised language without having trouble to the en-india, en-us etc..
NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
In and After ios9 this code need to take in cosideration
To know the current language selected within your localizations use
[[NSBundle mainBundle] preferredLocalizations]
Example:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
To get two letter word
NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
Swift:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString

Resources