Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regional languages support #336

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import androidx.preference.PreferenceManager;

import menion.android.whereyougo.R;
import menion.android.whereyougo.gui.extension.activity.CustomActivity;
import menion.android.whereyougo.preferences.PreferenceValues;
import menion.android.whereyougo.preferences.Preferences;
import menion.android.whereyougo.utils.Logger;
import menion.android.whereyougo.utils.Utils;

import static menion.android.whereyougo.gui.extension.activity.CustomActivity.setLocale;


public class XmlSettingsActivity
extends AppCompatActivity
Expand All @@ -38,7 +37,11 @@ public void onCreate(Bundle savedInstanceState) {
// Set language
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String lang = sharedPreferences.getString(getString(R.string.pref_KEY_S_LANGUAGE), "");
setLocale(this, lang);
CustomActivity.setLocale(this, lang);

setContentView(R.layout.layout_settings);
setTitle(R.string.settings);
((TextView) findViewById(R.id.title_text)).setText(R.string.settings);

setContentView(R.layout.layout_settings);
setTitle(R.string.settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,34 @@ private void unbindDrawables(View view) {
}

public static void setLocale(Activity activity, String languageCode) {
String lang = languageCode;
Locale locale;
String lang;
Resources resources = activity.getResources();
Configuration config = resources.getConfiguration();

// we need to get language and region first even for default to correctly initialize Locale
if (languageCode.equals("default")) {
lang = Locale.getDefault().getLanguage();
lang = Locale.getDefault().toString();
} else {
lang = new Locale(languageCode).toString();
}

// need to parse language codes with dash and hyphen
String[] loc = lang.split("[-_]");
if (loc.length == 1) {
if (loc[0].equals("pt")) {
// nasty hotfix for Portuguese language selected without regional code
locale = new Locale(loc[0], loc[0]);
Comment on lines +271 to +273
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it the way around - rename the language pt_PT to pt.
I created Issue #342 for it.
After that these lines are not more necessary.

} else {
locale = new Locale(lang);
}
} else if (loc.length == 2) {
locale = new Locale(loc[0], loc[1]);
} else {
locale = config.locale;
}
Locale locale = new Locale(lang);

Locale.setDefault(locale);
Resources resources = activity.getResources();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
config.locale = locale;
Expand Down