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();
                }
            }
        }
    }
}
1
  • What exactly are you trying to do? Android supports localization using separate resource files. Commented Mar 29, 2011 at 15:04

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

1 Comment

Changing the local programically was what I was looking for.
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

2 Comments

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
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 ;) stackoverflow.com/questions/2596352/… stackoverflow.com/questions/2078289/… stackoverflow.com/questions/2264874/… stackoverflow.com/questions/2324418/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.