Skip to content

Commit

Permalink
Fix Translation Logic Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX authored and markpash committed Jun 3, 2024
1 parent 9ebcdeb commit abcf26c
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 28 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ android {
}

configurations {
all {
configureEach {
exclude group: 'org.json', module: 'json'
}
}

dependencies {
implementation 'androidx.activity:activity-compose:1.8.2'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.activity:activity-compose:1.9.0'

This comment has been minimized.

Copy link
@wqerrewetw

wqerrewetw Jun 3, 2024

Contributor

This library is for Jetpack Compose, not for traditional View.

implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.github.zcweng:switch-button:0.0.3@aar'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
Expand Down
41 changes: 40 additions & 1 deletion app/src/main/java/org/bepass/oblivion/CountryUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
package org.bepass.oblivion;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.Log;
import android.util.Pair;

import java.util.Arrays;
import java.util.Locale;

public class CountryUtils {
public static String getCountryCode(String name) {
public static Pair<String, String> getCountryCode(Context context, String name){
Resources resources = context.getResources();

String[] translatedNames = resources.getStringArray(R.array.countries);
// Change locale to English
LocaleHelper.goEn(context);

// Translate the incoming country name to English
Pair<String, String> countryCodeAndName = translateToEnglish(context, name, translatedNames);

// Get the ISO country code using the translated name
String countryCode = getCoCo(countryCodeAndName.first);

// Restore the original locale
LocaleHelper.restoreLocale(context);
// Return the pair of country code and full country name
return new Pair<>(countryCode, countryCodeAndName.second);
}

private static Pair<String, String> translateToEnglish(Context context, String name, String[] translatedNames) {
Resources resources = context.getResources();
String[] englishNames = resources.getStringArray(R.array.englishCountries);
for (int i = 0; i < translatedNames.length; i++) {
if (translatedNames[i].equalsIgnoreCase(name)) {
return new Pair<>(englishNames[i], translatedNames[i]);
}
}

// If translation not found, return the original name
return new Pair<>(name, name);
}

private static String getCoCo(String name) {
for (String code : Locale.getISOCountries()) {
Locale locale = new Locale("en", code); // Set the language to English
if (locale.getDisplayCountry(Locale.ENGLISH).equalsIgnoreCase(name)) {
Expand Down
17 changes: 3 additions & 14 deletions app/src/main/java/org/bepass/oblivion/LocaleHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bepass.oblivion;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
Expand Down Expand Up @@ -86,21 +87,8 @@ private String getLanguageName(String languageCode) {
return languageCode;
}
}

// private void restartActivity(Context context) {
// Log.d("BeforeRestart", fileManager.getString(
// SELECTED_LANGUAGE
// ));
// context.recre
//// Intent intent = new Intent(context.getPackageManager().getLeanbackLaunchIntentForPackage(context.getPackageName()));
//// intent
//// context.startActivity(context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()));
//// System.exit(0);
// }

@SuppressLint("ObsoleteSdkInt")
public void restartActivity(Context context) {
Log.d("BeforeRestart", fileManager.getString(SELECTED_LANGUAGE));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
((Activity) context).recreate();
} else {
Expand All @@ -111,3 +99,4 @@ public void restartActivity(Context context) {
}
}
}

52 changes: 52 additions & 0 deletions app/src/main/java/org/bepass/oblivion/LocaleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.bepass.oblivion;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.Log;
import android.util.Pair;

import java.util.Locale;

public class LocaleHelper {
private static Locale originalLocale;

public static void goEn(Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
originalLocale = configuration.locale; // Save the original locale

// Change locale to English
configuration.setLocale(new Locale("en"));
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

public static void restoreLocale(Context context) {
if (originalLocale != null) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();

// Restore the original locale
configuration.setLocale(originalLocale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
}
public static String restoreText(Context context, String text) {
if (originalLocale != null) {
Resources resources = context.getResources();
// Load the English country names
String[] englishNames = resources.getStringArray(R.array.englishCountries);

This comment has been minimized.

Copy link
@wqerrewetw

wqerrewetw Jun 3, 2024

Contributor

Use this method to get the localized country name, no translation is required, only the country code is needed.

public static String getDisplayName(String language,String country){
     return new Locale("",country).getDisplayCountry(Locale.forLanguageTag(language));
}
restoreLocale(context);
// Load the translated country names
String[] translatedNames = resources.getStringArray(R.array.countries);

// Find the translated name by matching the English name
for (int i = 0; i < englishNames.length; i++) {
if (englishNames[i].equalsIgnoreCase(text)) {
return translatedNames[i];
}
}
}
return text; // Return the original text if no translation is found or original locale is not set
}
}
19 changes: 13 additions & 6 deletions app/src/main/java/org/bepass/oblivion/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.bepass.oblivion;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
Expand All @@ -21,7 +24,7 @@ public class SettingsActivity extends StateAwareBaseActivity {
private Spinner country;
private CheckBox.OnCheckedChangeListener psiphonListener;
private CheckBox.OnCheckedChangeListener goolListener;

private Context context;
private void setCheckBoxWithoutTriggeringListener(CheckBox checkBox, boolean isChecked, CheckBox.OnCheckedChangeListener listener) {
checkBox.setOnCheckedChangeListener(null); // Temporarily detach the listener
checkBox.setChecked(isChecked); // Set the checked state
Expand All @@ -32,8 +35,8 @@ private void setCheckBoxWithoutTriggeringListener(CheckBox checkBox, boolean isC
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

fileManager = FileManager.getInstance(getApplicationContext());
context = this;
fileManager = FileManager.getInstance(this);

LinearLayout endpointLayout = findViewById(R.id.endpoint_layout);
LinearLayout portLayout = findViewById(R.id.port_layout);
Expand Down Expand Up @@ -84,8 +87,8 @@ public void handleOnBackPressed() {
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
String name = parent.getItemAtPosition(position).toString();
String code = CountryUtils.getCountryCode(name);
fileManager.set("USERSETTING_country", code);
Pair<String, String> codeAndName = CountryUtils.getCountryCode(context, name);
fileManager.set("USERSETTING_country", codeAndName.first);
}

@Override
Expand Down Expand Up @@ -129,8 +132,10 @@ public void onNothingSelected(AdapterView parent) {}
}

private int getIndexFromName(Spinner spinner, String name) {
String ccn = CountryUtils.getCountryName(name);
String newname = LocaleHelper.restoreText(this,ccn);
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().equals(name)) {
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(newname)) {
return i;
}
}
Expand All @@ -146,8 +151,10 @@ private void settingBasicValuesFromSPF() {
String countryCode = fileManager.getString("USERSETTING_country");
int index = 0;
if (!countryCode.isEmpty()) {
LocaleHelper.goEn(this);
String countryName = CountryUtils.getCountryName(countryCode);
index = getIndexFromName(country, countryName);
LocaleHelper.restoreLocale(this);
}
country.setSelection(index);

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">забвение</string>
<string name="stateText">Не подключен</string>
<string name="basedOnWarp">Основано на Warp</string>
<string name="aboutApp">О приложении</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">遗忘</string>
<string name="stateText">未连接</string>
<string name="basedOnWarp">基于Warp</string>
<string name="aboutApp">关于应用</string>
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">Oblivion</string>
<string name="app_name" translatable="false">Oblivion</string>
<string name="stateText">Not Connected</string>
<string name="basedOnWarp">Based on Warp</string>
<string name="aboutApp">About the App</string>
Expand Down Expand Up @@ -73,4 +73,38 @@
<item>United Kingdom</item>
<item>United States</item>
</string-array>
<string-array name="englishCountries">
<item>Austria</item>
<item>Belgium</item>
<item>Brazil</item>
<item>Bulgaria</item>
<item>Canada</item>
<item>Croatia</item>
<item>Czechia</item>
<item>Denmark</item>
<item>Estonia</item>
<item>Finland</item>
<item>France</item>
<item>Germany</item>
<item>Hungary</item>
<item>India</item>
<item>Ireland</item>
<item>Italy</item>
<item>Japan</item>
<item>Latvia</item>
<item>Netherlands</item>
<item>Norway</item>
<item>Poland</item>
<item>Portugal</item>
<item>Romania</item>
<item>Serbia</item>
<item>Singapore</item>
<item>Slovakia</item>
<item>Spain</item>
<item>Sweden</item>
<item>Switzerland</item>
<item>Ukraine</item>
<item>United Kingdom</item>
<item>United States</item>
</string-array>
</resources>

0 comments on commit abcf26c

Please sign in to comment.