LoginSignup
6
5

More than 5 years have passed since last update.

Localeを指定して特定のstringを取得する方法

Posted at

AndroidでLocaleを指定してstringを取得したいことがあったのでまとめます。(API17以上でしか確認していません)

Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(Locale.ENGLISH); // ここで取得したいLocaleを指定します。
resources.updateConfiguration(configuration, null); // updateConfigurationを忘れずに…

// ここ以降で取得したstringはEngrishとなります。

// 取得したstringでなにかする。setText()とか…

configuration.setLocale(Locale.getDefault()); // 設定を元に戻すのを忘れないように。

サンプルコードを置いておきます。
ボタンをクリックすると文字列が、日本語と英語に切り替わるだけのアプリです。

ezgif.com-video-to-gif.gif

6
5
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
6
5