【问题标题】:How to set a default language when localizable.string not exist?localizable.string 不存在时如何设置默认语言?
【发布时间】:2014-06-24 01:24:44
【问题描述】:

我知道这篇文章似乎是重复的……但我已经研究过,但没有找到答案……请帮助我。

我有一个应用程序翻译成两种语言(葡萄牙语和英语),我的字符串和情节提要是葡萄牙语,例如:

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

我的资源中有以下文件:en.lproj/Localizable.stringspt.lproj/Localizable.strings

本地化原生开发区域设置为“en”。

我在这里的理解是,如果用户语言是英语,则加载 en.lproj,何时加载葡萄牙语 pt.lproj,何时加载法语或任何其他语言。 en.lproj 应该被加载,但事实并非如此。如果我在我的设备中设置为西班牙语、法语或任何其他语言,我会看到葡萄牙语,我想显示英语。

这是我看过的帖子,但仍然失败:

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?

当找不到用户设备语言的 Localizable.strings 时,我必须怎么做才能始终加载英语语言?

【问题讨论】:

    标签: ios objective-c localizable.strings


    【解决方案1】:

    我已经检查了您的链接,其中一个链接已经报告过,但请确保我想提及这一点。

    根据 Apples Property List Key Reference,您可以通过 CFBundleDevelopmentRegion 设置默认语言:

    CFBundleDevelopmentRegion(字符串 - iOS、OS X)

    指定捆绑包的本地区域。此键包含一个字符串 通常对应于人的母语的值 谁写的包。此值指定的语言用作 如果无法为用户的资源找到资源,则使用默认语言 首选地区或语言。

    这当然只有在您的语言项目设置正确的情况下才有效。因此,请检查两种语言是否都列在您的 projects settings 信息 tab 中,并且您的资源是否已正确复制 (Copy Bundle Resources)

    【讨论】:

    • 您好,谢谢您的帮助。我已经检查了这个链接,并相信我正确地遵循了文档。我将在下一个问题中展示我的项目的配置。
    【解决方案2】:

    在break head之后,我终于找到了答案。我不知道这个答案是否是最好的,但这对我有用。见下文:

    #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;
    }
    

    希望这对某人有所帮助。 谢谢。

    【讨论】:

      【解决方案3】:

      如果您仍在寻找解决方案,我遇到了类似的问题,如果当前选择的语言没有本地化文件,我想从特定的本地化文件中获取字符串。

      iOS Localizable.strings not being retrieved from BASE file

      包括上面我的问题的链接和下面我的解决方案的文本(除非你真的想要,否则不需要点击链接)。


      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], @"");
      

      简单的解决方案,满足我的一切需求。

      适用于 iOS 6.1iOS 7.1


      我认为你需要做的就是改变

      pathForResource:@"Base"
      

      pathForResource:@"en"
      

      或任何您喜欢的语言。

      希望这会有所帮助。很高兴我在长时间寻找答案后想出了这个解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2013-07-12
        • 2016-09-07
        • 2014-09-14
        相关资源
        最近更新 更多