来自iOS {J}运行时的可本地化字符串

时间:2017-06-03 08:17:33

标签: ios nslocalizedstring

我为故事板中使用的字符串或修复的源文件提供了用户可本地化的字符串。这些文件在Localizable.string(西班牙语),Localizable.string(德语)等文件中定义。但我要求这些字符串可以不断变化。收到这些字符串作为对REST API调用的响应。我的问题是如何使用它。

当前代码为let text = NSLocalizedString("Some string", comment: "")

NSLocalizedString查找Localizable.string文件的位置。如何让NSLocalizedString从我的自定义词典/ Json中查找本地化的单词?

1 个答案:

答案 0 :(得分:1)

试试这个

首先,您需要将这些文件复制到文档目录中。

获取本地化标签

let localisedString = self.getLocalizatioString(key: "your key", stringFileName: "test.strings") // 

<强>功能

func getLocalizatioString(key : String?, stringFileName : String ) -> String {
            let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
            let destinationPath = doumentDirectoryPath.appendingPathComponent(stringFileName)
            let dict = NSDictionary.init(contentsOfFile: destinationPath)
            return (dict?.object(forKey: key!) as? String)!
        }

<强>输出

enter image description here

在字符串文件中 enter image description here

相关问题