3

I know that this post seems duplicated....but I already researched and I not find a answer...please help me.

I have a app translated for two language (Portuguese and English), my string and storyboard is portuguese, example:

[[self labelTest] setText:NSLocalizedString(@"Essa é uma label", nil)];

I have in my resources the files: en.lproj/Localizable.strings and pt.lproj/Localizable.strings

Localization native development region is set to "en".

What I understand here, is that if the user language is English en.lproj is loaded, when is Portuguese pt.lproj is loaded, when is French or any other language. en.lproj should be loaded, but that is not what happens. If I set in my device to Spanish, French or any other language, I see Portuguese language, I want show English.

Here are the posts I looked but it still fails:

iOS App default Language "en" is not applied

AppStore language description and "Localization native development region"

What is the meaning of "Localization native development region" entry in info.plist?

http://useyourloaf.com/blog/2010/05/10/fixing-xcode-default-development-region.html

How can I load a default language plist if the localized version does not exist for the current language?

What I have to do to always load the language in English when Localizable.strings is not found for the language of the user's device?

Community
  • 1
  • 1

3 Answers3

0

I've checked your links and in one of them is this already reported, but just be sure I want to mention that.

According to Apples Property List Key Reference you can set the default language via CFBundleDevelopmentRegion:

CFBundleDevelopmentRegion (String - iOS, OS X)

specifies the native region for the bundle. This key contains a string value that usually corresponds to the native language of the person who wrote the bundle. The language specified by this value is used as the default language if a resource cannot be located for the user’s preferred region or language.

This of course only works, if your language project is properly setup. So check if both languages are listed in your projects settings within the info tab and your resources are copied properly (Copy Bundle Resources)

Alexander
  • 7,178
  • 8
  • 45
  • 75
  • Hello, thank you for help me. I've checked this link and believe that I'm following the documentation properly. I will show the configurations of my project in a next question. – Rondinelli Morais Jun 24 '14 at 17:05
0

After break head, I finally find out an answer. I don't know if this answer is the best, but this works for me. See below:

#import "UtilHelp.h"

@implementation UtilHelp

//https://developer.apple.com/library/mac/qa/qa1391/_index.html
+ (NSString*)preferredUserLanguage {

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * globalDomain = [defaults persistentDomainForName:@"NSGlobalDomain"];
    NSArray * languages = [globalDomain objectForKey:@"AppleLanguages"];

    NSString* preferredLang = [languages objectAtIndex:0];

    return preferredLang;
}

@end


int main(int argc, char * argv[])
{
    NSString * preferredUserLanguage = [UtilHelp preferredUserLanguage];

    // Save the user's preferred order
    NSArray * defaultAppleLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];

    // When your app loaded, use this object for back AppleLanguages to default
    [[NSUserDefaults standardUserDefaults] setObject:defaultAppleLanguages forKey:@"DefaultAppleLanguages"];

    //
    // Check if language of the device contains between your files Localizable.string
    // YES: Good!
    // NO: Defines developmentLocalization as the preferred language
    //
    if(![[[NSBundle mainBundle] localizations] containsObject:preferredUserLanguage])
    {
        // Don't forget the configure CFBundleDevelopmentRegion with your preferred region
        NSString * developmentLocalization = [[NSBundle mainBundle] developmentLocalization];

        // Apple Guide:
        // ------------------------------------------------------------------------------------------
        // The returned array contains the
        // languages associated with the AppleLanguages key in the user's preferred order.
        // Thus, in most cases, you would simply want to get the first object in the array.
        //
        preferredUserLanguage = developmentLocalization;
    }

    // set the preferred User Language
    [[NSUserDefaults standardUserDefaults] setObject:@[preferredUserLanguage] forKey:@"AppleLanguages"];

    @autoreleasepool{
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}



@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // default AppleLanguages
    NSArray * defaultAppleLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"DefaultAppleLanguages"];

    [[NSUserDefaults standardUserDefaults] setObject:defaultAppleLanguages forKey:@"AppleLanguages"];

    return YES;
}

Hope this helps someone. Thakn you.

0

If you are still looking for a solution, I had a similar problem where I wanted to get strings from a specific localization file if there was no localization file for the currently selected language.

iOS Localizable.strings not being retrieved from BASE file

Included the link to my issue above and the text of my solution below (no need to follow the link unless you really want to).


NSString *path = [[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"];
if (path == nil)
{
    path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
}

NSLocalizedStringFromTableInBundle(@"TAB_TITLE_Camera", @"Localizable", [NSBundle bundleWithPath:path], @"");

Simple solution that does everything I need.

Works on both iOS 6.1 and iOS 7.1.


I think all you would need to do is change

pathForResource:@"Base"

to

pathForResource:@"en"

or whatever language you would like.

Hope this can help. Really glad I came up with this solution after a long unsuccessful search for an answer.

Community
  • 1
  • 1
GRW
  • 605
  • 12
  • 27