What's "msgid" and "xliff" in strings.xml file? - android

Background
Sometimes I see some weird attributes on the "strings.xml" file made by Google's samples, for example, on the chips example (code available here), I can find this strings file of "res/values-en-rGB" (for English-Britain) :
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="more_string" msgid="8495478259330621990">"+<xliff:g id="COUNT">%1$s</xliff:g>"</string>
<string name="copy_email" msgid="7869435992461603532">"Copy email address"</string>
<string name="copy_number" msgid="530057841276106843">"Copy phone number"</string>
<string name="done" msgid="2356320650733788862">"Return"</string>
</resources>
I think both are used only for localized strings, as I never saw them inside "res/values" folder.
The question
What do those attributes mean?
What does the value of "xliff" mean?
When should you use them and what should you put there?
Are they even needed?
Is there any documentation about those things?

On Android Developers Localise your app page, search for xliff in the section named "Mark message parts that should not be translated".
The explanation is as follows:
Often strings contain contain text that should not be translated into
other languages. Common examples might be a piece of code, a
placeholder for a value, a special symbol, or a name. As you prepare
your strings for translation, look for and mark text that should
remain as-is, without translation, so that the translator doesn't
change it.
To mark text that should not be translated, use an
placeholder tag.
The suggestion is that text within the <xliff:g></xliff:g> tags should not be translated. These tags can also provide metadata about the non-translated text.
When you declare a placeholder tag, always add an id attribute that
explains what the placeholder is for. If your apps later replace the
placeholder value, be sure to provide an example attribute to clarify
the expected use.
For more information on the actual xliff tool, rather than how it relates to Android strings, check out the related question:
What does this mean "xmlns:xliff"? XML.

Related

How to escape HTML attributes in strings.xml?

I'm supplying XML strings files to Android developers and confused about escaping rules when using HTML styles.
The documentation here only talks about embedding <b>, <i> and <u> tags without escaping, but it also shows that you can use Html.fromHtml with any string of HTML.
So how should a chunk of HTML like <font color="red">"Quote"</font> appear in the strings.xml file?
As font tags can have attributes and quotes should be escaped, how should attribute quoting be handled?
The following is invalid XML, so clearly this is wrong:
<string name="eg_1"><font color=\"red\">\"Quote\"</font></string>
Should just the XML attributes be left unescaped?
<string name="eg_2"><font color="red">\"Quote\"</font></string>
It's valid XML, but seems wrong to have different escaping rules in the string.
Perhaps CDATA should be used to simply protect the entire string?
<string name="eg_3"><![CDATA[<font color=\"red\">\"Quote\"</font>]]></string>
Or even just escape it?
<string name="eg_4"><font color=\"red\">\"Quote\"</font></string>
So how should a chunk of HTML like "Quote" appear in the strings.xml file?
Use CDATA, per your third sample above.
The first example is an invalid XML (as you said).
The second example is an invalid strings.xml file.
The third example should be like this:
<string name="eg_3"><![CDATA[<font color="red">"Quote"</font>]]></string>
The fourth example is fine.

Text in android app (beginner...)

My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.

Find which resources are filtered

From the image above in a working android project. I would like a way to get the filters so I can compare them to something else I'm working on. For example I'd like to have an array that contains "mcc311,mnc490","mcc311,870".....,"mcc312,mnc530" etc. So I'm looking for a way to get the filters that the project actually uses.
Figured out a way to do this and it was pretty simple. In values-mcc310-mnc120 for example, i would put in a xml file defining a variable signifying what mcc and mnc identification im using.
For example, in values-mcc311-mnc870 i would have a xml file called networkcode.xml with contents of:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="network_id">MCC311/MNC870</string>
</resources>
this way i can read in that value of network_id at run time and know for certain what filter android is using.

How to make XML strings bold, underlined etc?

http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext

Problems with res folder and R.java

I'm doing Tutorials and I'm on section about images. It says to put them into the folder res/drawable. But I don't have that folder, I have three instead: res/drawable-hdpi, res/drawable-ldpi and res/drawable-mdpi. So whats the difference between them?
Im using this tutorial.
One of the steps is:
Create a strings.xml file in
res/values/ and edit the file to look
like
There already is strings.xml, combined with the above, telling me to use res/drawable, are these tutorials out of date?
This tutorial has code like:
R.id.spinner
R.array.planets_array
R.layout is just simple enum. Uses the main.xml in the layout folder. But where are R.id and R.array to come from. Because it is coming up in eclipse saying it doesn't know what they are. R.java gets updated automatically, so can someone tell me from reading that tutorial where id gets added to R? It says that
The R.array.planets_array ID
references the string-array defined
above
Only it doesn't work. I doubt it makes a difference that i didn't make strings.xml since it's the same filename in the same location. But since R.java is meant to be updated automatically I don't know how to fix this.
Those are for the different screen resolutions for the range of devices that are out there. Read about supporting multiple screens on the Android dev site.
Just so you know where the R stuff comes from.
The R.java file is a generated file which contains some kind of pointers to a resource in your application. It is a simple integer actually which uniquely identifies the resource in the internal resource management system of Android.
R.string identifiers are generated from resources XML files like this one for example.
<resources>
<string name="test">This is a test string.</string>
</resources>
R.array identifiers from string array XML files.
<resources>
<string-array name="days_of_week">
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Sunday</item>
</string-array>
</resources>
You can access that array using its identifier R.id.days_of_week now.
R.id identifiers are a bit special.
They are generated in two ways. The first one is when you define a View in your XML layout file using the #+id/... syntax. Note the + sign.
The other way is to define them in resource XML files like strings for example.
<resources>
<item type="id" name="first" />
<item type="id" name="second" />
</resources>
You'd then just use them in a layout XML file like this #id/first. Note that there is no + sign anymore as you reference it, before you were declaring it.
In code you then use it like this, R.id.first.
There are a lot of other resources. I'd like to point you to the Application Resources article and also make sure to checkout the Resource Types sub-article.
If you don't have the folder, just create it. It is basically the fallback for the case that you don't have a resource in a more specific folder like res/drawable-hdpi
The *-xx folders allow you to provide more specific drawables (images) for various screen resolutions.
The same principle applies to values/ and values-xx/ where xx is a country code ; the xx versions allow you to have translations for UI messages.

Categories

Resources