I have a special request to implement dynamic language packs and themes in Android. This basically means:
to download a zip file containing a file named strings.xml (containing the translation)
replace in the application the file /res/values/strings.xml with the one downloaded
Is this possible? Thank you for your help.
No. Things inside the Res folder are static and you can't recreate the R class in runtime.
Why don't you try using sqlite for your situation?
Related
I am using Assets files as help files in my app and have well over a dozen. I am porting the app to multiple languages. Where do the alternative language asset files go?
I am already using the "res/values" directories for language files (values, values-es, etc) for use within the app. I thought the "Assets" directory was for help files and items like that.
I am trying to NOT muddy my values folders with the many help files that I am including and was using "activity.getAssets().open( file )" to read the files.
Also, some of these "Asset" files are different language pictures.
Can you put the files in /res instead of /assets? This has built in support for multiple languages, there is an easy to follow guide here.
Basically, if your original text is in /res/values/strings.xml, for example, you would put your translations in /res/values-{ISO LANGUAGE CODE}/strings.xml
For example, your French translation would be in /res/values-fr/strings.xml.
Android will pick the appropriate translation file according to the locale of the user's phone.
There are some good explanations of the other differences between /res and /assets here.
For the voice recognition in my app (using Vosk) I have defined the specific asset folder as resource string.
I.e. values\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-en-us-0.15</string>
...
and values-de-rDE\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-de-0.15</string>
...
So I can access the asset directory via
val assets = Assets(activity)
val assetDir = assets.syncAssets()
val modelDir = activity.getString(R.string.language_directory)
recognitionListener.model = Model("$assetDir/$modelDir")
This way the correct directory is always chosen based in the active locale.
In my case, those are located at models\src\main\assets\sync\<language_directory>
Make folder in assets:
1. htmlpagesNL
2. htmlpagesUS
Copy file from htmlpagesNL and paste to htmlpagesUS and Translate
Use url inside Nl string file:
file:///android_asset/htmlpagesNL for NL translation
Use url inside Us string file:
file:///android_asset/htmlpagesUS for US translation
Support different languages
Support different languages and cultures
I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?
You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml
Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.
Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.
I have text files named hierarchy1.txt, hierarchy1.1.txt and heirarchy1.1.1.txt in my android project->res->raw folder. These files have json objects. I m trying to read from these files but these are not getting added in my R.file. How do I resolve this?
You should use underscores instead of dots in your filenames.
If you rename your file to hierarchy1_1.txt you should be able to access it with:
context.getResources().openRawResource(R.raw.hierarchy1_1);
I have 2 different functionality to be implemented as part of a single application.
They have different screen flows and BL to be implemented but a single apk,so it is transparent to the end user.
I have 100 screens for each of the flow,so can I have custom folder names in the resources directory for easy maintenance and loose coupling.
eg: res
-layout
-layout_savings
-layout_checking
-drawable
-drawable_savings
-drawable_cheking
currently we have only the following structure
res
-layout
-layout-land
-drawable
-values
Will there be any problem while generating the R.java file.
Any help would be appreciated.
for what you need , i suggest you to use the assets directory instead of res. in assets dir you can sort your files anyway you like (dir name/filename) . You access them with AssetManager.
this require a little change in code but i think this is the only way if you want to sort your file like that , res folder doesn't allow custom directory names.
(Or of course you can store files in the sdcard)
I have two resources file in res/values directory: string.xml and names.xml
how can I retreive all resources from names.xml only
the method
Field[] x=R.string.class.getFields();
retrieves resources from both files.
how can this be acheived
thanks
I am sorry, but that is not possible. It does not matter that you have your strings split between multiple named resource files -- Android combines them all when it compiles your project.
You are welcome to use prefixes or something to identify one set of strings from another. I do that with the support code for the Android Parcel Project, to allow reusable components to each define strings without one overwriting the strings of another.