Android localization: find only the strings that are used in the app - android

We are working on an App that has some history, and strings have been added to and removed from it a lot in the past. It resulted in the presence of lots of unused strings in the strings.xml file. As we pay for the localization to new languages, translating the redundant strings costs us money.
Is there a way to get the list of all the string keys that are definitely used somewhere in the project?

If you are using Android Studio then do this,
Menu -> Analyze -> Run Inspection by Name -> Type "Unused resources"
Hope it helps..!
Do mark as an answer so that it would be useful to others...

Related

Converting hard coded strings to string resources

So, i kind of created a monster. Built an android game for few years and used lots of hardcoded strings. Now i want to support different languages. My questions - is there an automatic way to create these resources?
for example tell Android studio for all the "hard coded" warnings apply extract string resource.
I aw aware of the inspect code that show you localization warnings, i am looking for a way to automate the fix
AFAIK, it's not possible to fix that automatically. You need to press Alt+Ctrl+Shift+i and type hardcoded strings. After completion of the finding process, manually create them in strings.xml.
I am attaching a link to site that will convert your string.xml to different language you want and then download translated string.xml in your stings folder, it will translate each string step by step, if this is what you want, hope it helps
https://asrt.gluege.boerde.de

android app/business-logic config

how does an app developer store app-specific (ie. my server addresses, path endpoints, etc.) global and build variant specific config values in an android app? further, is it possible to have overrides? ie. i want to set a default value in my global.config, but collisions in staging-build-variant.config and prod-build-variant.config should override this, while dev-build-variant.config would simply use the default value.
i've read about SharedPreferences but this seems for storing user input at runtime for later runs, and i've read about people using a class to hold constants, but that doesn't quite fit either as i don't get any benefit of a specific configuration values overriding common ones. there is also a lot of seemingly outdated articles out there which i'm not sure are accurate anymore.
i asked this as a comment in this question which seems to be close to what i'm looking for, but thought i'd ask as a question for more exposure.
EDIT: maybe i explained this poorly - to clarify, this comment.
Justin, it seems that what you whant is a library project. You will have to create a library project with one default configuration.
Let's say that project is called Core and then it has the following strings.xml:
<string name="server_address">htt://path.to.server</string>
This way you can create a module (called app1) for the project that uses the 'Core' library project. Then you will end with two strings.xml file.
So the strings.xml file from your app module will override the strings.xml from the library project. Then you can have:
<string name="server_address">htt://app1.path.to.server</string>
Resources:
Create a library Project
[UPDATED]
Instead of use a library you can try a grade variable. See this question:
Gradle Variables
And this link about build variants:
Build variants

Android: Get missing translations for strings-resources

