在iOS Playground中测试本地化

9
有没有办法将本地化字符串导入iOS Playground?我有许多从右到左的语言翻译,其中包括格式代码,我必须用十六进制编辑器进行编辑(以确保代码处于正确的字节顺序),因此我希望手动检查格式化字符串的输出是否正确。
此外,有没有任何Mac OS文本编辑器可以在从左到右模式下显示阿拉伯语和希伯来语?
3个回答

1
我能够通过在游乐场的Resources文件夹中创建一个en.lproj(请注意,我的系统是英文的,您可能需要将其放在适合您语言的lproj中才能使其在您的系统上正常工作)来使其正常工作。
需要注意的一点是,如果您使用复数形式的stringsDict,我经常会忘记您必须指定键是本地化字符串。您不能只这样做:
String.localizedStringWithFormat("next_step", beer)

你需要做的是:

String.localizedStringWithFormat(NSLocalizedString("next_step", comment: ""), beer)

这是我所做的事情的要点: https://gist.github.com/designatednerd/fdfb916cc4d4ad3f33c25e917a95a2be

0

对于所有寻找答案的人,这很简单:

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文件放在资源子文件夹中即可。


0

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"

谢谢回复 - 我实际上是在寻找一种使用我的应用程序字符串文件的方法,而不是在多个语言环境中打印单个字符串。 - JustinHK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接