When localizing the text in your app, you may come across cases where your new languages are structured differently than you're used to. In this video, review how to use string placeholders and Android plural resources to support these differently structured languages.
- [Instructor] The first thing most people think of when localizing an app is translating the text. We've talked a bit about externalizing our strings already. Well let's take a look at some other things to consider when preparing these strings for translation. Often your text will need to be dynamic in response to something custom in your application. In our local news app let's click on one of these articles and we can see on this article page we have a string that says published on October 5, 2017. We might implement that by adding a string that says published on with a space at the end of it and then concatenate the date to the end of that string for display, but that structure isn't consistent across all languages.
For example, in Hindi the date would go at the front of the phrase. So we can add this flexibility with formatting place holders. So let's go into Android Studio and we'll open the article activity. So here we can see that we are setting the text on the article date text view. We can see that there are actually two different errors. One is the hard code to string error and one is the place holder error. So let's fix both of them. We'll open the strings file and it's easy to jump to it by using the shortcut Command + Shift + O and if you type in strings.xml you'll see all of your strings files and make sure to select your default strings file.
Now let's add a new string towards the bottom of the file here on line 22. We'll call it published on and we'll make the text published on and here we need our place holder. The place holder is percent one dollar sign S. So it's made up of two parts, the index and the format type. The index, which is the percent one part, it indicates the order of the arguments and this string since there's only one argument it may not make a lot of sense, but it's required.
It let's you change the order around in different languages in cases where you may need to do that. And the second part is the format type. We're passing a string in, so we're going to use S, but you could use F for floating point or D for an integer value. Back in the code to get the argument passed into the string we have to use the get string method. So we'll go into set text and we'll replace what we have here with get string, the resource identifier for the string that we just created, published on and then pass in the date as the second parameter to this get string method.
So I'm going to delete the hard coded string and leave the date formatting there and scroll all the way to the end to make sure we have our all of our parenthesis that we need. Now we don't see the lint error anymore. If we run the program and go back into the article page we can see the date still shows up as expected. You might also want to use a formatting place holder for inserting numbers. On the same article screen here we display the comment count for an article, but it's a little more complicated if there's only one comment you'd want to say one comment, and if there are more than one comments you'd want to say five comments with an s.
This is handled in android with plural strings. Now this pluralization rule I just mentioned of one comment versus multiple comments is not universal across all languages. In Chinese for example, there is not distinction between one or many. In other languages zero is a special case that must be handled separately. Some of these distinctions are isolated to languages that you may not support initially, so rely on your translators to help make the distinction of what should and shouldn't be supported.
In Android plural strings are a different resource from regular strings. In Android Studio let's go ahead and open up the plurals.xml file. You can find that in the resources directory and then under values. Right here next to the strings file is plurals.xml. I've gone through and added details for each of the options, but the ones you'll most likely want to focus on are one, here on line eight and other here on line 21. I've added the strings for the different comment options, so let's go ahead and implement this in our code.
So back in article activity.java here where we're loading the comment count we can see we're getting the article comment count and we're just appending comments to the end of it. So let's go ahead and create a new variable to make it a little easier to read. We'll create a string and we'll call it comment count. To get access to the plurals we have to get the resources object, which is available in our activity, so we'll call get resources and then the method is called get quantity string, and it takes three parameters.
The first is the reference to the plurals resource, which ours is called comments, and then the second is going to be the number that's used to select which plural value to provide. In our case it is the article comment count. So article data.get article comment count. Then it also takes in a third parameter and the third parameter is used for our formatting string. So we're going to pass the exact same thing in.
The first time we pass the comment count in is to select the string that's used to display and that's to distinguish between comment and comments. The second time we pass it in is used to replace our place holder so the number is added to the beginning of the string. Again, we'll pass in article data.get article comment count. So now we've created that string and we'll replace the set text method call instead of the concatenation with the comment count text that we added.
So let's go ahead and run the app again. We'll click on the first article and here we can see it still says 124 comments and if we go back and go to a different article, the dog park article, we can see that this article only has one comment. Both of these recommendations may see like overkill if you're just targeting one language, but setting up string place holders and plural strings ahead of time will make any future localization a lot easier.
- The localization process
- Basic internationalization
- Choosing target markets
- Preparing your app for internationalization
- Translating your app
- Testing and releasing your translated app