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.
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 got this code from Settings app...
<string name="about_settings" product="tablet">About tablet</string>
<string name="about_settings" product="default">About phone</string>
then my questions are:
from where at runtime the system load the correct string resource ?
What must I do to add a new product? e.g.
<string name="about_settings" product="laptop">About laptop</string>
from where at runtime the system load the correct string resource ?
The system does not load this at runtime. The correct string resource is preloaded according to PRODUCT_CHARACTERISTICS defined for a specific target build. So you cannot use this while building from eclipse. This is used only for building apps preloaded on platform.
2 . What must I do to add a new product? e.g.
You need to add to PRODUCT_CHARACTERISTICS in device.mk file
Answer for your question 1.
You can get them via
String mystring = getResources().getString(R.string.mystring);
But your strings.xml should something like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="tablet" >About tablet</string>
<string name="default" >About phone</string>
</resources>
Answer for your question 2.
How will that work, because that is a resource file. If you want you can either create a file and save the information in it. Locally. But it will be only on that device.
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.
In my application I do have several different string resources each per locale like:
res/values/string.xml //default
res/values-en/string.xml //english
res/values-it/string.xml //italian
Now the problem - each of files contains hundreds of keys and from time to time I can't really define which language is lacking some keys. Say:
<string name="yes">Yes</string> <!-- Default -->
<string name="yes">Yes</string> <!-- English -->
<string name="yes">Si</string> <!-- Italian -->
And if in German string.xml there'll be no "yes" key corresponding value will be default "Yes" instead of German "Ja" - which is disaster.
Help me to find a way to define lacking string resource keys.
You can also try MOTODEV Studio. You can use it a standalone IDE (based on Eclipse) or as an Eclipse Plugin. What you would like is an editor, which includes, that makes really easy working with localizable strings. It will show you in a same view all the files as columns, so you will not need to do any merge or diff whatsoever.
Use http://winmerge.org/
You can compare files easily.
copy all the files to one text file,, Sort the file and then check one key at once,, loooong method but the most effective one
How to implement Android system l10n ?It has been l10n in German.What is different between Android and Linux in realizing system localization?
What is Operational process of implementing Android l10n ?
What is needed to implement Android system localization? such as Unicode UTF8, charset,other anything else?
Are you asking about internationalization/localization? If so there's a pretty extensive writeup in the docs.
Localization in Android is a native function, what you have to understand is how to "tell android" where to pick the words translated based on the Language that is set on the device that is running your application.
1. When developing an application for Android avoid "hardcoding" the string values and always use the strings.xml file located in the res/values folder. In that file enter every string used in your application using the tag:
<string name="app_title">Super App</string>
2. From the java side use this string resources from anywhere with the method getString(), this method receives as parameter the id of the item you want to get:
getString(R.string.app_title)
3. Once you have defined every string your app will use, just copy the strings.xml file and paste it in a new folder at the same level of the res/values folder but name it according to the new language you want to add (Read this)
4. Finally, translate every string in each folder to the proper language but keeping the same ids of every string, just changing its content:
res/values-EN/strings.xml
<string name="app_title">Best Application Ever!</string>
res/values-ES/strings.xml
<string name="app_title">La Mejor Aplicación!</string>
res/values-FR/strings.xml
<string name="app_title">Meilleure Application Jamais!</string>