Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在应用程序内动态本地化iOS应用程序?_Iphone_Ios_Objective C_Cocoa Touch_Localization - Fatal编程技术网

Iphone 在应用程序内动态本地化iOS应用程序?

Iphone 在应用程序内动态本地化iOS应用程序?,iphone,ios,objective-c,cocoa-touch,localization,Iphone,Ios,Objective C,Cocoa Touch,Localization,目前,我有三个独立的.strings文件,分别用于英语、法语和德语 我在preferences部分中有一个UISegmentedControl,我想根据选择的段更改使用的默认.strings文件 当我测试应用程序时,本地化已经在应用程序外部工作,我甚至根据选择的段(如果选择了法国,则下面的标签文本从“南”变为“南”,如果重新选择了英语,则再次变回)使用以下方法对单个对象进行本地化: if(German segment) { NSString *path= [[NSBundle mainB

目前,我有三个独立的
.strings
文件,分别用于英语、法语和德语

我在preferences部分中有一个
UISegmentedControl
,我想根据选择的段更改使用的默认
.strings
文件

当我测试应用程序时,本地化已经在应用程序外部工作,我甚至根据选择的段(如果选择了法国,则下面的标签文本从“南”变为“南”,如果重新选择了英语,则再次变回)使用以下方法对单个对象进行本地化:

if(German segment) {
   NSString *path= [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];
   NSBundle* languageBundle = [NSBundle bundleWithPath:path];

    // Returns german version of localised string as label text.
    self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil]; 

    // I would like to set the default .strings file to 'de' here.
}

else if(French segment) {
   NSString *path= [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
   NSBundle* languageBundle = [NSBundle bundleWithPath:path];

   // Returns french version of localised string as label text
   self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil]; 

   // I would like to set the default .strings file to 'fr' here
}

else if(British segment) {
   NSString *path= [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
   NSBundle* languageBundle = [NSBundle bundleWithPath:path];

    // Returns english version of localised string as label text
    self.defaultName.titleLabel.text = [languageBundle localizedStringForKey:@"PREF_WEATHER_NORTH" value:@"" table:nil]; 

    // I would like to set the default .strings file to 'en' here.
}

现在,由于我已经在单击按钮时对单个字符串进行了本地化,我想知道是否可以一次性对每个
.strings
文件中的所有字符串进行本地化(因此我正在更改默认值。使用的字符串取决于选择的段)或者至少在可接受的代码量中以尽可能多的代码为目标

显然,目前我可以一个接一个地键入它们,但由于应用程序中使用了大量本地化字符串,因此这并不是一个真正的选项

如果有任何帮助,我们将不胜感激。

您可以阅读:并看看您如何做到这一点(第二个更适合您的需要)

但请阅读以下内容(也是第二个链接):

通常,您不应该在应用程序中更改iOS系统语言(通过使用AppleLanguages pref键)。这与设置应用程序中切换语言的基本iOS用户模式背道而驰,并且使用了未记录的首选项,这意味着在将来的某个时候,该项名称可能会更改,这将破坏您的应用程序

如果您想在应用程序中切换语言,可以通过手动加载捆绑包中的资源文件来实现。您可以使用NSBundle:pathForResource:ofType:inDirectory:forLocalization:实现此目的,但请记住,您的应用程序将负责所有本地化数据的加载


您可以将此本地化方法包装在LocalizationSystem类中:

@interface PLLocalizationSystem : NSObject

@property (strong, nonatomic) NSString *language;

+ (PLLocalizationSystem *) sharedLocalizationSystem;
- (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)comment;

@end

@implementation PLLocalizationSystem {
    NSBundle *_bundle;
}


- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment {
    return [_bundle localizedStringForKey:key value:comment table:nil];
}

- (void)setLanguage:(NSString *)newLanguage {
    ...
    NSString *path = [[NSBundle mainBundle] pathForResource:newLanguage ofType:@"lproj"];
    if (path) {
        _bundle = [NSBundle bundleWithPath:path];
        _language = newLanguage;
    } else {
        [self resetLocalization];
    }
    ...
    [[NSNotificationCenter defaultCenter] postNotificationName:kLocalizationChangedNotification object:nil];
}
并为更简单的访问做一些定义(在LocalizationSystem头文件中定义):

此外,您还可以从AppName-Prefix.pch文件导入此文件,以便从任何地方进行访问

接下来,在控制器中,您可以观察本地化更改并调用适当的方法

现在,您可以用与NSLocalizedString相同的方式使用它

另外,我不会在这里写所有的实现,但如果有人需要,我可能稍后会在githab上分享

更新。用法示例。

例如,您有要动态本地化的UIViewController:

@implementation MyCustomViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   //first of all - register for localization changes
   //i made for this purposes category for UIViewController(localizable)
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(localize)
                                                name:kLocalizationChangedNotification
                                              object:nil];
}

//here you should apply all localization
- (void)localize {
    [self setTitle:PLLocalizedString(@"myTitle", nil);
    [label setText:PLLocalizedString(@"labelTitle", nil)];
    ....
}

@end
在您的首选项中,当您更改语言时,请调用PLLocalizationSetLanguage(语言), 并且为该事件注册的所有控制器将自动本地化。当然,这种方法需要所有本地化都应用于
localize
方法


当控制器解除分配或卸载时,不要忘记删除observer“我想知道是否可以定位每个文件中的所有字符串”,您能澄清一下吗?你的意思是当选择一个语言段时,整个应用程序中的所有字符串都会更新吗?@WDUK就是这样。应用程序中几乎所有的字符串都是NSLocalizedString,一次更改所有这些字符串,会将应用程序中几乎所有字符串的显示更改为该特定语言。。。或您可以使用不同的XIB/故事板:)我已经阅读了第二个链接,但这并不是我真正想要做的。手机设置是否设置为法语并不重要——如果用户选择英国国旗,我希望以英语显示所有本地化字符串,就像我在上面的一个字符串示例中所做的那样。我只需要一种方法来一次性将.strings文件中的所有字符串作为目标,而不是一个接一个地进行。我假设您对每种语言使用多个字符串文件,对吗?第二个链接(如果您下载了源代码)定义了他们自己的NSLocalizedString验证,它使用LocalizationSystem.m方法从正确的文件中读取字符串。更改语言就像调用LocalizationSetLanguage(@“French”)一样简单,然后再次使用AMLocalizedStringYeah更新所有字符串,3个单独的文件,每种语言一个。我会检查一下,然后再给你回复,谢谢。这与Tjirps链接中的示例中的代码相同-它使用了大约多个类/文件来完成我已经在3/4行代码中完成的相同操作。它实际上不会更新语言,因为您仍然需要手动分别输入所有本地化对象。我现在已经可以做到了。。
@implementation MyCustomViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   //first of all - register for localization changes
   //i made for this purposes category for UIViewController(localizable)
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(localize)
                                                name:kLocalizationChangedNotification
                                              object:nil];
}

//here you should apply all localization
- (void)localize {
    [self setTitle:PLLocalizedString(@"myTitle", nil);
    [label setText:PLLocalizedString(@"labelTitle", nil)];
    ....
}

@end