extension UILabel {
@IBInspectable
private var localizedKey: String? {
get { fatalError("only set this value") }
set {
if let newValue = newValue {
text = newValue.localized()
}
}
}
}
extension String {
func localized() -> String? {
return NSLocalizedString(self, comment: "")
}
}
こう書いておくだけで、Android Studioのようにstoryboardからキーを設定してローカライズすることができるようになります。 すごい!画期的!! extension String {
private static let localizedEmptyKey = "##not exists##"
func localized() -> String {
let string = NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: String.localizedEmptyKey, comment: "")
if string == String.localizedEmptyKey {
fatalError("not exists localized key")
}
return string
}
}
キーが無かった場合には強制的にアプリを落としてしまうことによってキーの漏れに気づけるようになりました。 #!/bin/bash for file in `\find . -name \*.storyboard`; do IFS=$'\n' for xmlKey in `\grep 'keyPath="localizedKey"' ${file}`; do localizedKey=`echo $xmlKey | sed -e 's/.* keyPath="localizedKey" value="\([0-9a-zA-Z_-]*\)".*/\1/g'` for localizedStringFile in `\find ${SRCROOT} -name Localizable.strings`; do grep "\"${localizedKey}\" =" $localizedStringFile > /dev/null 2>&1 if [ $? != 0 ]; then echo "not exists key '${localizedKey}' in ${localizedStringFile}" exit 1 fi done done donestoryboardで設定されているlocalizedKeyをひっぱてきてLocalizable.stringsのファイルと突き合わせて存在しなければエラーを吐き出します。
こんにちは。virapture…
View Comments
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.com/zh-CN/register-person?ref=WTOZ531Y
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.com/tr/register?ref=OMM3XK51
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.info/id/join?ref=DB40ITMB
I don't think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.info/ph/join?ref=WTOZ531Y
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.info/pt-BR/join?ref=T7KCZASX
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/ar/register?ref=DB40ITMB