Android Studio error message I18n - android

I got an error message that says:
[I18N] Hardcoded string "Today - Sunny - 78/67", should use #string
resource.
How can this error be resolved?
The line of code is: android:text="Today - Sunny - 78/67"

i18n is an abbreviation for internationalization. Android Studio is just giving you a hint that the way you are setting the text won't allow you to easily translate it.
Instead you should put your string in a strings.xml file within the res/values folder:
<resources>
<string name="my_string">Today - Sunny - 78/67</string>
</resources>
You then reference that string on your TextView like this:
android:text="#string/my_string"
This would allow you to make another file for spanish strings, res/values-es/strings.xml that you could add a translation to:
<resources>
<string name="my_string">Hoy - Soleado - 78/67</string>
</resources>
And then that string will automatically get picked for your TextView if the user's locale language is Spanish.

It's implying that you should not explicitly write your text in your XML. You should use the /Values/Strings.xml to hold your string values.
Take a look through the folders on the left side, for values. The file is already there. You just need to add your string to it.
For the record, this doesn't normally generate an error of the "I'm gonna crash now" variety. It's usually just a warning.

you can put mouse on problems line and use android studio hot key - alt+enter. After that you can choose "Extract string resource"
Sory fo my bad english =)

Related

Why my android studio doesn't support typing tamil language?

I am new to android studio. I am trying to create an application with localisation(Tamil). When I paste my Tamil language into android editor the font is not appearing as it should. Here is the Screen shot of my android studio
Already surfed lot here. There is no answer for this issue. It would be great if anyone help on this.
When i tried to generate that same condition I came to know that you haven't added that string named "app" in your default locale i.e. in strings.xml and you are directly trying to add a translation for it.
When u hover to that error it states:
app is transalted here but not found in default locale.
First add(create) that string in your strings.xml file and then try to add it in your strings.xml (tamil) file.
Follow these steps:
First go to your project's res folder --> then open values folder --> then open "strings.xml" file and remove all text inside it and paste the text shown below:
<resources>
<string name="app_name">My Application</string>
<string name="tamil_app_name">ஆங்கில தட்டச்சு வழியாக நம் மொழி</string>
</resources>
Because you have to create a Tamil language string file to set your language. please do the following steps-:
1) Firstly declare all the strings in main strings.xml
2) Then create the string file according to your language choice, Example strings.xml(ta)
3) (ta) is the language code accordingly.
4) The Strings that you have in your main string file convert them into particular language which you want then paste it into the new created String file.

Why does Android Studio give me warnings to add code that Android Studio already added automatically?

As soon as I drag/drop a button from Palette into the XML file, Android Studio gives me the following error when I click on "Show Warnings and Errors":
Message: [I18N] Hardcoded string "Button", should use `#string` resource Suggested Fixes:
- Extract string resource
- Suppress: Add tools:ignore="HardcodedText" attribute
Inside the Button code in Text the following Android Studios enters itself automatically when I dragged/dropped button into XML file:
android:text="Button"
Do I just always have to click "Suppress" choice because I don't know what they want me to change for "Extract String Resource" choice. Or is it asking me to change Android Studio's code that it sets itself automatically?
You can do this instead of suppressing:
Add a String resource in res/values/strings.xml:
<string name="button_label">Button</string>
Set the above resource as text for your Button:
android:text="#string/button_label"
It is not an Error, this is just Warning that say you should use string resources, but not hardcoded strings in your code. But you can use hadrdcoded Strings and all will work fine.
Using String resources simplifies your life. For example when you want to localize your app. See screenshot.
using string resources
You can set string res. to TextView.Text, then create few android resource directories with names values, values-ru, values-uk etc.
In this direcroties you should create file strings.xml for each.
and Override your string like
<string name="message"> Message </string> in values
<string name="message"> Сообщение </string> in values-ru etc.
Now if you change your device language, from en to ru , in your app in textView will be written "Сообщение"
It is also useful in several other cases.
Message: [I18N] Hardcoded string "Button", should use #string
resource
Suggested Fixes:
Extract string resource
You should always pick string from string.xml in res/values/strings.xml. It's not the right way to hard code the strings, putting it into the string.xml file helps for localization in future.
Add tools:ignore="HardcodedText" attribute
When you use tools, it doesn't actually set the text to view, but it just shows you in preview window as how it visually appears, when you run the app, you won't the text added in tools.

Compare Android String xml files

I have two string xml for two different languages, I would like to know the different between those xml files.
For example, there is one xml for English,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Keep Accounts</string>
<string name="insertNewOne">Insert Accounts</string>
<string name="browseRecord">Browse Records</string>
<string name="set">Setting</string>
</resources>
And another xml for other language,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Example</string>
<string name="insertNewOne">Example</string>
<string name="browseRecord">Example</string>
<string name="dateNoColon">Example</string>
</resources>
We can see the difference is xml for English has element string name="set", and the other has not. On the other hand, the xml file for other language has element string name="dateNoColon" but the xml for English has not.
In this case, I would like to know the English xml lacks the element string name="dateNoColon", and other xml lacks the element string name="set".
Android Studio has translations editor starting of 0.8.12 version. You can find there missing translation strings.
You can enable check for missing translations in Lint tool. There are "Missing translation" and "Extra translation" checks.
Extra translation If a string appears in a specific language translation file, but there is no corresponding string in the default locale, then this string is probably unused. (It's technically possible that your application is only intended to run in a specific locale, but it's still a good idea to provide a fallback.).
Incomplete translation If an application has more than one locale, then all the strings declared in one language should also be translated in all other languages.
Suppose if the device is set to Other language, Android will look for title in the otherlanguage.xml file in value folder. But if no such string is included in that file, Android will fall back to the default, and will load title in English from the english.xml file.
For more detail go to http://developer.android.com/guide/topics/resources/localization.html#using-framework
I wrote a small tool for that: resdiff.
Check it out! https://github.com/danijoo/resdiff
Try sorting both files using some perl or bash script or something like that for example, using bash:
sort temp.txt -o temp.txt
and then look at the diff for example using DiffMergeit.
Use Android Lint to find both incomplete translations i.e. strings missing in a language variant and extra translations i.e. strings introduced in a language variant but missing in the default locale.
In Android Studio you can run Lint (and some other analysis tools) with Analyze -> Inspect Code.

Internationalization of string.xml issue

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

Strings defined in strings.xml not displayed in "Resource chooser" in graphical layout (main.xml)

Using this tutorial at section 5.5, I attempted to "Assign the 'celsius' string attribute to the 'text' property of the first radio button and "fahrenheit" to the second"
The "Resource Chooser" (called, by mistake (?), "Reference chooser" in the above article) is displayed but...I can choose only between two strings : "app_name" and "hello". "celsius" and "fahrenheit" attributes are not proposed.
How do you solve this issue?
In the res/values folder should be an XML file called strings.xml When you open this you will see the values you referred to ("app_name" and "hello"). Make an entry similar to them like this:
<string name="celsius">Celsius</string>
You could just hard code the text into the text property you are referring to but Android provides this way to store strings of text in an XML file so that they can be 1) easily found in one spot and 2) easily translated if required later. So it is recommended to do it "the Android Way." This is explained in section 5.3 of the tutorial you linked to.
Now you should be able to reference them by the element name (celsius) as you were trying to.
The strings.xml file has to be SAVED once modified in order to have its modifications taken into account !

Categories

Resources