/* * Geopaparazzi - Digital field mapping on Android based devices * Copyright (C) 2016 HydroloGIS (www.hydrologis.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.geopaparazzi.library.locale; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import java.util.Locale; /** * Utilities for locale handling. * * <p>Taken from: http://snowpard-android.blogspot.it/2013/03/programmatically-change-language-in.html</p> * * @author Andrea Antonello */ public class LocaleUtils { /** * Changes the locale for the application. * * @param context * @param lang the language code. If "", the command is ignored. */ public static void changeLang(Context context, String lang) { if (lang.equalsIgnoreCase("")) return; Locale myLocale = new Locale(lang); saveLocale(context, lang); Locale.setDefault(myLocale); android.content.res.Configuration config = new android.content.res.Configuration(); config.locale = myLocale; context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); } private static void saveLocale(Context context, String lang) { String langPref = "Language"; SharedPreferences prefs = context.getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString(langPref, lang); editor.apply(); } /** * Get the current locale code from the preferences (set by changeLang). * * @param context * @return the current code or null if none was set in the preferences. */ public static String getCurrentLocale(Context context) { String langPref = "Language"; SharedPreferences prefs = context.getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE); String language = prefs.getString(langPref, ""); if (language.length() == 0){ return null; } return language; } }