activity.xmlからstring.xmlに切り分けるのが意外と手間だったので指示するだけでいいのは楽。
あなた:
次のactivity.xmlからstring.xmlに切り分けて
<?xml version="1.0" encoding="utf-8"?>
ChatGPT:
こちらが activity.xml から文字列リソースを strings.xml に切り出した例です。
<resources> <string name="cover_button_text">表紙画像を読み込む</string> <string name="figure_button_text">挿絵を読み込む</string> <string name="open_file_button_text">ファイルを選択 (SAF)</string> <string name="open_settings_button_text">設定を開く</string> <string name="process_file_button_text">ファイルを処理</string> <string name="cancel_button_text">処理中止</string> </resources>
res/values/strings.xml に上記を追加してください。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- 表紙画像ファイルの読み込みボタン --> <Button android:id="@+id/coverButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/cover_button_text" /> <!-- 挿絵ファイルの読み込みボタン --> <Button android:id="@+id/figureButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/figure_button_text" /> <Button android:id="@+id/button_open" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/open_file_button_text" /> <!-- 設定を開くボタン --> <Button android:id="@+id/openSettingsButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/open_settings_button_text" /> <Button android:id="@+id/button_process" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/process_file_button_text" /> <Button android:id="@+id/button_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/cancel_button_text" /> <TextView android:id="@+id/textViewLog" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#EEEEEE" android:text="" android:textSize="16sp" android:scrollbars="vertical" /> </LinearLayout>
これで完全に多言語対応の準備ができた状態になります。 必要に応じて、strings.xml の values-ja, values-en などを作って翻訳も入れられますよ。
別言語用の strings.xml も作ってほしい場合は、言語と言葉を教えてくれたら用意します!
あなた:
androidアプリの多言語サポートの手順
ChatGPT:
Android アプリに多言語(多言語ローカライズ)をサポートする手順は、とてもシンプルです!以下の手順で、英語・日本語・その他の言語に対応できます。
これは デフォルト言語(通常は英語や日本語) の文字列です:
<!-- res/values/strings.xml --> <resources> <string name="app_name">青空EPUB変換</string> <string name="menu_settings">設定</string> </resources>
例えば:
言語 | フォルダ名 | 例 |
---|---|---|
日本語 | values-ja/ | res/values-ja/strings.xml |
英語 | values-en/ | res/values-en/strings.xml |
繁体字中国語 | values-zh-rTW/ | res/values-zh-rTW/strings.xml |
韓国語 | values-ko/ | res/values-ko/strings.xml |
<resources> <string name="app_name">Aozora EPUB Converter</string> <string name="menu_settings">Settings</string> </resources>
<resources> <string name="app_name">青空EPUB変換</string> <string name="menu_settings">設定</string> </resources>
<!-- activity_main.xml など --> <Button android:text="@string/menu_settings" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Android は、ユーザーのスマホの設定言語に応じて、自動的に values-xx フォルダから正しい文字列を選択してくれます。特別な処理は必要ありません。
Android 13(API 33)以上では「アプリ内言語切り替え」に公式対応しています。 詳しくは androidx.core.os.LocaleManagerCompat などを使ってカスタマイズ可能。
- Android Studio の Translations Editor を使うと、翻訳を一括管理できます。
- strings.xml をエクスポートして翻訳者に渡すこともできます。
Android Studio の Translations Editor を使うと、翻訳を一括管理できます。
strings.xml をエクスポートして翻訳者に渡すこともできます。
手順 | 内容 |
---|---|
1 | res/values/strings.xml に文字列定義 |
2 | values-ja/, values-en/ など言語別フォルダを追加 |
3 | 翻訳した strings.xml を作成 |
4 | @string/キー名 を UI に使う |
5 | 自動で切り替わる or 手動で切り替える |
必要なら、アプリ内で言語を変更する実装(設定画面で切り替え)も手伝えます!やってみます?
[blog:g:11696248318754550880:banner]