LoginSignup
21
18

More than 5 years have passed since last update.

文字列のローカライズはextensionで

Posted at

役に立つかどうかよくわからないTipsシリーズ

ローカライズが必要なアプリに
もろにこんな感じで書いてはいませんか?

let label = UILabel()
label.text = NSLocalizedString("thank you", comment: "")

こういうのを extension しておくのは大事

extension String {

    func localize(comment: String = "") -> String {
        return NSLocalizedString(self, comment: comment)
    }
}

そもそも comment って使う?
必要にかられたことはないけど、必要なときもあるかもしれないから
省略可能な引数にしてしまいます

let label = UILabel()
label.text = "thank you".localize()

文字列に.localize()を付けるだけでローカライズ対応できました

21
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
18