iPhone 应用程序本地化 - 英语问题?
我有一个应用程序,正在将其翻译成多种不同的语言。问题在于,该应用程序在澳大利亚和新西兰的值有一些不同,这两个国家都是英语国家。
我创建了一个 en_AU 和一个 en_NZ 语言文件,但它们都使用标准英语文件。我删除了英语文件,但这种情况仍然发生...
关于如何让它发挥作用有什么想法吗?
谢谢你,
--d
I have an app that I am translating to a bunch of different languages. The problem is that the app will have a few different values in Australia than will in New Zealand, which are both English speaking countries.
I have created an en_AU and an en_NZ language file, but they're both using the standard English file. I deleted the English language file, but it continues to happen...
Any ideas on how I can get this to work?
Thank you,
--d
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
iPhone 本地化(或者是本地化?)不会注意到用户设置的区域(即英国、澳大利亚、新西兰)。默认情况下只有一种“英语”语言翻译可用。但是,您可以进行一些修改以强制它使用不同的翻译设置 - 我刚刚这样做是为了在“英语”(美国)和“en_GB”(英式英语)之间进行选择。
在你的 main.m 文件中,修改它,使其看起来像下面这样(放入你自己的 NZ 或 AU 测试)
}
这会将用户语言(例如“en”)弹出到语言 NSString 中,以及用户区域设置(例如, NZ、GB、AU) 转换为区域设置 NSString。如果它们(就我而言)匹配 en 和 GB,那么我将用户默认语言首选项设置为“en_GB”,然后是“en”。
然后,在您的应用程序委托 application:didFinishLaunchingWithOptions 方法中,您想要删除刚刚使用代码设置的 NSUserDefaults 设置,
此时删除是安全的,因为所有捆绑包初始化都已完成。您的应用程序现在应该使用 en_GB.lproj 目录中的 Localization.strings 文件。
这是一个有点可怕的、hacky 的解决方案,但它对我有用。
iPhone localisations (or is that localizations?) do not take any notice of the Region the user sets (ie, UK, Aus, NZ). There is only one "English" language translation available by default. However, you can hack around with things to force it to use a different translation setting - I've just done this for choosing between "English" (US) and "en_GB" (British english).
In your main.m file, alter it so it looks something like below (put in your own tests for NZ or AU)
}
This pops the users language (eg "en") into the language NSString, and the users locale (eg, NZ, GB, AU) into the locale NSString. If they (in my case) match en and GB, then I set the users default language preference settings to be "en_GB", then "en".
Then, in your application delegates application:didFinishLaunchingWithOptions method you want to remove that NSUserDefaults setting you just set with the code
It's safe to remove at this point because all the bundle initialisation has been completed. Your app should now be using a Localization.strings file within the en_GB.lproj directory.
It's a bit of a horrible, hacky solution, but it works for me.
我想出了一个我认为是 rickerbh 接受的答案的稍微改进的版本。首先要认识到的是,用户默认值被组织到域中,并且
@"AppleLanguages"
键不是来自应用程序的域,而是来自域层次结构中更高的某个域。这意味着从用户默认值中删除它是完全安全的:调用此代码后,您会注意到调用
[defaults objectForKey:@"AppleLanguages"]
仍然返回一个值。因此,与其稍后删除@"AppleLanguages"
(根据应用程序的复杂性,这可能会出现问题),您需要执行相反的操作:删除@"AppleLanguages “
立即。本质上,这会将其重置回默认值并捕获系统对其所做的任何更改,例如,如果用户更改了她的首选语言。这就是我所做的:(
请注意,
filteredArrayUsingPredicateFormat:
是我编写的扩展方法。弄清楚它的作用或工作原理并不是火箭科学。)这会为列表中的每种语言创建本地化组合与用户所在地区。例如,如果原始列表是
es en en-GB
并且用户的区域是 AU,我们将得到es-AU es en-AU en en-GB
。请注意,es-AU
不存在,但这没有什么区别。由于应用程序找不到关联的本地化或资源,因此它会忽略它。I've come up with what I think is a slightly improved version of rickerbh's accepted answer. The first thing to realize is that user defaults are organized into domains, and the
@"AppleLanguages"
key comes not from the app's domain, but from some domain higher up the hierarchy of domains. This means it is completely safe to remove it from user defaults:After calling this code, you'll notice that calling
[defaults objectForKey:@"AppleLanguages"]
still returns a value. So, rather than deleting@"AppleLanguages"
at some point later, which could be problematic depending upon the complexity of your app, you want to do the opposite: delete@"AppleLanguages"
immediately. Essentially, this resets it back to its default value and captures any changes to it the system has made, if, for instance, the user has changed her preferred language.Here's what I do:
(Note that
filteredArrayUsingPredicateFormat:
is an extension method I wrote. It's not rocket science to figure out what it does or how it works.)This creates localizations for every language in the list combined with the user's region. E.g., if the original list was
es en en-GB
and the user's region is AU, we'll getes-AU es en-AU en en-GB
. Note thates-AU
doesn't exist, but it makes no difference. Since the app finds no associated localizations or resources, it just ignores it.Apple 在此记录了这一缺失的 iOS 功能。
Apple documents this missing iOS feature here.
我在德语方面也遇到了同样的问题,并且认为我找到了“正确”的解决方案。错误是,最初我的基本语言只是“德语(de)”,当添加“德语/奥地利(de_AT)”本地化时,文件被忽略。将基本语言更改为“德语/德国 (de_DE)”时,奥地利语翻译不会被忽略。
I had the same problems with German and think I found the "right" solution. The mistake was, that originally my base language was only "German (de)", when adding "German/Austria (de_AT)" localizations, the files were ignored. When changing the base language to "German/Germany (de_DE)" the austrian translations were not ignored.
这里值得注意的是,XCode 非常具有误导性 - 您可以转到“项目”(而不是“目标”)、“信息”、“本地化”,按出现在语言列表下方的 + 按钮,然后向下滚动到出现在“语言列表”中的语言列表的底部。弹出窗口,直到您到达“其他”(旁边有向右箭头),这将打开一个不错的大列表,其中包括语言的区域变体。然而,这些区域变体在 iPhone 上不起作用 - 你不会得到任何东西(如 Apple 所记录并在此处出现的另一个答案中引用)。显然,上面列出的基于代码的解决方案之一是必要的。
Worth noting here that XCode is very misleading - you can go to Project (not Target), Info, Localizations, press the + button that appears under the list of languages, then scroll down to the bottom of the list of languages that appear in the popup until you get to "Other" (with right arrow next to it), this will open a nice big list which includes regional variants for languages. However, these regional variants do not work on the iPhone - you don't get anything (as documented by Apple and referenced in another answer that appears here). Evidently one of the code-based solutions listed above is necessary.