iOS 国际化

iOS 国际化

创建 Localizable.strings 文件
1.Localizable.strings 这个是读取多语言字符串方法NSLocalizedString默认会加载的文件,如果自定了这个文件名字,则使用NSLocalizedStringFromTable指定table即可
2.InfoPlist.strings info.plist的字符串国际化文件,系统默认读取,名字固定

在pch文件中,写入以下宏定义,

#define AppLanguage @"appLanguage"
#define PJLocalString(key) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:nil]
public func NSLocalizedString(key: String, tableName: String? = default, bundle: NSBundle = default, value: String = default, comment: String) -> String
//匹配中文  "[^"]*[\u4E00-\u9FA5]+[^"\n]*?"
//在搜索框替换方式 NSLocalizedString($0, comment)

class Localized: NSObject {
    // MARK: 文件目录
    static let CHINESE = "zh-Hans"
    static let ENGLISH = "en"
    // MARK: FUNC
    class func language() -> String {
        let languages : [String] = NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages") as! Array
        let language : String = languages[0]
        if language.hasPrefix(CHINESE) {
            return CHINESE
        }
        return ENGLISH
    }

    class func bundle() -> NSBundle {
        guard let path = NSBundle.mainBundle().pathForResource(self.language(), ofType: "lproj"),
            let bundle = NSBundle(path :path)
            else { return NSBundle.mainBundle() }
        return bundle
    }
}

let currentLanguage = Localized.language() // en
NSUserDefaults.standardUserDefaults().setObject("zh-Hans", forKey: "AppleLanguages")


Xliff Editor for Mac 2.5.0.1破解

你可能感兴趣的:(iOS 国际化)