简体   繁体   English

本地化不适用于 Xcode

[英]Localization not working on Xcode

I can't get my app to work with localization.我无法让我的应用程序使用本地化。

  • I have created a Localizable.strings file and ticked the languages that I am localizing.我创建了一个Localizable.strings文件并勾选了我正在本地化的语言。 (For now I have base, english and spanish.) (现在我有基础,英语和西班牙语。)
  • Localized storyboard with three languages.具有三种语言的本地化故事板。
  • In Project settings > Info I checked Use Base internationalization and all languages are correctly added.在项目设置> 信息中,我检查了使用基础国际化并正确添加了所有语言。 (Moreover, beside the language it shows 2 Files Localized , which I guess they must be the storyboard and .strings file. (此外,除了它显示的语言2 Files Localized ,我猜它们必须是故事板和 .strings 文件。

I am using this example for testing:我正在使用此示例进行测试:

self.loginLabel.text = NSLocalizedString(@"login", @"Login text info");

This is how the Localizable.strings (Base) file is looking like:这是Localizable.strings (Base)文件的样子:

"login" = "Log in";

However, it is not working and instead it is showing login .但是,它不起作用,而是显示login Have I forgotten a key step?我是不是忘记了一个关键步骤?

BTW: I have tried already changing the login text just it case something was overriding the label.顺便说一句:我已经尝试过更改login文本,以防万一覆盖了标签。

Those are the steps indeed, I don't think you've left anything out.这些确实是步骤,我认为你没有遗漏任何东西。 I usually set the comment to nil instead of another NSString though - not sure if that makes a difference.我通常将注释设置为 nil 而不是另一个 NSString - 不确定这是否有区别。

However, rather than in only base, make sure you're adding the same line also to the English and Spanish files (obviously translated as appropriate).但是,请确保您在英语和西班牙语文件中也添加了相同的行(显然已根据需要进行了翻译),而不仅仅是在 base 中。

Next, for the language to show up, set the device to said language.接下来,要显示语言,请将设备设置为所述语言。 Close your app and start it again.关闭您的应用程序并重新启动它。 Strings should now show up in the other language.字符串现在应该以其他语言显示。

Note that the Simulator often has trouble with new translations, especially when they've been added to an already deployed app.请注意,模拟器经常会遇到新翻译的问题,尤其是当它们被添加到已部署的应用程序时。 In that case, remove the app from the Simulator, click on Product - Clean, and re-deploy.在这种情况下,从模拟器中删除应用程序,单击产品 - 清理,然后重新部署。

In my case, it didn't work because the UILabel text was set as "Attributed" instead of "Plain" in the Attributes Inspector.在我的情况下,它不起作用,因为 UILabel 文本在属性检查器中被设置为“属性”而不是“普通”。

Localization with a .strings file doesn't seem to work on Attributed strings. .strings 文件的本地化似乎不适用于属性字符串。

I even had a storyboard with mixed Plain and Attributed string labels, and the localization only worked on the Plain ones.我什至有一个混合了普通和属性字符串标签的故事板,并且本地化只适用于普通的。

For anyone having a similar issue in the future:对于将来遇到类似问题的任何人:

Check if your *.strings file has any non-printed characters.检查您的*.strings文件是否有任何非打印字符。 I used JavaScript for a simple task and didn't realize it included some whitespace characters.我将 JavaScript 用于一个简单的任务,但没有意识到它包含一些空白字符。

An easy way to check is to put the translation at the top of the file and see if it works.一种简单的检查方法是将翻译放在文件的顶部,看看它是否有效。 If it does, there might be some whitespace characters.如果是这样,可能会有一些空白字符。 The next steps are... binary search and remove the whitespace characters.接下来的步骤是...二分查找并删除空白字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM