以前言語切替に関する記事を書いたが、未完であったので続きを書く。
前回、前々回の記事
https://qiita.com/QiitaD/items/bd4199f2b77167ebe4c3
https://qiita.com/QiitaD/items/cd06c029f1ed541c1bab
はじめに
言語に合わせたフォルダを用意する必要がある。
resにvaluesフォルダ、その下にstrings.xmlがあると思うが
これを英語用フォルダファイルとして使う。
そしてresの下に新たなフォルダvalues-jaを作成し
その下にstrings.xmlを作成し、
日本語用のレイアウトファイルとする。
このようにすることでAndroidのLocaleとしてセットされている言語の
レイアウトファイルを反映することができる。
この時、左上の「パッケージ」という部分がデフォルトでは「Android」になっている。
それだとValues-jaフォルダを作成しても表示されないので「パッケージ」を選択して頂きたい。
必要ファイル
・MainActivity.java
・activity_main.xml
・strings.xml(デフォルト、valuesフォルダにある、英語表示用レイアウトファイル)
・strings.xml(新規作成したvalues-jaフォルダに作成する、日本語表示用レイアウトファイル)
・MyContextWrapper.java(言語設定を切り替えるプログラム)
実装のポイント
まず言語切替の中核となる
MyContextWrapper.java内のプログラムは以下である。
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
return new MyContextWrapper(context);
}
}
wrap()内で言語切替を行っている。
以前のバージョンではupdateConfiguration()が使われていたが、
非推奨になったので、createConfigurationContext()を使った実装になっている。
参考サイト
https://null-i.net/index.html?AndroidJava/%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%A8%E8%8B%B1%E8%AA%9E%E3%81%AA%E3%81%A9%E3%81%A7%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88%E3%82%8B
このメソッドをMainActivity.javaから呼び出す。
呼び出す際の書き方は以下のとおりである。
MainActivity.java
Locale newLocale = Locale.JAPAN;
Context context = MyContextWrapper.wrap(newBase2, newLocale);
これで上書きされている。
ちなみにアプリを起動した時点で言語を指定(初期化処理)したい場合は、
attachBaseContext()を使う。
MainActivity.java
@Override
protected void attachBaseContext(Context newBase) {
newBase2 = newBase;
Locale newLocale = Locale.JAPAN;
Context context = MyContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
結果
今回はボタン押下による完全な言語切替まで実現できていないが、
デフォルトが日本語のエミュレータを
英語で初期化し、ボタン押下で日本語に切り替えるという機能まで実装できた。
サンプルはまとめの次を見て頂きたい。
まとめ
次の記事では完成形を見せたい。
以上
サンプル
・MainActivity.java
public class MainActivity extends AppCompatActivity {
Context newBase2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
Button button = findViewById(R.id.button);
TextView textView2 = findViewById(R.id.textView2);
button.setOnClickListener((View view1)->{
Locale newLocale = Locale.JAPAN;
Context context = MyContextWrapper.wrap(newBase2, newLocale);
if (isLocaleJp(context)){
textView2.setText("こんにちは");
}
});
}
@Override
protected void attachBaseContext(Context newBase) {
newBase2 = newBase;
Locale newLocale = Locale.ENGLISH;
Context context = MyContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
static boolean isLocaleJp(Context context){
// 渡された Contextが日本語なら trueを返す
Locale locale;
locale = context.getResources().getConfiguration().getLocales().get(0);
return (locale.equals(Locale.JAPAN) || locale.equals(Locale.JAPANESE));
}
}
・activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="244dp"
android:layout_marginTop="91dp"
android:text="@string/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="176dp"
android:layout_marginTop="243dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
・strings.xml(デフォルト、valuesフォルダにある、英語表示用レイアウトファイル)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">language_change_sample</string>
<string name="textView">Hello</string>
<string name="button">in Japanese</string>
</resources>
・strings.xml(新規作成したvalues-jaフォルダに作成する、日本語表示用レイアウトファイル)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">language_change_sample</string>
<string name="textView">こんにちは</string>
<string name="button">英語表示</string>
</resources>
・MyContextWrapper.java(言語設定を切り替えるプログラム)
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
return new MyContextWrapper(context);
}
}