3

I'm looking for a way to change the value of a string resource dynamically. I have tried to use reflection but it claims 'invalid value for field'.

I use the strings for values within the layout, but need to swap them out for different languages.

Please see the attached code below.

public class Lang{
    public static void langInit(){
        java.lang.reflect.Field[] langStringFields = R.string.class.getFields();

        Log.d(Global.TAG,"--> Lang Listing: " + langStringFields.length);
        Log.d(Global.TAG,"--> Pref for language:");

        String prefInLang = Prefs.cPrefsGet.getString("in_lang","en");

        String fieldName = null;
        String fieldValue = null;
        String newFieldName = null;

        String tmpA = "one";

        for (int i=0; i<langStringFields.length; i++){
            java.lang.reflect.Field field = langStringFields[i];

            fieldName = field.getName();

            try {
                fieldValue = Global.gActivity.getString(field.getInt(R.string.class));
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (fieldName.substring(0,2).equals("lo")){
                try {
                    newFieldName = R.string.class.getField(prefInLang + "_" + fieldName.substring(3)).getName();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.d(Global.TAG,"--> Field: " + fieldName + "value: " + fieldValue + "new field:" + newFieldName);
                try {
                    java.lang.reflect.Field field2 = Class.forName(R.string.class.getName()).getDeclaredField(newFieldName);
                    field2.setAccessible(true);
                    field2.set(R.string.class,tmpA.toString());
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105

4 Answers4

3

Use built-in mechanism of localization, introduced in android. You don't need to change anything. You just need to specify the new strings.xml for each locale.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • No I am not looking for localization unless I can set it programically. Users will choose the applications desired language from a menu, not based on their device local. – Kevin Parker Mar 29 '11 at 15:53
  • Then I would advice to implement your own localization subsystem instead of trying set something to generated class. I don't know why you need this, but it is not common user experience on android to have in-app localization. – Vladimir Ivanov Mar 29 '11 at 17:31
2

If you want to change current language for you app you can do it by using standard built-in localization features and changing locale programatically.

Community
  • 1
  • 1
GrAnd
  • 10,141
  • 3
  • 31
  • 43
1

you should rather add a locale value to your resources and duplicate them : one for each language, thus letting the device choose the right one according to it's settings : check it there http://developer.android.com/resources/tutorials/localization/index.html

olamotte
  • 917
  • 9
  • 20
  • No I am not looking for localization unless I can set it programically. Users will choose the applications desired language from a menu, not based on their device local – Kevin Parker Mar 29 '11 at 15:54
  • then i guess it's for the purpose of your app, i find it is an interesting issue : i don't have time to focus on this by now, but a quick search gave me these posts : please keep me posted on your progress ;) http://stackoverflow.com/questions/2596352/android-change-language-settings-locale-for-the-device http://stackoverflow.com/questions/2078289/android-controling-the-user-language http://stackoverflow.com/questions/2264874/android-changing-locale-within-the-app-itself http://stackoverflow.com/questions/2324418/android-forced-locale-resetted-on-orientation-changes – olamotte Mar 29 '11 at 17:33
1

I believe using Android's built-in localization features is preferable to implementing it by hand. Here's a guide you can refer to:

http://developer.android.com/guide/topics/resources/localization.html

Unless, of course, we misunderstood your use case, but it does really sound like you are trying to do standard localization :-)

Bruno Oliveira, Developer Programs Engineer, Google

Bruno Oliveira
  • 5,056
  • 18
  • 24