Parse an xml file depending on the current language - android

Is there a way to parse an xml file depending on the current language? For example, I have two countries.xml files. One in the values folder, and another one in the values-de folder. I want to parse the file specific to the current language.

The android environment parses these files for you, and you can get to the data through various accessors, like if you have a set of strings in your strings.xml, you use
getText(R.strings.my_string);
which will give you the string from the strings.xml for that locale (or the default one if none match).
Check the developer's guide on resource localization for more info.

Related

How can I load the strings.xml dynamically? Can I put the file in a server?

Why i ask this is because the size of my APK is huge. I wanna make it smaller.
There're lots of strings in strings.xml. Our product manager force us to support all languages on the earth.
I'd like to know, can I just put some languages of strings.xml locally, put others in a server, then when user launches the app, downloads the strings.xml from it dynamically according to the language of the user.
I am not quite clear about the process of how android load the strings.xml file. Any idea about it?
Thanks~~
can I just put some languages of strings.xml locally, put others in a server, then when user launches the app, downloads the strings.xml from it dynamically according to the language of the user.
You cannot modify resources at runtime. You are welcome to download and process XML files at runtime, and those XML files might contain strings that you want to display to the user. However, you cannot use the Android resource system to pull in those strings. You would need to write your own Java code that uses those values, including determining which translation to use for a given device (taking into account the multiple-locale support offered in Android 7.0).
You can easily call some web-service on your application start and fetch the strings of desired/selected language in form of say JsonArray. Then parse that data to some data-model like ArrayList of string to manage in your app

How does Android handle unsupported languages?

I've made an app that runs in multiple languages, but if the user runs it in an unsupported language, will it display the main one?
If it is the case, how can I make it display in English instead?
You are right.
Just put your english strings on the strings.xml file, so it will be loaded by default if no other language matches the phone lang.
Hope this helps.
if the user runs it in a not supported language, will it display the main one?
The default language, yes See the docs for detailed information.
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml. If this default file is absent, or if it is missing a string that your application needs, then your application will not run and will show an error.
how can I make it to display English instead?
Also see the docs for that, you should use english in the default strings.xml
How to Create Default Resources
Put the application's default text in a file with the following location and name:
res/values/strings.xml (required directory)
The text strings in res/values/strings.xml should use the default language, which is the language that you expect most of your application's users to speak.
Depends really what language are you using in your string.xml (main one). If there is no language case for what the user selects in the Settings, the app will load in the default values from the strings.xml
Language which you want to load by default then put that strings.xml
in values
folder without any suffix with values folder
In fact when you run your app, the app will check if the locale is available thanks to the string.xml contains in the value-locale folder. If nothing is found the string.xml in values is used so just put english traduction in your string.xml.
I hope to be clear.

display contents in different languages

Currently, I have a txt file that is formatted (using delimiters) in a specific way. I read the file, parse it and populate the GUI of the app.
I need to make the multi-lingual app so basically display non English characters. I have 2 ideas in mind:
Write the file in non English and read it/parse it based on the mobile language.
In the English file, I change the actual contents to be reference to String ID. These ID are the ones in String.xml. When I read the file, I parse the ID and call the corresponding string. I will let Android figure out the system language and pick the appropriate String.xml
OR completely different solution that you recommend

Android Localization structure of values folder

I`m still learning android development and come with one app which I want to localize to multiple languages.
But please help me a little bit with values folder structure. I have main values folder and in it I have analytics.xml, strings.xml,styles.xml,mraid_attr.xml and colors.xml.
When I make new folder for localization, eg: values-fr I must put all of these files in it or just string.xml where I have translated strings?
You can just put your translated strings in your localization folder, but Lint will warn you about non-translated strings.
So if you have configuration strings (i.e no translation needed), create a separate XMl (like config.xml) and let only your translated strings on your localization folders.

android localization resource chooser

I have an android application on eclipse it's text-views and the buttons' texts on Arabic language , I want to localize my application to both Arabic and English users .
Firstly under res folder I have created another folder and named it values-en .
I let the original folder values as is it and later I will use it for Arabic .
My first question it is necessary to create another folder (values-ar) for Arabic localization issues.
Or original values folder is just enough?.
Secondly
On values-en folder I created strings.xml file and added the following statement to it
COMTAS
I set the text of one of the textviews from resource chooser so choose application-title .
after that the textview's text converted to #string/application-title.
the project gives me the following message
NOTE: This project contains Java compilation errors, which can cause rendering failures for custom views. Fix compilation problems first.
Can someone explains to me how to make localization in simple steps.
And how I can retrieve textviews' texts either in english or arabic (reading from values folder)?
First: Leaving a strings.xml in the "values" folder will cause this language to be "default" (or rather "fallback") language.
Second: The text is read automatically from the strings.xml placed in the values-folder matching your System Locale (the locale you get when you call Locale.getDefault()), e.g. if system locale is set to english, the values are taken from values-en folder. Therefore it would be wise to place the arabic values in a values-ar folder.
I can't say anything related to your compile errors as you didn't post the code causing the error.
It's always good to follow the documentation. Here is the link for Localizing with Resources.
Create a values-ar folder for the arabic language, and let the default language as english. Why?
Whenever the application runs in a locale for which you have not provided locale-specific text, Android will load the default strings from res/values/strings.xml.
So if your application is running on a device set to Spanish language, it would still load arabic language (if it's set as default).
English is a most talked language, which would be better to let english by default, ant let the arabic language in values-ar(in my humble opinion).
Hope this helps you.

Categories

Resources