Android Strings - android

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?

All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.

In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

Related

How to import/export Android string resource to Excel for localization?

I use Android Studio in app development. I want to translate strings by exporting/importing the Android language resources (strings.xml) to Excel file (xlsx). What is the best way to do it?
If anyone else needed the answer,
from res -> strings -> right click-> Open Translations Editor. Select data/variable you need then copy and paste data from Translations Editor to excel . done.
Since CTRL+A not working now in the android studio.
There is one way to convert the android strings file to CSV and then translate it with the help of google translator and then again convert back to XML.
https://www.skydevelopers.net/blog/2-best-ways-to-translate-the-android-strings-file/
here is a blog in detail
Export Strings resource file to csv
Get its content translated(probably from google translate)
convert back the Translated file to Strings.xml(android string resource file)
I used http://convertcsv.com/csv-to-xml.htm this website for converting csv file to strings resource file
need to mention Custom output template to convert it to strings resource file
<string name="{f1}">{f2}</string>
put this in template section provided
website also displays the desired converted output file
As many others pointed out, pressing Ctrl+A in the Translations Editor doesn't work since Android Studio 3.2
I work for a company that outsources translations constantly, so we need to convert android strings to and from xls files.
The only solution that worked for us reliably is this fork of the older android-lang-tool. Just build with maven and run the jar.
It exports strings, string-arrays, plurals and their key-values to an xls file. It even exports the comments.
I would suggest the best tool for android app string localization is the Translations Editor that is inbuilt into Android Studio.
The reason this is a great approach is you are able to make the process both easier for translators and less prone to errors. The XML string files in Android Studio support XLIFF notations that are a standardized method to aid string localization.
By utilizing XLIFF notation in your XML string files you can do the following to help the translators:
Provide additional context for declared strings
Mark message parts that should not be translated
To use XLIFF in your Android string XML files you need to include the XLIFF 1.2 namespace:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Here are a few examples of strings from the android localization documentation:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Example placeholder for a special unicode symbol -->
<string name="star_rating">Check out our 5
<xliff:g id="star">\u2605</xliff:g>
</string>
<!-- Example placeholder for a for a URL -->
<string name="app_homeurl">
Visit us at <xliff:g id="application_homepage">http://my/app/home.html</xliff:g>
</string>
<!-- Example placeholder for a name -->
<string name="prod_name">
Learn more at <xliff:g id="prod_gamegroup">Game Group</xliff:g>
</string>
<!-- Example placeholder for a literal -->
<string name="promo_message">
Please use the "<xliff:g id="promotion_code">ABCDEFG</xliff:g>” to get a discount.
</string>
</resources>
To access the Translations Editor in Android Studio, select Open Translations Editor from the context menu for your XML string file (ie. strings.xml) in your project tree (see below).
Convert your strings.xml to csv xml-to-csv
Import to Google Sheets
Translate using the formula =GOOGLETRANSLATE(B2, "auto", "de")
Generate output in another column using =CONCATENATE("<string name=",char(34),A2,char(34),">",C2,"</string>") where A2 is the resource_ID and C2 is the translated string
Copy the whole output column and paste inside the <resource>...</resource> tag
As Saad Mahmud answered, you can copy from the translation editor (ctrl+a ctrl+c) and then paste into a spreadsheet.
You can copy it back from a spreadsheet to the translation editor by only copying the "default value" and other languages columns, click on the topmost default value and paste (ctrl+v).
It also works with subsets (both subsets of rows and columns), as long as they are next to each other.
Be aware that empty cells in the spreadsheet will not blank out the translation in the editor, it will leave the current untouched.
Also be careful that you haven't added or removed any translation keys since the spreadsheet was created...
Export or copy to excel only supported in Windows PC.
Still not yet in MAC
As many others pointed out, you can't simply copy and paste translations from and into Translations Editor since Android Studio 3.2.
The simplest solution I found was saving the Excel file with translations as CSV file and then converting it to XML with regex and vice versa.
To "import" translations the steps:
Save xls/xlsx file with key in first column and translation in second column as CSV file (If you have file with non-ANSI caracters use Google sheets, because Excel doesn't support saving in CSV using utf-8)
Open csv file in text editor which supports "find and replace" with regex (eg. Notepad++)
Open "find and replace" and set regex search
Search ^([^,]*),(.*)$ and replace it with <string name="$1">$2</string>
Copy file to string resources file between tag
Fix possible mistakes
You can use similar method in reverse for "export". Use <string.+name="(.*)".*>(.*)</string> for finding and $1,$2 for replacing. But it only works if every string tag in in one line.
NOTE: If your res folder doesn't contians strings.xml then Android Studio won't show "Open Editor" in top right corner of the strings.xml file(Open the file). In my case all my string res files are named like strings_feature.xml
To copy/paste from Translations Editor use Android Studio 3.2 Version and below. It allows copy/paste of full column.

Can I include a file of strings that would be autocompleting and crossplatform (iOS/Android)?

I'm not even sure what the vocabulary for this question is, but I'd like to have a file which is a list of strings which could be included as constants in Android and iOS.
I'm trying to find better vocab to describe this issue so comments are greatly appreciated too, thanks all.
Edit: For example, I'd like to have a file such as
color_names.txt
COLOR_NAME_BLUE "blue"
COLOR_NAME_RED "red"
COLOR_NAME_GREEN "green"
Which I can include in both an Android and an iOS project I have, in a way that in the code COLOR_NAME_BLUE is symbol checked, and if someone were to type COLOR_NAME_BLEU it would throw a compile error.
The actual file will be much larger and is something I want to be maintainable. I could put this in JSON but then I'd have to do the checking at run time, which isn't terrible I just am trying to figure out if there is a better way.
We also have iOS and Android apps that should be sharing strings.
You should use a python program (or some inferior scripting system) that takes your input file (checking it for errors) and outputs a Localizable.strings file for the iOS and strings.xml file for the Android.
So long as you have a good handle on your directory structure, you should be able to place both the Localizable.strings file and strings.xml file right where they need to be for your build.
For example, for a label pair like this:
PRIMARY_AGE_10 "Primary Age 10"
The label/string matchup is pretty obvious for the Android strings.xml:
<string name="PRIMARY_AGE_10">Primary Age 10</string>
The iOS Localizable.strings format is like this:
"PRIMARY_AGE_10" = "Primary Age 10";
Then when I want to use the label "Primary Age 10" instead of using an NSString, or #"Primary Age 10" i just make a call like this:
NSLocalizedString(#"PRIMARY_AGE_10", nil)
One other big advantage is if you need to localize, you can generate multiple Localizable.strings files and strings.xml files.

Copy string to strings.xml

Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/

Can localization resources be downloaded in runtime?

I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?
You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml
Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.
Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.

Keep Strings into .txt file or put them into a Database?

Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.

Categories

Resources