Back to Home

Android, PhraseApp and files - or say a word about poor Android lines

Android · PhraseApp · strings.xml

Android, PhraseApp and files - or say a word about poor Android lines

  • Tutorial
When working on the Android platform, you often come across the need to fill in the string constants strings.xml. All would be nothing, but the rules that Android imposes on the contents of these files are very limited. Some of them are obvious, for example, the apostrophe sign must be escaped with a slash, while others are associated with both an XML format device and Android features. These features will be discussed in the article below.

So, let's say we have a set of lines received from the customer in the form of a text file or iOS PLIST or even an Excel document. In my case, these were strings imported into the project from PhraseApp. The name fields are filled in automatically based on the names given by the developer of the locales and PhraseApp rule set. It is worth noting that for other platforms on which there are not so many restrictions, export is successful, which greatly speeds up the development and localization in a multi-platform environment. Unfortunately, looking for line errors using AndroidStudio is just as inconvenient as many versions ago, the studio just doesn’t show the error line numbers in XML, so Eclipse can still help a tired developer. So, get to know the pitfalls closer.

Spaces in names



Are you sure?

space between confirm and exit is not valid, replace it with an underscore

Are you sure?


Dots in names



In general, dots in names are valid; the environment will be happy to accept the option

Male

but the devil, as you know, is in the details, trying to collect an array of strings or plurals with a dot in the name, we get an error of the type
Error retrieving parent for item: No resource found that matches the given name 'age'.

0-1314-1819-2122+

for all string-array and plurals we change the point in the names to the same underline



Java keywords in names



This is not entirely obvious, but Java keywords cannot be used in names. The next line will not let the project get together.

Else

The way out is the same - we supply the else with an underscore at the beginning of _else and the project will reassemble.

Formatted output



Formatting the output greatly facilitates the readability of the source code, but here we are in trouble. We will output two or more values ​​with substitution in one line at the same time, for example, the time from and to. We get the error
Multiple substitutions specified in non-positional format; did you mean to add the formatted = "false" attribute?

Time from %s to %s

adding the attribute formatted = “false” corrects the situation, but we still have plurals and string-array, we complicate the example and try to collect

Error %s occured%s errors ocurreds in %s

we get two errors at once
Multiple substitutions specified in non-positional format; did you mean to add the formatted = "false" attribute?
Found tag where is expected "attribute?

Adding the formatted =" false "attribute already doesn’t fix anything, but Google suggests that the parameters must be set with their serial numbers, so that the system understands what and where to substitute. The principle is simple, the parameters are numbered starting from one and use dollar sign: For the first parameter, the replacement will be% 1 $ s - for the second% 2 $ s, for the next% (i + 1) $ s.

Error %s occured%1$s errors ocurreds in %2$s


Back to PhraseApp, when using the plugin of the current version, all of the above errors await the careless developer, in the event that the lines were created without taking into account the specific requirements of Android. These errors need to be fixed every time resources are updated. This is clearly inefficient. Automation begs. Unfortunately, I did not succeed in writing a sane script for sed. In addition, Windows users are not eager to look for this sed and add it to the startup path.

As an alternative, I bring to your attention another bike for working with strings in Java. The project is open and lies on GitHub. All code is in one file ignoring OOP, work with string.xml is done forehead, without DOM or SAX. There are three operating modes in the utility: by feeding the directory to the input, we will process all the lines in all subdirectories in files with the name strings.xml, by feeding the two files - we will take the first one and copy it to the second one with error correction, and substituting one file we will process and rewrite it. To start, just put the assembled utility in the root of Eclipse or AndroidStudio and execute with the parameter as a

java -jar point AndroidCleanFixXml.jar.

For PhraseApp users, editing the source code of the utility may fail. Often those who stuff locales create a template for formatted output in the form of% {location} -% {duration}, such a substitution will not work with String.format, so all of them have to be replaced. Since in general it is impossible to understand what type of data will be substituted, there are three static constants PHRASE_FLOAT, PHRASE_DECIMAL and PHRASE_STRING in the code. All substitutions found in your localization files must be manually added to the constant.

No temporary files are created during operation - so remember about backups or commit to the repository before use. Enjoy using it, write issue on github, I will try to fix it as far as possible.

Read Next