In Android, you can specify the texts in the default locale in res/values/strings.xml. Additional translations can be added for new languages in res/values-it/strings.xml (for Italian for example). If a string is not translated, the fallback-default locale is used.
Currently I can not tell which strings I still need to translate (so are in values/strings.xml, but not in values-$/strings.xml for all $ in languages) and which are translated, although the are obsolte (so are in values-$/strings.xml, but not in values/strings.xml exists $ in languages)
I'm searching for a tool which gives me the translations which are missing and the one which are obsolete.
To be honest, it is not that difficult to write such a tool for the command-line, I can only hardly believe nobody has already done this.
This isn't automated, but it's very fast. In Eclipse, to go Window->Show View->Other->Android->Resource Explorer.
Now, under the Resource Explorer tab at the bottom (or wherever you've moved it to) look under String. Each string should have the same number of versions if you have a complete translation, so you can scan down the list in just a few seconds.
Do this for each project that has strings.
I didn't know about this until after I localized, but it's still useful (such as when I add a new string).
If you are using Android Studio, it is easy to find which string is missing.
Right click on values/strings.xml
and choose Open Translations Editor:
Where you can easily find missing strings in all languages as below :
Thank You...
The new official Android Lint tool helps you detect this problem, and many others: http://tools.android.com/tips/lint
Interesting question. I've wrote simple script to find duplicate resources in android project at https://gist.github.com/1133059. It is ugly, I know, but I'll rewrite it in a few days and maybe create a project on github.
To run it from console:
$scala DuplicatesFinder.scala /path/to/android/project
UPDATE:
I've made a project on github https://github.com/4e6/android-localization-helper, maybe someone find it helpful
There's a much improved version of the Android Dev Kit lint tool in Eclipse since SDK version 17 - see the docs here: New Eclipse Lint UI
Just click the "lint" tool bar item, run it on your project then open the "is not translated" item that will appear to show you every tag that needs translation.
Credit to satur9nine - this is an updated version of their answer which lead me to this one.
You could also use Android lint (easy access from Eclipse). Right click your project -> Android Tools -> Run Lint. This will give you a list of all missing translations, and also some other common errors.
It will also show you duplicate resources and strings not available in the default translation.
On Android Studio, Analyze > Run Inspection By Name, Type following and execute inspections for Custom scope Project Production Files.
Extra translation
Incomplete translation
I created a tool to solve precisely this problem. You can download the tool from https://github.com/vijtheveg/tea.
The tool can generate an Excel spreadsheet from the Android project, like the one shown below, with the source strings and their translations shown side-by-side.
Most importantly, the tool will output only those strings that were newly added or modified since the last translation!
You can send this Excel spreadsheet to your translator and once the translations are filled in, you can regenerate the string XML files for the target language from this Excel file.
Best of all, you can perform this process (add/delete/modify strings in the source language XML files) -> (generate Excel and send it for translation) -> (regenerate target language XML files) any number of times, and each time the tool will only output those strings that need translation into the Excel file. The tool will also delete strings that have been removed from the source language from the target language XML files.
More details on the tool's GitHub page above. I hope you find it useful.
I'm the Product Manager for MOTODEV Studio. As #hjw mentioned, this is a feature of MOTODEV Studio called the "Localization Files Editor". This editor is similar to a spreadsheet and lets you see all your strings in one view. You can edit as a spreadsheet or the underlying XML in the same view.
MOTODEV Studio is a branded version of Eclipse, so it should work with your existing projects if you use Eclipse. If you prefer to continue using your existing Eclipse setup, you can still use MOTODEV Studio to handle the editing of the string.xml files, just so long as only one version can have the workspace open at a time.
If you have any questions about how to use it, feel free to send me a message or post on our discussion boards at developer.motorola.com
There is none which I am aware, I am favouriting the question. :) However as a best practice, I first complete the default strings.xml and translate it in the very end. I also add a small marker comment to specify end of translation and any new strings are added below that. This helps me keep track of ones which are not translated.
-- UPDATE --
With latest ADT tool for eclipse you can install Lint which takes care of all the issues regarding duplicates and a lot more with its exhaustive set of warnings.
If you're willing to use the getlocalization.com web site, which is free of charge if you're willing to have your translators work on your localization publicly (otherwise, you have to pay to make your project private).
You can just use their Eclipse plugin, to automatically import the strings from your Android project:
http://getlocalization.github.io/eclipse/
Then this is the interface your translators will see when they do the actual translation:
I recommend you right-click on the screenshot above to view it in a larger format on a separate tab. It's actually well thought out and should make the translator's job easier too.
Steps to get all missing translations are :
enable lint error for missing translation in app level build.gradle
lintOptions {
abortOnError false
enable 'MissingTranslation'
}
add languages to compare inside default config of level.gradle (here english and hindi)
resConfigs "en", "hi"
right click on default strings.xml. Then Analyze -> Inspect Code
now check inspection result. Android -> Lint -> Correctness -> Messages > Incomplete Translation
All selected strings are missing translations
Do you know MotoDev Studio for Android? It features a localization tool. Within that tool all langauges are columns and all texts are rows. It's very easy to find missing translations within that "spreadsheet". The other way, find obsolet translations, is not that easy.
I suggest Amanuens that let you easily identify untranslated strings and strings that not match in master and translated files. It can, optionally, be configured to automatically keep translation files synchronized with the repository. You can also give your translators access to the service and they can find an easy to use web editor to translate your application.
I built a command line tool called ams (for android-missing-strings) that prints a report of every missing entry as well as leftover entries that no longer appear on your base strings.xml file.
It's available here
https://github.com/gubatron/android-missing-strings
ams - Android Missing Strings reporting tool.
Usage:
ams [-l xx[,yy,zz...]] -o <output_file>
Options:
-h --help Print this help
-l --lang <xx> Specify a language or many with comma separated 2-char language codes.
e.g: -l cn (creates report for Chinese strings.xml)
-l cn,it,fr (creates report for Chinese, Italian and French strings.xml files)
If this parameter is ommited, a report with every language file found will be created.
-o --oFile Specify the output file name for the report
Copyright (c) 2014 - The Mit License (MIT)
Authors:
Angel Leon <gubatron#gmail.com>
Katay Santos <kataysantos#gmail.com>
If you also want to let other people contribute and have a web site you can use the open source TranslateApp-tool.
It keeps track of what is translated and not, you can also update the default language and say if translations should be invalidated of not.
https://bitbucket.org/erik_melkersson/translateapp
Note: I am the author of the tool. Pull requests with updates are welcome. I actively use the tool myself.

