Is there a way to tag a strings resource folder with more than one language(values-en-es)?
My problem is that for Hebrew on some devices the language code "iw" and on others it is "he".
My current solution is to make two folders with the same content and only change their name
respectively.
I wonder if there is a more accurate way to do it?
Resource folder names can have multiple qualifiers but only one qualifier per type:
For example
values-en-rGB //Language + Region
is valid but
values-en-fr//Language + Language
is not valid, since it has multiple values for a single qualifier. So
values-iw-he
is not possible.
Source: Android Developers, Qualifier Name Rules.
However this doesn't mean you have to duplicate the files. Instead, you can create an Alias Resource.
Android Developers explains Alias Resouces like this:
Creating Alias Resources: When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.
For example, a String resource in one folder
<string name="app_name">My Awesome App</string>
can be referenced in another String resource in another folder as:
<string name="application_name">#string/app_name</string>
More about alias-resources on Android Developers.
You can make a File Link in eclipse, as described here.
So you have your values-iw/strings.xml with the real values and you make a File Link to that file in your values-he folder. This has the benefit that you do not have to edit 2 files, the linked 'file' gets updated automatically.
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 a small confusion, when in drawable folder in android i try to copy same file it says that "Resource with this name already exists" which is absolutely correct. Again if try to copy an image with different .extension but with same file name it takes it. But in R.java file only 1 resource id is generated. What does this mean? Also ID will point to which resource?
It simply means that at the time of compilation it is only able to generate id from one of the available resource. Why can't it generate both id's if it has allowed to keep both resources in the same folder? Because for each resource, there is a static integer named after the name of file, excluding extension so naturally, there can't be more than one of these static integers in one file.
ID points to which resource? I think you can't be sure. I just checked by recreating the scenario (out of curiosity) and I found that it pointed to the resource I added in the end!
Still, it is irrelevant as to which resource it points to. Because it won't allow to run the app.
P.S: If you really want to use resources with same name (diff extension) then you can do so by using assets
I have two values directories - one for English and one default.
if the resource are located in any of those folders, i can use it.
But if there is no resource in default values directory and locale is other than English, it causes ResourceNotFoundException.
I understand why this happens and why Android was built this way (to prevent ambiguous resource usage in case there are more than one non-default resource files with this resource).
But is there any way to force Android use, for example, English resource bundle if resource couldn't be found?
The only option you have is to make sure the /res/values/strings.xml file contains every string in English but you must also have /res/values-no/strings.xml which has the Norwegian strings.
If /res/values-no/strings.xml doesn't have the resource it will drop back to /res/values/strings.xml. It's the only way to do it.
I am creating an app which utilizes images from local device folders (brought back from server) at runtime rather than from the app's own resources folders.
I would like to take advantage of Android's runtime ability to use the appropriate resource files for different display types/orientation, similar to its present runtime usage of different resource folders (ldpi, hdpi, sw600sp etc) but in my case, not actually with complete resource folder content present during the app build.
Which would be the preferred methodology to achieve this?
i) pulling back the server images files and creating a sub-directory structure recognized by Android in a similar fashion as it presently recognizes its res directory structure -- i.e. a runtime pseudo-resource folder if you like.
ii) overriding events to catch orientation changes on existing activities and manually determining display type at start-up, in order to point all the app's activities to appropriate (locally stored at runtime) image files.
I am not entirely sure how to achieve either of the above, hence any indication of preference and general approach would be appreciated.
You can use Android's resource identification mechanism for this too. You can create the different images on your server and you can store the links to this in your strings.xml
So you will have different strings.xml in different res/values-xx folders (where xx stands for the configuration qualifiers). Take a look at Providing Resources to know more about the configuration qualifiers.
so in values-ldpi, your strings.xml can have a value as:
<string name="icon">http://my-server/images/icon-ldpi.jpg</string>
whereas the strings.xml in values-hdpi will have the value as:
<string name="icon">http://my-server/images/icon-hdpi.jpg</string>
and so on.
Edit:
In case the url contains runtime parameters, you can specify the same in the strings.xml as:
<string name="icon">http://my-server/%1$s/images/icon-ldpi.jpg</string>
And you use the same in the Java code as follows:
String iconURL = getResources().getString(R.string.icon, siteId);
If the siteID is 001, depending on the device type, the imageURL would be "http://my-server/001/images/icon-ldpi.jpg" or "http://my-server/001/images/icon-mdpi.jpg" or "http://my-server/001/images/icon-hdpi.jpg" or ... And the fact is that you don't have to do anything in your code to determine the display attributes for this to work.
This has turned out to be easier than I thought. I can just return 'identifier' strings stored in assorted values-xxx folders in order to build an appropriate set of possible file names.
e.g.
In values\string.xml:
<resources>
<string name="app_name">Resource Tester</string>
<string name="action_settings">Settings</string>
<string name="myindicator_lang">en</string>
<string name="myindicator_dpi">normal</string>
<string name="myindicator_orient">port</string>
</resources>
In values-de\string.xml:
<resources> <string name="myindicator_lang">de</string> </resources>
In values-land\string.xml:
<resources> <string name="myindicator_orient">land</string> </resources>
In values-hdpi\string.xml:
<resources> <string name="myindicator_dpi">hdpi</string> </resources>
etc.
With this string info returned at runtime, I can just dynamically build the required file name based on device configuration, allowing me to store alternative server/local storage resources with an appropriate name and have my app utilise the most appropriate image file if it exists.
i.e. in server/local storage have:
mybanner-en.jpg
mybanner-de.jpg
mybanner-de-land.jpg
etc.
I can now search for an existing stored file which best matches the device config at the time.
Many thanks Rajesh, for pointing me in the right direction.
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.