Can one combine android resource strings into new strings? - android

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.

Related

How do I submit my own translations for Android app buttons and strings?

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

Lint does not check missing translations

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>

Android strings xml similar texts

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 to include a Android values strings.xml file into another strings.xml

I need to put all codes inside values-zh/strings.xml into values-zh-rCN/strings.xml. How do i do this?
I've tried this <include layout="#values-zh/strings" /> but did not work, Intelij is telling me to declare the file first.
Assuming values-zh/strings.xml is your base string resources, you can put all your strings there, and only provide values for those you need to override in values-zh-rCN/strings.xml.
In values-zh/strings.xml:
<string name="title">default title</string>
<string name="content">default content</string>
In values-zh-rCN/strings.xml:
<string name="title">simplified title</string>
Now when you are on simplified Chinese config, content will be 'default content', and title will be 'simplified title'.

Is concatenation of resource strings/string concatenation, possible in layout file?

I have resource strings :
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
In my application, at few places I am using these strings alone and at few places I am succeeding them by a colon.
I don't what to create another four resource strings :
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
Now to achieve this, I have to concatenate my resource strings with colon. I know this is very easy though java code which I can write in my activity class. But what I want is to do the concatenation in my layout file.
Question : Is string concatenation possible in layout file?
This is where I have to perform the concatenation:
android:text="concatenation_if_possible"
Using XML entities it's possible to use the same string multiple places within an XML file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY appname "MrQuery">
<!ENTITY author "Oded">
]>
<resources>
<string name="app_name">&appname;</string>
<string name="description">The &appname; app was created by &author;</string>
</resources>
I used this answer: dynamic String using String.xml?
Question : Is string concatenation possible in layout file?
Nope as far as I know.
One solution is to use what #DIVA has answered before.
Another possible solution is to create a custom view that extends TextView (or the view you want to achieve this) and create a custom attribute custom:concatenate which receives a string reference and perform the concatenation automatically. IMHO I think this is the most clean approach.
In code will look as this:
<com.whatever.ConcatenateTextView
android:text="#string/whatever"
custom:concatenate="#string/second_string"/>
Or… you can use the power of Drawables creating a custom TextDrawable (which is explained very well by #Devunwired in this post and the concrete implementation of it in Github).
Copying what #Devunwired has said in his post about it:
With this class, text can now be part of the Drawable world, meaning it can not only be set alone in places where you would normally put an image, it can also be placed together with other Drawables in containers like StateListDrawable or animated with the likes of TransitionDrawable and ClipDrawable. In many cases, we can use this to do a job that would otherwise require multiple views or compound controls just to achieve a given visual effect; thus it can reduce overhead in your view hierarchy.
This combined with your custom TextView as I explained before (or whatever view you want to use) gives you a very powerful option. Again copying the example that #Devunwired wrote in his post:
ImageView mImageOne;
TextDrawable d = new TextDrawable(this);
d.setText("SAMPLE TEXT\nLINE TWO");
d.setTextAlign(Layout.Alignment.ALIGN_CENTER);
mImageOne.setImageDrawable(d);
If you need more help please let me know in the comments and I'll gladly update the answer!
You can so something like this :
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>
String strMeatMsg = String.format(strMeatFormat, ":");
textview.setText(strMeatMsg);
No, you cannot concatenate several string resources into a single string when directly referencing those strings from a layout file.
XML layout files are simply a template of instructions for Android to build a user interface, and you should consider them as a cleaner and more organized way to generate your UI than using a Java class that manually creates and positions views in a layout. That being said, there are limitations to what you can do with a layout file, and one of them is being able to reference a single string resource from every view, meaning that you can't do anything more complex than that, including concatenating several strings into one.
You can do so using this plugin I've created: https://github.com/LikeTheSalad/android-stem It will concat all of the strings you'd like to and will generate at build time a final XML string that you can reference anywhere as with any other manually string you've added.
For your case, you can do the following:
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
<string name="address_with_colon">${address}: </string>
<string name="city_with_colon">${city}: </string>
<string name="country_with_colon">${country}: </string>
<string name="pincode_with_colon">${pincode}: </string>
And then the tool will generate:
<!-- Auto generated during compilation -->
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
And anytime you decide to change either your template or values strings, the plugin will keep the generated strings updated. More info on the repo's page.

Categories

Resources