首页 > 解决方案 > 在 iOS App 中添加自定义语言方言

问题描述

我正在对一个应用程序进行本地化,在该应用程序中我遇到了一个国家语言方言的问题。我的主要问题是,是否有任何添加自定义语言的规定。

例如:

假设有两种语言:

PL for Poland

UK for Ukraine

我需要支持pl-uk,即波兰乌克兰语

标签: iosxcodelocalization

解决方案


如果可以从系统首选项中选择此方言,则添加 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)
    }
}

根据代码的组织,您可以在单例或处理本地化的类中实现该功能。


推荐阅读