added option to change language in-app

This commit is contained in:
Ahmet Inan 2024-05-21 07:56:17 +02:00
parent 2150004849
commit c56d30d33f
7 changed files with 62 additions and 0 deletions

View file

@ -44,6 +44,7 @@ import androidx.appcompat.widget.ShareActionProvider;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.os.LocaleListCompat;
import androidx.core.view.MenuItemCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@ -75,6 +76,7 @@ public class MainActivity extends AppCompatActivity {
private Decoder decoder;
private Menu menu;
private String currentMode;
private String language;
private int recordRate;
private int recordChannel;
private int audioSource;
@ -355,6 +357,7 @@ public class MainActivity extends AppCompatActivity {
state.putInt("recordRate", recordRate);
state.putInt("recordChannel", recordChannel);
state.putInt("audioSource", audioSource);
state.putString("language", language);
super.onSaveInstanceState(state);
}
@ -365,6 +368,7 @@ public class MainActivity extends AppCompatActivity {
edit.putInt("recordRate", recordRate);
edit.putInt("recordChannel", recordChannel);
edit.putInt("audioSource", audioSource);
edit.putString("language", language);
edit.apply();
}
@ -373,19 +377,23 @@ public class MainActivity extends AppCompatActivity {
final int defaultSampleRate = 44100;
final int defaultChannelSelect = 0;
final int defaultAudioSource = MediaRecorder.AudioSource.MIC;
final String defaultLanguage = "en-US";
if (state == null) {
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
AppCompatDelegate.setDefaultNightMode(pref.getInt("nightMode", AppCompatDelegate.getDefaultNightMode()));
recordRate = pref.getInt("recordRate", defaultSampleRate);
recordChannel = pref.getInt("recordChannel", defaultChannelSelect);
audioSource = pref.getInt("audioSource", defaultAudioSource);
language = pref.getString("language", defaultLanguage);
} else {
AppCompatDelegate.setDefaultNightMode(state.getInt("nightMode", AppCompatDelegate.getDefaultNightMode()));
recordRate = state.getInt("recordRate", defaultSampleRate);
recordChannel = state.getInt("recordChannel", defaultChannelSelect);
audioSource = state.getInt("audioSource", defaultAudioSource);
language = state.getString("language", defaultLanguage);
}
super.onCreate(state);
setLanguage(language);
Configuration config = getResources().getConfiguration();
EdgeToEdge.enable(this);
setContentView(config.orientation == Configuration.ORIENTATION_LANDSCAPE ? R.layout.activity_main_land : R.layout.activity_main);
@ -582,9 +590,30 @@ public class MainActivity extends AppCompatActivity {
showTextPage(getString(R.string.about_text, BuildConfig.VERSION_NAME));
return true;
}
if (id == R.id.action_english) {
setLanguage("en-US");
return true;
}
if (id == R.id.action_simplified_chinese) {
setLanguage("zh-CN");
return true;
}
if (id == R.id.action_russian) {
setLanguage("ru");
return true;
}
if (id == R.id.action_german) {
setLanguage("de");
return true;
}
return super.onOptionsItemSelected(item);
}
private void setLanguage(String language) {
this.language = language;
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(language));
}
private void storeScope() {
int width = scopeBuffer.width;
int height = scopeBuffer.height / 2;

View file

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>

View file

@ -3,6 +3,26 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="xdsopl.robot36.MainActivity">
<item
android:icon="@drawable/baseline_language_24"
android:title="@string/language"
app:showAsAction="always"
app:iconTint="@color/tint">
<menu>
<item
android:id="@+id/action_english"
android:title="@string/english" />
<item
android:id="@+id/action_simplified_chinese"
android:title="@string/simplified_chinese" />
<item
android:id="@+id/action_russian"
android:title="@string/russian" />
<item
android:id="@+id/action_german"
android:title="@string/german" />
</menu>
</item>
<item
android:id="@+id/action_toggle_mode"
android:icon="@drawable/baseline_auto_mode_24"

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="language">Sprache</string>
<string name="share">Teilen</string>
<string name="store_scope">Schirm Speichern</string>
<string name="toggle_mode">Modus umschalten</string>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="language">Язык</string>
<string name="share">Поделиться</string>
<string name="store_scope">Сохранить экран</string>
<string name="toggle_mode">Зафиксировать режим</string>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="language">语言</string>
<string name="share">分享</string>
<string name="store_scope">保存图像</string>
<string name="toggle_mode">切换模式</string>

View file

@ -25,6 +25,11 @@
<string name="rate_32000" translatable="false">32 kHz</string>
<string name="rate_44100" translatable="false">44.1 kHz</string>
<string name="rate_48000" translatable="false">48 kHz</string>
<string name="english" translatable="false">English</string>
<string name="simplified_chinese" translatable="false">简体中文</string>
<string name="russian" translatable="false">Русский</string>
<string name="german" translatable="false">Deutsch</string>
<string name="language">Language</string>
<string name="share">Share</string>
<string name="store_scope">Store Scope</string>
<string name="toggle_mode">Toggle Mode</string>