如何本地化 iOS info.plist 文件中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25736700/
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
How to localise a string inside the iOS info.plist file?
提问by Soheil
As you might know the iOS 8requires NSLocationWhenInUseUsageDescriptionkey for using user's location. I have added this key and some general information into my info plist.
您可能知道iOS 8需要NSLocationWhenInUseUsageDescription密钥才能使用用户的位置。我已将此密钥和一些一般信息添加到我的信息 plist 中。
How can I use translation string inside the plist file ?
如何在 plist 文件中使用翻译字符串?
-- Update --
- 更新 -
I already have a Localizable string. I'm just wondering that can I use something like NSLocalizedString(MYSTRING,nil) inside the plist string. I know that I can create multiple file of info.plist for localisation but I was wondering there might be an easier way.
我已经有一个 Localizable 字符串。我只是想知道我可以在 plist 字符串中使用类似 NSLocalizedString(MYSTRING,nil) 的东西。我知道我可以为本地化创建多个 info.plist 文件,但我想知道可能有更简单的方法。
回答by Fahri Azimov
You should use InfoPlist.strings
file to localize values of Info.plist
. To do this, go to File->New->File
, choose Strings File
under Resource
tab of iOS
, name it InfoPlist
, and create. Open and insert the Info.plist
values you want to localize like:
您应该使用InfoPlist.strings
file 来本地化Info.plist
. 为此,请转到File->New->File
,Strings File
在Resource
选项卡下选择iOS
,将其命名为InfoPlist
,然后创建。打开并插入Info.plist
要本地化的值,例如:
NSLocationWhenInUseUsageDescription = "Description of this";
Now you can localize InfoPlist.strings
file with translations. Good Luck!
现在您可以InfoPlist.strings
使用翻译本地化文件。祝你好运!
EDIT:
编辑:
select the localization options, or enable localization if needed,
选择本地化选项,或根据需要启用本地化,
you should be able to see the file also on the left side editor.
您应该也可以在左侧编辑器中看到该文件。
EDIT:
编辑:
Hereis the official documentation for Info.plist
keys localization.
这是Info.plist
密钥本地化的官方文档。
Creditsto Marco, thanks for including the pics in this answer!
积分到马可,感谢包括在这个答案的图片!
回答by Zvi
All the above did not work for me (XCode 7.3) so I read Apple reference on how to do, and it is much simpler than described above. According to Apple:
以上所有对我都不起作用(XCode 7.3),所以我阅读了苹果参考资料,它比上面描述的要简单得多。据苹果称:
Localized values are not stored in the Info.plist file itself. Instead, you store the values for a particular localization in a strings file with the name InfoPlist.strings. You place this file in the same language-specific project directory that you use to store other resources for the same localization.
本地化的值不存储在 Info.plist 文件本身中。相反,您将特定本地化的值存储在名为 InfoPlist.strings 的字符串文件中。您将此文件放在用于存储相同本地化的其他资源的相同语言特定项目目录中。
Accordingly, I created a string file named InfoPlist.stringsand placed it in the xx.lproj folder of the "xx" language (and added it to the project using File->Add Files to ...). That's it. No need for the key "Localized resources can be mixed" = YES, and no need for InfoPlist.strings in base.lproj or en.lproj.
因此,我创建了一个名为InfoPlist.strings的字符串文件,并将其放置在“xx”语言的 xx.lproj 文件夹中(并使用 File->Add Files to ... 添加到项目中)。就是这样。不需要关键“本地化资源可以混合”= YES,也不需要base.lproj或en.lproj中的InfoPlist.strings。
The application uses the Info.plist key-value as the default value if it can not find a key in the language specific file. Thus, I put my English value in the Info.plist file and the translated one in the language specific file, tested and everything works.
如果在语言特定文件中找不到键,应用程序将使用 Info.plist 键值作为默认值。因此,我将我的英语值放在 Info.plist 文件中,并将翻译后的值放在特定语言的文件中,经过测试,一切正常。
In particular, there is no need to localize the InfoPlist.strings (which creates a version of the file in the base.lproj, en.lroj, and xx.lproj), and in my case going that way did not work.
特别是,不需要本地化 InfoPlist.strings(它在 base.lproj、en.lroj 和 xx.lproj 中创建文件的一个版本),在我的情况下,这种方式不起作用。
回答by tuvok
If something is not working make sure you added:
如果某些东西不起作用,请确保您添加了:
"Localized resources can be mixed" = YES
“本地化资源可以混合”= YES
into the info.plist. In my case the InfoPlist.strings files were just ignored.
进入 info.plist。在我的例子中,InfoPlist.strings 文件被忽略了。
回答by Matt S.
I would highly recommend reading Apple's guides, and viewing the WWDC resources listed here: Internationalization and Localization Topics
我强烈建议阅读 Apple 的指南,并查看此处列出的 WWDC 资源: 国际化和本地化主题
To specifically answer your question, when you add a new language to your project, you get an opportunity to choose what InfoPlist files to include (if you have multiple targets, you'll have multiple Info plist files). All you need to do to get the following screen is hit the + under Localizations and choose a new language to add support for.
为了具体回答您的问题,当您向项目添加新语言时,您有机会选择要包含哪些 InfoPlist 文件(如果您有多个目标,则将有多个 Info plist 文件)。要获得以下屏幕,您只需点击 Localizations 下的 + 并选择一种新语言来添加支持。
Once you've added, it will create the necessary string files in the appropriate lproj directories for the given language.
添加后,它将在给定语言的相应 lproj 目录中创建必要的字符串文件。
--EDIT--
- 编辑 -
Just to be clear, iOS will swap out the string for your Plist file based upon the user's currently selected language using the plist entry's key as the key in the localized strings file.
需要明确的是,iOS 将根据用户当前选择的语言使用 plist 条目的键作为本地化字符串文件中的键,为您的 Plist 文件换出字符串。
回答by Giuseppe Beccari
Tips
提示
Remember that the iOS Simulator exploits by default your system language. Please change the language (and region) in the iOS Simulator Setting too in order to test your translations.
The localisation string (see Apple docs here) should be
NSLocationWhenInUseUsageDescription = "Description of this";
and not (with quote "...")
"NSLocationWhenInUseUsageDescription" = "Description of this";
请记住,iOS 模拟器默认使用您的系统语言。请也更改 iOS 模拟器设置中的语言(和地区)以测试您的翻译。
本地化字符串(请参阅此处的Apple 文档)应该是
NSLocationWhenInUseUsageDescription = "Description of this";
而不是(用引号“...”)
"NSLocationWhenInUseUsageDescription" = "Description of this";
回答by Costas Bakoulias
Step by step localize Info.plist:
一步一步本地化Info.plist:
- Find in the Xcode the folder Resources (is placed in root)
- Select the folder Resources
- Then press the main menu File->New->File...
- Select in section "Resource" Strings File and press Next
- Then in
Save As
field write InfoPlist ONLY ("I" capital and "P" capital) - Then press Create
- Then select the file InfoPlist.strings that created in Resources folder and press in the right menu the button "Localize"
- Then you Select the Project from the Project Navigator and select the The project from project list
- In the info tab at the bottom you can as many as language you want (There is in section Localizations)
- The language you can see in Resources Folder
- To localize the values ("key") from info.plist file you can open with a text editor and get all the keys you want to localize
- You write any key as the example in any InfoPlist.strings like the above example
- 在 Xcode 中找到文件夹 Resources(放在 root 中)
- 选择文件夹资源
- 然后按主菜单 File->New->File...
- 在“资源”字符串文件部分中选择,然后按下一步
- 然后在
Save As
字段中仅写入 InfoPlist(“I”大写和“P”大写) - 然后按创建
- 然后选择在 Resources 文件夹中创建的文件 InfoPlist.strings 并在右侧菜单中按“Localize”按钮
- 然后从项目导航器中选择项目并从项目列表中选择项目
- 在底部的信息选项卡中,您可以使用任意数量的语言(在本地化部分中有)
- 您可以在资源文件夹中看到的语言
- 要本地化 info.plist 文件中的值(“键”),您可以使用文本编辑器打开并获取要本地化的所有键
- 您可以在任何 InfoPlist.strings 中编写任何键作为示例,如上例
"NSLocationAlwaysAndWhenInUseUsageDescription"="blabla";
"NSLocationAlwaysAndWhenInUseUsageDescription"="blabla";
"NSLocationAlwaysUsageDescription"="blabla2";
"NSLocationAlwaysUsageDescription"="blabla2";
That's all work and you have localize your info.plist file!
这就是全部工作,您已经本地化了 info.plist 文件!
回答by Edward
For anyone experiencing the problem of the info.plist not being included when trying to add localizations, like in Xcode 9.
对于在尝试添加本地化时遇到未包含 info.plist 问题的任何人,例如在 Xcode 9 中。
You need make the info.plist localiazble by going into it and clicking on the localize button in the file inspector, as shown below.
您需要通过进入并单击文件检查器中的本地化按钮来使 info.plist 本地化,如下所示。
The info.plist will then be included in the file resources for when you go to add new Localizations.
当您添加新的本地化时,info.plist 将包含在文件资源中。
回答by Cap
As RGML say, you can create an InfoPlist.strings, localize it then add your key and the value like this: "NSLocationWhenInUseUsageDescription" = "Help To locate me!";
正如 RGML 所说,您可以创建一个 InfoPlist.strings,对其进行本地化,然后添加您的键和值,如下所示:“NSLocationWhenInUseUsageDescription” = “Help To locate me!”;
It will add the key to your info.plist for the specified language.
它会将指定语言的密钥添加到您的 info.plist 中。
回答by d.ennis
In my case everything was set up correctly but still the InfoPlist.strings file was not found.
就我而言,一切都设置正确,但仍未找到 InfoPlist.strings 文件。
The only thing that really worked was, to remove and add the InfoPlist.strings files againto the project.
唯一真正有效的是,删除并再次将 InfoPlist.strings 文件添加到项目中。
回答by Mike Glukhov
In my case the localization not worked cause of '-' symbol in the name. Example: "aero-Info.plist" And localized files: "aero-InfoPlist.strings" and "aeroInfoPlist.strings" did not work.
在我的情况下,本地化不起作用,原因是名称中带有“-”符号。示例:“aero-Info.plist” 本地化文件:“aero-InfoPlist.strings”和“aeroInfoPlist.strings”不起作用。