ios - 在 iOS App 中添加自定义语言方言
问题描述
我正在对一个应用程序进行本地化,在该应用程序中我遇到了一个国家语言方言的问题。我的主要问题是,是否有任何添加自定义语言的规定。
例如:
假设有两种语言:
PL for Poland
UK for Ukraine
我需要支持pl-uk,即波兰乌克兰语
解决方案
如果可以从系统首选项中选择此方言,则添加 apl-UK.lproj
将是有意义的,但事实并非如此。如果您有本地设置,恐怕除了自己管理本地化之外没有其他解决方案 - 而且它不适用于 Interface Builder 文件。
最简单的方法是将所有pl-UK
差异存储在一个单独的文件中(它可以是.strings
您存储到pl.lproj
文件夹中的一个(您用波兰语本地化 - 以尊重系统的语义)。然后在自定义函数中,您加载这些字符串:
func localize(_ string : String, comment: comment) {
guard !isUkrainianPolish else {
return NSLocalizedString(string, comment: comment)
}
// retrieve the cache and check if a key with string exists
if let url = Bundle.main.url(forResource: "localizable_pl_UK" /* or any other name*/, withExtension: "strings", subdirectory: nil, localization:"pl"),
let data = try? Data(contentsOf: url),
let plist = (try? PropertyListSerialization.propertyList(from: data, options: [], format: nil)) as? [String:String] {
// cache the dictionary where you want
return plist[string] ?? NSLocalizedString(string, comment: comment)
}
}
根据代码的组织,您可以在单例或处理本地化的类中实现该功能。
推荐阅读
- python - 如何让 pylint 在全局范围内忽略单个模块的未使用导入?
- python - 如何从 SQL Server 表中提取日期并将其存储在 Pandas 的变量中,没有噪音,只有日期
- floating-point - 如何在不使用宏或反复试验的情况下找到用于浮点数的基数
- c# - C# Coverlet 结果始终为空
- pandas - 在 Python/Pandas 中循环通过两个 csv 相同的代码仅适用于其中一个文件
- php - 另一种方法使异步 REST API 与 laravel 没有 ajax
- node.js - 如果我要集成两个单独的 API,那么 MERN 堆栈是否合适?
- google-calendar-api - Google 日历和夏令时的结束
- parsing - Bigquery SEC 归档数据集中似乎缺少数据
- android - 如何解决此错误(正文中是所有必需的信息)