Adding new String resource to multiple (localized) strings.xml files in Android app

I have an app with support for several different languages.
Now lets say I want to add a new feature, which will usually will require new string resources to be added.
What is the (Or is there) way to add new string resources that will be added automatically to all the localization strings files? (Means i'll just need to translate the content of the string and not to create new one with the same key for each localization file)
Because it dosen't make any sense to copy-paste it manually when you have a lot of localization files...
If you follow Android localization rules, there isin't any other way other than manually adding to each language strings.xml file. You can script this but then you will have to write the script. So, my suggestion is to bite the bullet and do it.
As you said if there are a lot of files, then scripting it is best way, even then you may have to manually add the localized string values to each file.
What you refer to is common Localization problem. It is not just strings.xml and Android platform related.
As you might be guessing, people actually found ways to resolve it. Typically, you will send just the English file to translators (translation vendors) and they will update it using Translation Memory software.
Or if you are independent Software Developer, you may want to use some crowd-sourcing platform for your translations, like Launchpad, BabelZilla or Crowdin. These platforms also act as Translation Memory, so you won't have to manually synchronize individual language files.

Adding/Removing the language entries in the "Settings" -> "Select Locale" from Android phone

My question:
How can I add or remove the language entries in the "Settings" -> "Language & keyboard settings" -> "Select Locale"(or "Select Language") in the Android phone from the source code level?
I'll provide more details on what I want and what I have done. Please excuse me if I present any silly understanding or information. I'm pretty new to Android development, starting to learn it several days ago. There is still a lot that I don't know.
More info on what I want:
I'm recently working on a task to localize the entire phone product with new languages(I mean, I'm not working on a specific application. This is why I need to add a new locale in the Settings). When I got the development phone, I found there are only several language entries available in the "Select Locale", such as "English", "French" and "Spain". So first of all, I need to add and display some new language entries to this list, such as "Japanese", "Chinese", "Korean".
I have all the source code of Android platform and my own code, so I think this task can be done by modifying the source code or the resource files somewhere. Unfortunately, I don't know where the code and files are.
The "Hello, L10N" tutorial shows a way to add a new locale from "Custom Locale" at run-time. But what I want is to add the new locales at build time so when the phone is started up, the new locales will be displayed in the "Select Locale" directly without any other actions.
What I have done:
I did a lot of research on the internet before I come here. Here is what I found:
Sinkiru in Stackoverflow asked a question about what I am exactly looking for. See it here. However, nobody else replied him. His own reply only tells where to query the CURRENT language setting, which is not what I want. I personally sent him an email for this question but haven't got his reply.
I searched a lot in Stackoverflow, with the keywords "android locale", "android language", "android new locale" and something similar. Most of the questions in this site are asking how to change the locale of an app, how to add multi-language support for an app, etc.. They are NOT what I want.
I searched a lot in the android-platform Google group(http://groups.google.com/group/android-platform). Some people are asking my question, but after reading through the messages I still don't get my answer.. :-( Here is a list of the posts I read in the group(and I do not get what I want from them):
"OS localization" - I'll talk more below.
"How to add new locale and it's associated font in android source"
"How to add new L0cale"
"Addition and Removal of language from Android Phone"
"Localization, committing new language"
"Additional languages support"
"Localization - new locale - HELP"
Back to the entry "(a)" listed above. In the "OS localization" message thread, Dianne Hackborn, an Android framework engineer, said the following:
"The settings app just shows you all of
the locales that are available in the
platform resources. Adding strings
for a new locale should automatically
have those shown in settings. You
tell the build system which locale
resources to include when it does the
build... unfortunately I can't help
you on that part, but maybe someone
else can."
His words in bold suggest that my task might be done automatically by adding new locale resource to the source code, and by setting up or changing something in the build system. I'm wondering if someone here has ever done the similar thing before? I'm going to give a try tomorrow.
Last but not least, I searched in the official Android developer site. In the "Run and Test the Localized Application" of the "Hello, L10N" tutorial it says something about the available locales in the "Settings" application, but that is basically talking about what to do in the emulator and this shouldn't be what I need. I want it done on a real phone.
I had also dived into the Android platform source code trying to discover something. I started at the $(ANDR_SRC_ROOT)/Packages/app/Settings/src/.../LocalePicker.java. In the LocalePicker::onCreate() method, I noticed this line of code:
String[] locales = getAssets().getLocales();
The code below this line tries to set all the obtained locale names to an ArrayAdapter and then set the adapter to the list view to show. Thus I think the "getLocales()" method is the key to the solution.
I then looked into the getLocales() code. getLocales() is a member method of AssetManager and is declared as native, hence I stepped into the $(ANDR_SRC_ROOT)/frameworks/base/libs/utils/AssetManager.cpp which I GUESS should be the actual implementation of the java getLocales() method. The C++ AssetManager::getLocales() finally leads me to the C++ ResTable::getConfigurations() method in which a member variable "mPackageGroups" is scanned through to retrieve all the available ResTable_config items. Thus I think all the available locales should be added into this mPackageGroups during the system started up. However, I lost in the code at last and couldn't find where the locales are added to the mPackageGroups.
you have to create "values" folder in "res" folder of android's framework-res package. Name of that new folder have to contain country code of your need ie. values-pl-rPL (the rPL part is required) for polish language (for british english: en-rGB, etc), that should be enough - if not - create strings.xml file in that folder, and add there empty "resources" node.
on HTC phones you must also modify /system/customize/CID/default.xml ... That is the file they use to specify locales on their stock roms. If the rom is a WWE, supposedly editing CID is all one need do, but this doesn't always hold true.
I think that a normal application should not add locations to the phone. There are special packages like MoreLocale 2 for that purpose. If you'd add a locale, you'd be responsible not only to provide localisation for your own application but for the whole system!
To create or add multiple languages to your application, first you have to define all you string values in the string.xml in the values folder rather than hard coding them in you program. Then you also have to have different Value folders for different languages, e.g. Value-es for Spanish and Value-fr for french. This should do the job. I dont think the application can change locale by default.
I think you have to edit the
PRODUCT_LOCALES
Present in build/target/product/languages_full.mk , and add and remove the strings assigned to this.
You can define your custom string like zz_zz and add folder values_zz_zz in each of your res folders.
Have not tried this but this might work.
Note: PRODUCT_LOCALES might be overridden somewhere else depending on your build system
For removing languages, if we remove the locale string from PRODUCT_LOCALES, it will work.
I am also looking for a way to add new languages, but just adding to this doesn't help. I think we need to update some low level files or libraries in order to add new languages.

Categories

Resources