LoginSignup
12
11

More than 5 years have passed since last update.

Storyboard上のUITextViewを.stringsでローカライズしたときにテキストが反映されない

Posted at

仕様なのかバグなのかわからないけど

"VR6-6g-rqx.text" = "翻訳";

みたいなのを頑張って書いても、UITextViewのテキストの値はうまく翻訳されてくれないようだ。
StackOverflow でいくつかワークアラウンドが紹介されている。

以下に自分のとった方法を紹介する。

例えばMain.storyboard上にあるHogeViewController上のFugaTextViewを日本語に翻訳したいとする

1. Main.Storyboard用の翻訳ファイルであるMain.stringsに翻訳を指定

Main.strings(Japanese)
"FUGA_TEXT" = "日本語翻訳";

2. 翻訳が存在すれば読み込むようなユーティリティメソッドを準備

QIUtils.m
+ (UITextView *)applyTranslationToUITextView:(UITextView *)target withKey:(NSString *)key andStoryBoardName:(NSString *)storyBoardName
{
    NSString *translation = NSLocalizedStringFromTable(key, storyBoardName, @"");
    if (![translation isEqualToString:key]) {
        target.text = translation;
    }
    return target;
}

3. HogeViewControllerのviewDidLoadで翻訳を読み込む

HogeViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [QIUtils applyTranslationToUITextView:self.fugaTextView           
                                  withKey:@"FUGA_TEXT" 
                        andStoryBoardName:@"Main"];
}
12
11
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
12
11