How to support localization in android library

I want to create an android library (jar file). Jar files must be localized. How do I have a class:

class MyMessageService { //Needs to return different languages message according to locale String getMessage1(); } 

I tried to pack res/values/string.xml and R.class in a jar.

But in my main application that references the jar, I call

  String s = this.getResources().getString(mylibrary.localize.R.string.hello); 

I got "s", which is from my main project (resource identifier collides).

Is there a way to create an android library that supports localization? I can not allow the main application to import my library resource, because my library must be sent alone.

+4
source share
2 answers

You are doing the wrong way on Android. I also ran into a problem like you in order to reuse the user interface component between different projects Just try the Android library project (example lib.jar.path for the sample package), create lib proj and now you will get the generated class R, refer to lib proj to your proj project (in the "Project Properties" tab of Android) and use lib. jar.path.R to find your string or something else.

+1
source

Unfortunately, this cannot be done in a standard way with a can. But you can implement localization yourself - the current user language can be restored by Locale.getDefault (). After that, you can create a switch and return according to the language lines (you can simply save it in code or in some external file).

0
source

All Articles