There are a few basic things to do up front when building your application to make it easy to localize in the future. In this video, learn why it's important to externalize the strings in your app, and look at some lint rules that make it easy to find hard-coded strings in your app.
- [Instructor] Let's take a look at a very basic example of internationalizing and localizing an app. We're inside of our sample project. I'm looking at the subscription screen. In here, we have a lot of text that we need to make sure is readily available for translation. It's best to have your strings externalized for this purpose. Meaning, not hard-coded inside your app. If we switch over to the text view of this layout by clicking the Text tab here at the bottom of the screen, you can see at the bottom of the layout our Subscribe button and our Cancel button have hard-coded text.
Android Studio alerts us with a lint error that this is incorrect. You can see the line highlighted and you get more information if you hover over the text. It's pretty easy to notice this whenever you're just looking at the file since it's highlighted, but what if you're just joining a project and there's a lot of code and you want to see if and where there are any hard-coded strings? Android Studio has an inspector built-in that will do this for you. If you go up here to the Analyze menu and select Inspect Code, that brings up the code inspector.
We'll run the inspection across the entire project. For your project, it may take a while but in this small project, it runs quickly. Now that it's ran, we can see the results. The ones we care about here are under Android > Lint > Internationalization. If we expand that, there are two different categories, Hardcoded text and TextView Internationalization. And if we expand TextView Internationalization and then ArticleActivity.java, we can see two warnings.
Let's click on String literal and setText here and we get a preview here on the right that shows our String literal in our Java code without ever even having to open the file. But we notice that the errors in the layout file aren't in this group. So to see them, we can go back to the Results list, expand Hardcoded text, and there now, we can see our XML files, the activity_photo and activity_subscription. Telling you to expand that. You can see the warnings for both the Cancel and the Subscribe button.
So let's go ahead and fix this issue with the Subscribe button. If you double-click on this error, it will take us right to it. And let's go ahead and hide the inspection results to get a little more space to work with. In the XML, if you click here on the highlighted line, we can see this little light bulb show up on the left near the line numbers. This is Android Studio's quick fix system. If you click on the light bulb, we can see one of the options it provides is Extract string resource.
Choosing that option gives us this dialog where we can name the new string resource and add the value. And since this is a pretty basic string, let's just leave the label as subscribe and the value as subscribe. Then we can click OK. And now, the string in the layout file has been replaced with string resource. You can see the lint error is gone. And if you Command + Click, or on Windows, Control + Click, on the string resource itself, it will take you to the string resources file and see that the string resource was created automatically for us.
And now, as long as we're here in the strings file, let's see how to create a simple translation. We're going to translate this new string Subscribe into Spanish. At the top of the string file, you might see this banner that says Edit translations for all locales in the translations editor. If you do, you can click Open editor, but if you don't see it, you can use my favorite shortcut in Android Studio, which is cmd + shift + a or on Windows, ctrl + shift + a, and this lets you type in the name of any command in Android Studio to execute it directly so you don't have to find it in a menu.
So we can type translation and see Open Translations Editor as the first result, so we'll hit Enter to open that. So the Translations Editor here is pretty sparse right now. There's only a few strings and only one language other than the default, which is English in the United Kingdom. So, let's add our new language. If you click the globe here at the top of the Translations Editor, then it gives you options for many different locales, but what we want to do is just add Spanish. So you can just start typing and as you type, it filters down to exactly the language that you're looking for.
Now that we selected Spanish, it's added a new column here and prepopulated with the app_name. They're still empty translations for all the other strings. So now that we have this column, let's go ahead and add a translation for subscribe. Here we can call is Suscribir. Now, we can find that strings file in the panel on the left. If we expand values, and now, strings.xml, now there are three strings files.
One default, one for British English, and a new one for Spanish that includes our new translation. And then we can go back into the subscription's layout, switch to the Design tab, and change our language to see the new string Suscribir at the bottom of the screen. This is just a very basic example of internationalizing and localizing a part of your app. At the very least, you should make sure your app is internationalized even if you're not planning to localize right away, so make sure your strings are all externalized.
And the use of the translations that are here is just a quick way to view all of your translations. As we'll see later, there are other ways to get the strings out of your project and into the hands of professional translators, so they can do the hard work of translating your content.
- The localization process
- Basic internationalization
- Choosing target markets
- Preparing your app for internationalization
- Translating your app
- Testing and releasing your translated app