1 package org
.libreoffice
;
3 import android
.content
.Context
;
4 import android
.content
.SharedPreferences
;
5 import android
.content
.res
.Configuration
;
6 import android
.content
.res
.Resources
;
7 import android
.os
.Build
;
8 import android
.preference
.PreferenceManager
;
10 import java
.util
.Locale
;
12 public class LocaleHelper
{
14 private static final String SELECTED_LANG
= "org.libreoffice.selected.lang";
15 // value for language that indicates that system's default language should be used
16 public static final String SYSTEM_DEFAULT_LANGUAGE
= "SYSTEM_DEFAULT_LANGUAGE";
18 public static Context
onAttach(Context context
){
19 String lang
= getPersistedData(context
, Locale
.getDefault().getLanguage());
20 return setLocale(context
, lang
);
23 public static Context
setLocale(Context context
, String lang
) {
24 persist(context
, lang
);
25 return updateResources(context
, lang
);
28 @SuppressWarnings("deprecation")
29 private static Context
updateResources(Context context
, String lang
) {
31 if (lang
.equals(SYSTEM_DEFAULT_LANGUAGE
)) {
32 locale
= Locale
.getDefault();
34 locale
= new Locale(lang
);
36 Locale
.setDefault(locale
);
38 Resources res
= context
.getResources();
39 Configuration cfg
= res
.getConfiguration();
41 cfg
.setLayoutDirection(locale
);
43 res
.updateConfiguration(cfg
, res
.getDisplayMetrics());
47 private static void persist(Context context
, String lang
) {
48 SharedPreferences preferences
= PreferenceManager
.getDefaultSharedPreferences(context
);
49 preferences
.edit().putString(SELECTED_LANG
, lang
);
50 preferences
.edit().apply();
53 private static String
getPersistedData(Context context
, String lang
) {
54 SharedPreferences preferences
= PreferenceManager
.getDefaultSharedPreferences(context
);
55 return preferences
.getString(SELECTED_LANG
, lang
);