iOS App Bundle 中的多个本地化 .strings 文件

您所在的位置:网站首页 app开发工具软件 iOS App Bundle 中的多个本地化 .strings 文件

iOS App Bundle 中的多个本地化 .strings 文件

2023-03-13 20:58| 来源: 网络整理| 查看: 265

本文介绍了iOS App Bundle 中的多个本地化 .strings 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个相当复杂的项目,由几个大型本地化子项目组成.

I have a fairly complicated project, consisting of several large localized sub-projects.

我的大部分子项目都通过一个 Localizable.strings 文件进行本地化.该文件被复制到 SubProjectName.bundle 目标中,该目标与主项目中的 SubProjectName.a 静态库结合使用.这很好用.

Most of my sub-projects are localized through a single Localizable.strings file. This file is copied into a SubProjectName.bundle target, which is used in conjunction with a SubProjectName.a static library in the main project. This works fine.

但是,我的一个子项目包含许多本地化的 .strings 文件.无论设备(或模拟器)如何配置,此项目都无法读取除英语以外的任何语言的字符串.

However, one of my sub-projects contains many localized .strings files. This project fails to read strings in any language other than English, regardless of how the device (or simulator) is configured.

比如这行代码总是返回英文字符串:

For example, this line of code always returns the English string:

[[NSBundle myResourcesBundle] localizedStringForKey:@"MY_TEST_STRING" value:@"" table:@"MyTable"]

其中 MyTable 对应于本地化为多种语言的 MyTable.strings 文件.当我查看 .app 包时,所有本地化内容都在那里,位于应用程序内的MyBundle.bundle"资源中.

Where MyTable corresponds to a MyTable.strings file localized into several languages. When I peek into the .app package, all the localizations are there, sitting inside the "MyBundle.bundle" resource within the app.

但是,以下代码在所有本地化中正确找到给定字符串的翻译:

The following code, however, correctly finds the translations for a given string in all localizations:

for (NSString *language in [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]) { NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle myResourcesBundle] pathForResource:language ofType:@"lproj"]]; NSLog(@"%@: %@", language, NSLocalizedStringFromTableInBundle(@"MY_TEST_STRING", @"MyTable", bundle, nil)); }

因此,当包是实际的 MyBundle.bundle/.lproj 文件夹时,字符串查找有效.但这显然违背了 iOS 提供的自动查找的目的.

So when the bundle is the actual MyBundle.bundle/.lproj folder, the string lookup works. But obviously this defeats the purpose of the automatic lookup provided by iOS.

(请注意,上面的 [NSBundle myResourcesBundle] 只是为子项目获取我的自定义包的静态便捷方法).

(Note that [NSBundle myResourcesBundle] above is simply a static convenience method to fetch my custom bundle for the sub-project).

--

编辑:我一直在尝试这个,如果我从我的子项目的包中删除 en.lproj 文件夹,那么它会正确使用设备或模拟器的语言环境.

Edit: I've been experimenting with this some more, and if I delete the en.lproj folder from my sub-project's bundle, then it correctly uses the locale of the device or simulator.

例如,我有:

MyApp.app/ | - MyResources.bundle/ | - en.lproj/ | - zh-Hans.lproj/

当我将模拟器(或设备)设置为简体中文时它会在 en.lproj 中查找字符串,即使语言环境是 zh-Hans时间>.如果我删除 en.lproj 文件夹并重新启动应用程序,它会正确使用 zh-Hans 本地化.

When I set the simulator (or device) to Chinese Simplified it looks up strings in en.lproj even though the locale is zh-Hans. If I delete the en.lproj folder and restart the app it correctly uses the zh-Hans localization.

推荐答案

我现在有一个 hacky 解决方案,但如果有人有更好的答案(或解释为什么上述方法不起作用),我将不胜感激.

I now have a hacky solution to this, but would appreciate if someone has a better answer (or explanation for why the above doesn't work).

p>

我扩展了我的 NSBundle 类别以包含首选语言资源:

I expanded my NSBundle category to include a preferred language resource:

标题

@interface NSBundle (MyBundle) + (NSBundle*) myResourcesBundle; + (NSBundle*) myPreferredLanguageResourcesBundle; @end

实施

@implementation NSBundle (MyBundle) + (NSBundle*) myResourcesBundle { static dispatch_once_t onceToken; static NSBundle *myLibraryResourcesBundle = nil; dispatch_once(&onceToken, ^ { myLibraryResourcesBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyResources" withExtension:@"bundle"]]; }); return myLibraryResourcesBundle; } + (NSBundle*) myPreferredLanguageResourcesBundle { static dispatch_once_t onceToken; static NSBundle *myLanguageResourcesBundle = nil; dispatch_once(&onceToken, ^ { NSString *language = [[[NSBundle myResourcesBundle] preferredLocalizations] firstObject]; myLanguageResourcesBundle = [NSBundle bundleWithPath:[[NSBundle myResourcesBundle] pathForResource:language ofType:@"lproj"]]; if( myLanguageResourcesBundle == nil ) { myLanguageResourcesBundle = [NSBundle myResourcesBundle]; } }); return myLanguageResourcesBundle; } @end

然后我有一个简单的宏来获取我的本地化字符串:

I then have a simple macro for getting my localized strings:

#define MyLocalizedDocumentation(key, comment, chapter) NSLocalizedStringFromTableInBundle((key),(chapter),[NSBundle myPreferredLanguageResourcesBundle],(comment))

此解决方案只是从 NSLocale 获取首选语言代码,然后检查该语言是否存在捆绑包.如果没有,它会退回到主资源包(也许它应该遍历 NSLocale 首选语言索引以检查包是否存在?有人知道吗?)

This solution simply gets the preferred language code from NSLocale and then checks to see if a bundle exists for that language. If not, it falls back to the main resource bundle (perhaps it should iterate through the NSLocale preferredLanguage indices to check if a bundle exists? Does anyone know?)

这篇关于iOS App Bundle 中的多个本地化 .strings 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3