有没有办法将本地化字符串导入iOS Playground?我有许多从右到左的语言翻译,其中包括格式代码,我必须用十六进制编辑器进行编辑(以确保代码处于正确的字节顺序),因此我希望手动检查格式化字符串的输出是否正确。
此外,有没有任何Mac OS文本编辑器可以在从左到右模式下显示阿拉伯语和希伯来语?
此外,有没有任何Mac OS文本编辑器可以在从左到右模式下显示阿拉伯语和希伯来语?
Resources
文件夹中创建一个en.lproj
(请注意,我的系统是英文的,您可能需要将其放在适合您语言的lproj
中才能使其在您的系统上正常工作)来使其正常工作。stringsDict
,我经常会忘记您必须指定键是本地化字符串。您不能只这样做:String.localizedStringWithFormat("next_step", beer)
你需要做的是:
String.localizedStringWithFormat(NSLocalizedString("next_step", comment: ""), beer)
对于所有寻找答案的人,这很简单:
let locale = Locale(identifier: "ru_RU")
let format = NSLocalizedString("localizedStringKey", comment: "")
let argument = 100 // some argument for localised string
print(String(format: format, locale: locale, argument))
支持复数形式,将.stringsdict
文件放在资源子文件夹中即可。
Swift 3:用于测试本地化的Playground示例
//: Playground - noun: a place where people can play
import UIKit
//current locale identifier
NSLocale.current.identifier //"en_US"
//available identifiers
NSLocale.availableLocaleIdentifiers //["eu", "hr_BA", "en_CM", "en_BI" ...]
// particular locales
let unitedStatesLocale = NSLocale(localeIdentifier: "en_US")
let chinaLocale = NSLocale(localeIdentifier: "zh_Hans")
let germanyLocale = NSLocale(localeIdentifier: "de_DE")
let indiaLocale = NSLocale(localeIdentifier: "en_IN")
let arabicLocale = NSLocale(localeIdentifier: "ar")
let hebrewLocale = NSLocale(localeIdentifier: "he")
//called in English
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"English (United States)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: chinaLocale.localeIdentifier)! //"Chinese (Simplified)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: germanyLocale.localeIdentifier)! //"German (Germany)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: indiaLocale.localeIdentifier)! //"English (India)"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: arabicLocale.localeIdentifier)! //"Arabic"
unitedStatesLocale.displayName(forKey: NSLocale.Key.identifier, value: hebrewLocale.localeIdentifier)! //"Hebrew"
//particular locale called in german
germanyLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"Englisch (Vereinigte Staaten)"
//particular locale called in arabic
arabicLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"الإنجليزية (الولايات المتحدة)"
//particular locale called in hebrew
hebrewLocale.displayName(forKey: NSLocale.Key.identifier, value: unitedStatesLocale.localeIdentifier)! //"אנגלית (ארצות הברית)"
//representing Numbers
let pi:NSNumber = 3.14159265358979
var numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
//differences in formatting in various locales
numberFormatter.locale = unitedStatesLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"
numberFormatter.locale = chinaLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"
numberFormatter.locale = germanyLocale as Locale!
numberFormatter.string(from: pi ) //"3,142"
numberFormatter.locale = indiaLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"
numberFormatter.locale = arabicLocale as Locale!
numberFormatter.string(from: pi ) //"٣٫١٤٢"
numberFormatter.locale = hebrewLocale as Locale!
numberFormatter.string(from: pi ) //"3.142"