一个 iOS 应用程序中的多个 Localizable.strings 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4811745/
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
Multiple Localizable.strings files in one iOS app
提问by Dimitris
Is it possible to have multiple components that have their separate localizations in an app?
是否可以在应用程序中拥有多个具有单独本地化的组件?
For example I want to localize strings in my app but also use ShareKit which is localized itself (so my bundle will contain 2 Localizable.strings in different locations). So, ShareKit contains folders like en.lproj and de.lproj that contain Localizable.strings files and I want to create the same for the rest of my project but keep them separate.
例如,我想在我的应用程序中本地化字符串,但也使用自身本地化的 ShareKit(因此我的包将在不同位置包含 2 个 Localizable.strings)。所以,ShareKit包含像en.lproj和de.lproj包含Localizable.strings文件的文件夹,我想创建为我的项目的其余部分相同,但将它们分开。
I have tried just keeping them separate and that resulted in the strings of my app not being used. Any ideas?
我曾尝试将它们分开,这导致我的应用程序的字符串未被使用。有任何想法吗?
回答by Scott Little
There is a way to do this that is quite simple.
有一种非常简单的方法可以做到这一点。
There are several macros for localizing, one of which is NSLocalizedStringFromTable()
. This takes three parameters:
有几个用于本地化的宏,其中之一是NSLocalizedStringFromTable()
. 这需要三个参数:
- The string to localize
- The table name, which is just a file name
- The comment, just like in
NSLocalizedString()
- 要本地化的字符串
- 表名,它只是一个文件名
- 评论,就像在
NSLocalizedString()
If you use a table name then you can have another strings file, i.e. if I do NSLocalizedStringFromTable(@"name", @"MyStrings", @"whatever");
then put my strings in MyStrings.strings
it will use that.
如果您使用表名,那么您可以拥有另一个字符串文件,即如果我这样做,NSLocalizedStringFromTable(@"name", @"MyStrings", @"whatever");
则将我的字符串放入MyStrings.strings
其中将使用该文件。
See Matt Gallagher's postabout this.
请参阅 Matt Gallagher关于此的帖子。
回答by meronix
every file/resource can be localizable, also the IB .xib files or images jpg/png, you just need to select a file in your project, open info, go in the "general" tab/section and press the "make file localizable"... then you can add any language you want, xCode will create a new file duplicating the first one and will put it in the right folder...
每个文件/资源都可以本地化,IB .xib 文件或图像 jpg/png,您只需要在项目中选择一个文件,打开信息,进入“常规”选项卡/部分,然后按“使文件可本地化“...然后你可以添加任何你想要的语言,xCode 将创建一个新文件复制第一个文件并将它放在正确的文件夹中......
luca
卢卡
回答by meronix
oh ok, I don't know ShareKit, but i guess you cannot edit its text files and just add your new words to that files, isn't it?
哦,好吧,我不知道 ShareKit,但我想你不能编辑它的文本文件,只是将你的新词添加到这些文件中,不是吗?
then you may consider to create your own "dictionary" file and try to localize it, then read it and put it in a NSDictionary where you can read the single words/value... you can insert a record in your file "myDict.plist" with key "greeting" of type String with value "ciao" for the italian one and "hallo" in the english one
那么您可以考虑创建自己的“字典”文件并尝试对其进行本地化,然后读取它并将其放入 NSDictionary 中,您可以在其中读取单个单词/值……您可以在文件“myDict.txt”中插入一条记录。 plist”,键为 String 类型的“greeting”,意大利语为“ciao”,英语为“hallo”
NSString *bundle = [[ NSBundle mainBundle] pathForResource:@"myDict" ofType:@"plist"];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:bundle];
NSString *greetingsTranslatedInTheGoodLanguage = [savedStock objectForKey:@"greeting"];
// just to try what you have read:
NSLog (@"translated by your own dictionary: %@", greetingsTranslatedInTheGoodLanguage);
[savedStock release];
luca
卢卡
ps read the comments too...
ps也看评论。。。
回答by jj0b
It is not possible to do what you are asking. You can have multiple Localizable.strings files, but only one per language in their respective .lproj folders.
不可能做你所要求的。您可以有多个 Localizable.strings 文件,但在其各自的 .lproj 文件夹中每种语言只能有一个。
I understand that you do not want to edit the ShareKit Localizable.strings files because that would be a pain when updating but I think you need to look at how much work it would actually take. Depending on how many languages you need to support it may be less work to localize your own strings and add them to the bottom of the ShareKit Localizable.strings files than to implement your own localization scheme.
我知道您不想编辑 ShareKit Localizable.strings 文件,因为这在更新时会很麻烦,但我认为您需要查看它实际需要多少工作。根据您需要支持多少种语言,与实现您自己的本地化方案相比,本地化您自己的字符串并将它们添加到 ShareKit Localizable.strings 文件的底部的工作可能更少。
BTW, to detect what language the device is currently set to you can use:
顺便说一句,要检测设备当前设置的语言,您可以使用:
NSArray *preferedLocalizations = [[NSBundle mainBundle] preferredLocalizations];
This gives you an array of two letter language code strings. The item at index 0 is the currently selected language. This could be helpful if you need to make logic decisions based on the language.
这为您提供了一个由两个字母的语言代码字符串组成的数组。索引 0 处的项目是当前选择的语言。如果您需要根据语言做出逻辑决策,这可能会有所帮助。