Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Android App Localization

I have read here that localization in Android apps occurs via XML files that are deployed with the app.

Is it possible to dynamically load those XML files at run-time into the app?

If not, is it possible to override the binding between the UI XML and the resources XML in such a way that I can bind to my own, dynamically loaded XML file instead of one in res/values?

Thanks

like image 440
Micah Avatar asked Jan 11 '13 16:01

Micah


People also ask

What is dynamic localization?

Localization or Internationalization is the way of presenting your apps to the global audiance. In other terms, its the ability of your app to present itself in multiple languages.

How do I localize my apps on Android?

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.

How do I change localization in Android?

You'll find this screen either in the System Settings app: Languages, or System Settings: System: Languages and input. The Language preference screen should contain one entry called “English (Europe)”. Click Add language and add a fallback language.

What is i18n in Android?

Internationalization (i18n) is the engineering effort to make a product — an app in our case — ready for localization, i.e. adaptation to different regions. It includes important simplifications in the source code needed to avoid individual changes when entering foreign markets.


2 Answers

Is it possible to dynamically load those xml files at runtime into the app?

No, sorry.

If not, is it possible to override the binding between the UI xml and the resources XML in such a way that I can bind to my own, dynamically loaded xml file instead of one in res/values?

Nothing in res/values/ is ever used, unless you use it. Hence, there is nothing to "override".

For example, suppose that you have a TextView. You want it to display some text. You want that text to be localized. Normally, you would set up a series of string resources, one per translation, and then use those string resources with the TextView (e.g., android:text in a layout, or setText() in Java). In your case, you would not set up string resources, but "do your own thing", and call setText() as needed.

What you lose in your approach is the automatic conversion. If the user switches languages while your app is running, Android will treat that as a configuration change and restart your activities as they return to the foreground. Normally, that would automatically load in the new string resources. In your case, it won't, because you are not using string resources. Instead, you will need to tell Android to not restart your activities (via android:configChanges) and manually reload all your TextView, etc. widgets yourself. And if you forget one, well, the user is just screwed.

IMHO, unless somebody is pointing a gun at your head to force you to try to change translations without shipping new versions of the app, just use string resources and ship new versions of the app.

like image 145
CommonsWare Avatar answered Oct 04 '22 22:10

CommonsWare


If you'd like, you could reinvent the wheel and implement your own Locale-based resource loading.

You can get the current Locale via

Locale = context.getResources().getConfiguration().locale;

Then use the results from that to appropriately load your own Strings/Drawables/whatever. Of course you have to do all that programmatically (XML layouts won't automatically use the correct resources).

I would highly recommend sticking with the standard localization techniques.

like image 44
Bryan Herbst Avatar answered Oct 04 '22 20:10

Bryan Herbst