3

I wanted to apply localization in my app dynamically so is it possible to define strings in java file and fetch that strings to our layout xml file i.e as we do @string/anystringname replacing this by the strings defined in java file to our layout file @somestring defined to java file ????

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shailee
  • 71
  • 1
  • 8

3 Answers3

6

Basically, No.

You cannot manipulate resource file dynamically. You need to implement such functionality on your own.

Here is an example:

english.json

{
    "hello": "Hello"
}

french.json

{
    "hello": "Bonjour"
}
public class StringManager {
    private static StringManager sInstance = null;
    private static String sDefaultLanguageCode = "en";

    private Map<String, JSONObject> mLanguageMap = new ArrayMap<>();

    private StringManager() {
    }

    public static StringManager getInstance() {
        if (sInstance == null) {
            synchronized (StringManager.class) {
                if (sInstance == null)
                    sInstance = new StringManager();
            }
        }
        return sInstance;
    }

    public static void setDefaultLanguageCode(String languageCode) {
        sDefaultLanguageCode = languageCode;
    }

    public void addLanguage(String languageCode, JSONObject json) {
        mLanguageMap.put(languageCode, json);
    }

    public String getString(String key) {
        return mLanguageMap.get(mDefaultLanguageCode).getString(key);
    }

    public String getString(String languageCode, String key) {
        return mLanguageMap.get(languageCode).getString(key);
    }
}

public class SomeActivity extends Activity {
    ...
    public void initStringResources() {
        // assume englishJsonObject is created from english.json
        StringManager.getInstance().addLanguage("en", englishJsonObject);
        StringManager.getInstance().addLanguage("fr", frenchJsonObject);

        StringManager.setDefaultLanguageCode("fr");
    }

    public void useSomeString() {
        String helloString = StringManager.getInstance().getString("hello");
        // helloString will be "Bonjour"

        // and you can get specific language string
        String englishHelloString = StringManager.getInstance().getString("en", "hello");
    }

    // this may be called from Button or other UI component
    public void onLanguageChanged(String languageCode) {
        StringManager.setDefaultLanguageCode(languageCode);
        // and update UI components that display localized strings
    }
}
nexus5x
  • 457
  • 4
  • 13
  • hii..tried ur solution but its not working..can I keep the file in asset folder and then after reading that file can that file be used in layout – Shailee May 30 '16 at 10:23
  • As I mentioned, you cannot change xml files at runtime. If you tried my solution like ` ` this, of course, will not work. Instead, you can set text of TextView programatically. If you declare a TextView in xml file like this `` You can use my solution as follows; TextView textView = (TextView) findViewById(R.id.my_textview); textView.setText(StringManager.getInstance().getString("hello")); – nexus5x May 30 '16 at 10:36
  • okay got your solution ..i need to do it programatically only creating one common function for all and callin string – Shailee May 30 '16 at 13:35
  • Check this out , there is library for the same with added asynchronisation https://github.com/B3nedikt/restring – ArpitA Apr 20 '21 at 10:24
0

This is not possible. It is however possible to change the language of your app at runtime. This allows you the switch between the strings.xml for the different languages. A library I used for this is the Android localization library All your activities have to extend the LocalizationActivity which extends the AppCompatActivity. You just have to call setLanguage("en"); To change your apps language, and use a different strings.xml

In this thread you can read more possible solutions.

Community
  • 1
  • 1
Rockney
  • 10,380
  • 2
  • 20
  • 26
  • yaa throug values folder for different languages is possibl to fetch all strings but how to do it dynamically without input of all different language strings ?? i.e if i create json for all strings storing in db getting through key values and use that string in my layout file – Shailee May 28 '16 at 08:08
  • @Shailee this solution only works when you include the localizations in the strings.xml. The library allows you to switch your language. The strings.xml for a different language are now used. This allows dynamic localization. If however you want to retrieve the localized strings from a databse. Your best option is probably to subclass TextView and add a custom attribute. https://developer.android.com/training/custom-views/create-view.html. In your textview you can implement the logic for reading the right string. – Rockney May 28 '16 at 08:21
  • ok so is it possible to change languages dynamically on its button click through this localization library without making new string file for different languages? – Shailee May 28 '16 at 08:33
  • It is possible to change language dynamically on a button click. If you want to read the strings from a other source than strings.xml You should implement your own logic to localize the strings. Why would you not seperate the languages in different strings.xml? Except if you would want to load them from a other source – Rockney May 28 '16 at 08:54
  • yes but actually i don't want to add everytime string.xml file for localization but should change dynamically so – Shailee May 30 '16 at 05:09
-6
<TextView  
android:id="@+id/tv"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/tv"
/>

<string name="tv">old text</string>//old text

final TextView textView = (TextView) findViewById(R.id.rate);
textView.setText("new text");//new text
SHINTO JOSEPH
  • 377
  • 4
  • 17