xcode NSLocalizedString 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10863513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:28:10  来源:igfitidea点击:

NSLocalizedString doesn't work

objective-ciosxcodecocoa-touchlocalization

提问by Elftheitroados

I want to use NSLocalizedString in my app but it always failed. What i do is:

我想在我的应用程序中使用 NSLocalizedString 但它总是失败。我做的是:

  • Define 3 Localizations in Project Properties (See screenshot bellow)
  • Create a new file: Resource Strings File
  • Check in the app bundle if file.strings is there
  • 在项目属性中定义 3 个本地化(见下面的截图)
  • 创建一个新文件:资源字符串文件
  • 如果 file.strings 存在,请检查应用程序包

Then I use NSLocalizedStrings as follow but it doesn't work!

然后我使用 NSLocalizedStrings 如下,但它不起作用!

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSString *v1 = NSLocalizedString(@"MyWindow", nil);

    //NSString *v1 = [[NSBundle mainBundle] localizedStringForKey:(@"MyWindow") value:@"" table:nil];
    [label setStringValue:v1];
}

In my 3 .strings files I define the below key/value: "MyWindow" = "Ma Fenetre";

在我的 3 个 .strings 文件中,我定义了以下键/值:"MyWindow" = "Ma Fenetre";

Normally, my label should display "Ma Fenetre" and not "MyWindows" You can download an example project here and tell me where is the issue.

通常,我的标签应该显示“Ma Fenetre”而不是“MyWindows”。您可以在此处下载示例项目并告诉我问题出在哪里

Bellow the content of Resources folder in my app bundle :

下面是我的应用程序包中 Resources 文件夹的内容:

DerivedData om$ find test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//MainMenu.nib

Thanks Elftheitroados

谢谢 Elftheitroados

回答by Adam

NSLocalizedStringuses Localizable.stringsfile by default. Change your File.stringsname and try again.

NSLocalizedStringLocalizable.strings默认使用文件。更改您的File.strings姓名并重试。

回答by Pratik Somaiya

For every time you make changes in .Strings file you need to clean your project and remove application from device and simulator. This is the only way to develop Localised application. Have a happy coding.!

每次在 .Strings 文件中进行更改时,您都需要清理项目并从设备和模拟器中删除应用程序。这是开发本地化应用程序的唯一方法。祝你编码愉快。!

回答by Evan Mulawski

As Adam stated, NSLocalizedStringuses Localizable.stringsfor a lookup table. To specify a custom table, use:

正如亚当说,NSLocalizedString使用Localizable.strings一个查找表。要指定自定义表,请使用:

NSLocalizedStringFromTable(@"MyWindow", @"File");

NSLocalizedStringFromTable(@"MyWindow", @"File");

Documentation

文档

回答by johnlholden

Need to take care over case sensitive file names: Localizable.strings not localizable.strings. Simply rename in Finder, delete reference in Xcode project and add Localizable string back to the project. Then make sure the appropriate Localization boxes are selected in the file inspector. Don't know if there's a better slicker process.

需要注意区分大小写的文件名:Localizable.strings 而不是 localizable.strings。只需在 Finder 中重命名,删除 Xcode 项目中的引用并将 Localizable 字符串添加回项目即可。然后确保在文件检查器中选择了适当的本地化框。不知道有没有更好的slicker工艺。