I need a place where I can store basic application configuration(URIs, DB-name, external storage folder-names, etc) in XML or any other decent format.
Is it /assets folder ? If so - give a basic example of using it in conjunction with XML files.
Thanks.
Yes there is: Resources folder: res/values/strings.
There you can place all the strings you want.
To use locale specific strings create multiple value folders ending with the locale suffix.
values folder (default):
<string name="app_name">My android app name</string>
<string name="email">myemail#gmail.com</string>
<string name="contact_me">Contact me</string>
values-PT folder (portuguese strings):
<string name="app_name">My android app name in PT</string>
<string name="email">myemail#gmail.com</string>
<string name="contact_me">pt string for contact me</string>
then just use them as: yourPackage.R.string.email on your classes.
Related
I would like to provide my own translations for my new Android app without using the Translation Service in Google Play Console. I have already provided translations for the app descriptions but this would be for the buttons and strings in the app.
I started to upload the xml files into the Translation Service, but then decided I don't want to pay for something that I can already translate myself. What is the best way to do this?
For example, English:
<resources>
<string name="SaveSetting">Save setting</string>
<string name="ViewSettings">View settings</string>
<string name="SavedSettings">Saved settings</string>
<string name="settingHint">Enter the setting name</string>
</resources>
Portuguese:
<resources>
<string name="SaveSetting">Salvar configuração</string>
<string name="ViewSettings">Ver configurações</string>
<string name="SavedSettings">Configurações salvadas</string>
<string name="settingHint">Nome da configuração</string>
</resources>
You create multiple string files, one for each language and the phone will automatically use the one based on the OS settings
see here: https://developer.android.com/guide/topics/resources/localization
In your case you would put your default strings in the res/values/strings.xml and the Portuguese ones in a res/values-pt/strings.xml
If you are using Android Studio, you can Open the translations editor and then add the translations for your app's strings on any language.
For more details check: https://developer.android.com/studio/write/translations-editor
Try using this tree in your project with correct folders and strings.xml suffix:
MyProject/
res/
values/
strings.xml
values-b+es/
strings.xml
mipmap/
country_flag.png
mipmap-b+es+ES/
country_flag.p
"es" for Spain, "us" USA...
Take a look:
https://developer.android.com/training/basics/supporting-devices/languages
I use Lint to check for missing translations in strings.xml (in values-de, values-en, values-hu, etc.). However, I noticed that if there are no strings in another language in other strings.xml files, Lint does not find the missing translations.
Example:
values/strings.xml:
<string name="abc">test</string>
values-en/strings.xml:
<!--empty-->
-> not work
values/strings.xml:
<string name="dummy">dummy</string>
<string name="abc">test</string>
values-en/strings.xml:
<string name="dummy">dummy</string>
-> works
Is there any way to force Lint to control that translation for string called "abc" is missing without adding dummy string? Unfortunately, even empty strings.xml files are not enough.
If you want to have a string only in strings.xml try that.
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<string translatable="false" name="abc">test</string>
</resources>
In my Android app I'm using strings.xml for all texts. I have many situations where I use almost the same string,
e.g. "Name" and "Name:" - translation is the same only additional colon is difference.
Is there any other way to have these two string except creating two string items like this:
<string name="name">Name</string>
<string name="name2">Name:</string>
There is no way you can concatenate strings in the strings.xml file.
All you can do is specify the format,
<string name="name">Name</string>
<string name="string_with_colon">%s:</string>
Then pass the name programatically,
String.format(getString(R.string.string_with_colon), getString(R.string.name));
Yes, you can do so without writing any Java/Kotlin code, only XML by using this small library I created which does so at buildtime: https://github.com/LikeTheSalad/android-stem
Usage
Based on your example, you'd have to set your strings like this:
<string name="name">Name</string>
<string name="name2">${name}:</string>
And then after building your project, you'll get:
<!-- Auto generated during compilation -->
<string name="name2">Name:</string>
How do I determine the language setting of an Android device. I am doing this to internationalize my app. Thanks!
You have to use different string.xml files
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Refer below link
Supporting Different Languages
And also Read Localization
The most common to do this is to keep a string file with the strings you want to localize in the res/values folder. You can then create additional values folders marked with resource qualifiers that will contain localized string files.
For example, if you want to translate to german, you create the res/values-de folder and put your localized string file in the folder. Android will load this folder automatically if the language is set to german. If the current system language has no values folder, it will use the default res/values folder.
There is a very simple method: right click on the strings.xml file and select Open translation editor or click on open editor.
You will see the different entries appear in the strings.xml file. By clicking on the icon representing the Earth (Add local), you can add a new language and enter the translation of the entries as desired.
When this is done, you should see a new strings.xml file appear in the tree window, accompanied by the flag of the chosen language and the extension of this language in parentheses.
You just have to compile and test the application on your emulator, by testing different language choices on it.
Create values folder for different language and put this language String.
example : values-ca and put Canada language String in this folder.
So, if user change the phone language then it is show this country language.
Thanks.
You have to use different string.xml files
res/
values/
strings.xml
values-hi/
strings.xml
<resources>
<string name="welcome">स्वागतम</string>
<string name="email">ईमेल पता</string>
<string name="password">पासवर्ड</string>
<string name="login">लॉगिन</string>
<string name="signup">खाता नहीं है? साइन अप करें</string>
values-gu/strings.xml
<resources>
<string name="welcome">પધારો </string>
<string name="email">ઈમૈલ નું નામ</string>
<string name="password">પાસવોર્ડ</string>
<string name="login">લોગીન</string>
<string name="signup">એકાઉન્ટ નથી ? સાઈન ઉપ કરો</string>
values-ja/strings.xml
<resources>
<string name="welcome">歓迎</string>
<string name="email">電子メールアドレス</string>
<string name="password">パスワード</string>
<string name="login">ログイン</string>
<string name="signup">アカウントをお持ちでない場合は?サインアップ</string>
values-ja/strings.xml
<string name="welcome">Willkommen!</string>
<string name="email">Email Addresse</string>
<string name="password">passowrd</string>
<string name="login">Login</string>
<string name="signup">müssen nicht angemeldet? Anmeldung</string>
Do you really need this to do manually? Actually in android are already built-in internationalization mechanizms - resource qualifiers.
All you need to do is add specific strings.xml for each locale.
http://developer.android.com/guide/topics/resources/localization.html
Android allows to create aliases of resource strings like:
<resources>
<string name="foo">somestring</string>
<string name="bar">#string/foo</string>
</resources>
by which the resource "bar" becomes an alias for the resource named "foo".
What I would for my app is a possibility to combine an existing resource prefix with different suffixes, i.e. to extend it like:
<resources>
<string name="foo">foo</string>
<string name="bar">#string/foo+bar</string>
<string name="else">#string/foo+else</string>
</resources>
where the resource "bar" would yield the string "foobar". Its clear that '+' doesn't work here but is there some other option to achieve such a string concatenation, so that one could define a bunch of string resources that have a common prefix?
I realize of course that I could do such resource string concatenation at runtime but defining them statically in the resources would seem so much more elegant and simpler.
Michael
No, it's not possible.
You can just use <string name="bar">This is my %s</string>
and then use String.format() in your app to fill in the variable with for example
getResources().getString(R.string.foo);
I know it's late, but anyways now you can do so by using this library I've created: https://github.com/LikeTheSalad/android-stem
It will concat all of the strings you want to at build time and will generate new strings that you can use anywhere in your project as with any other manually added string.
For your case, you'd have to define your strings like so:
<resources>
<string name="foo">foo</string>
<string name="bar">${foo} bar</string>
<string name="else">${foo} else</string>
</resources>
And then, after building your project, you'll get:
<!-- Auto generated during compilation -->
<resources>
<string name="bar">foo bar</string>
<string name="else">foo else</string>
</resources>
These auto generated strings will keep themselves updated for any changes that you make to your templates and values. They also work with localized and flavor strings. More info on the repo's page.