Android : assets dir and multi language

2019-08-29 12:45发布

问题:

Good morning. Into my android application I have in assets directory a file changelog. I have creato also the directory assets-it and I have put into the file changelog in italian language but when I start the application it always display the file into assets directory also if the device language is set to italian. Why ?

回答1:

That is the expected behavior. Resources in res/ can be localized in that manner, but the assets/ directory cannot.

Since it's just a changelog, I'd recommend storing it as a localized string resource in res/values-LOCALE/. If you really want localization of assets, you could manually recreate it by getting the current locale and loading, e.g. assets/changelog-en, vs. assets/changelog-it, but I think that would generally not be a good idea, and it certainly isn't necessary in your case.



回答2:

Try this:

  1. put both changelog files into assets folder:

Path is for example: assets/en/changelog_file and assets/it/changelog_file

  1. enter this into your values/strings.xml:

<string name="changelog_file">file:///android_asset/en/changelog_file</string>

  1. put in your values-it/strings.xml:

<string name="changelog_file">file:///android_asset/it/changelog_file</string>

Like this you can call the string from the code: getResources().getString(R.string.changelog_file) and it will call the values-en or values-it according to device locale.