Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 18. Localization

This chapter is quick and simple, but what we will learn to do can make your app accessible to millions of potential users. We will see how to add additional languages and we will see why adding text the correct way via String resources benefits us when it comes to adding multiple languages.

In this chapter, we will do the following:

  • Make the Note to self app multilingual by adding the Spanish and German languages

  • Learn how to better use String resources

Let's get started.

Making the Note to self app Spanish, English, and German


First, we need to add some folders to our project – one for each new language. The text is classed as a resource, and, consequently, needs to go in the res folder. Follow these steps to add Spanish support to the project.

Note

While the source files for this project are provided in the Chapter18 folder, they are just for reference. You need to go through the processes described next to achieve multilingual functionality.

Adding Spanish support

Follow the next steps to add the Spanish language:

  1. Right-click on the res folder, then select New | Android resource directory. In the Directory name field, type values-es.

  2. Now we need to add a file in which we can place all our Spanish translations.

  3. Right-click on res, then select New | Android resource file and type strings.xml in the File name field. Type values-es in the Directory name field.

We now have a strings.xml file that any device set to use the Spanish language will refer to. To be clear...

Running Note to self in German or Spanish


Run the app to see whether it is working as expected. Now, we can change the localization settings to see it in Spanish. Different devices vary slightly in how to do this, but the Pixel 2 XL emulator can be changed by clicking on the Custom Locale app:

Next, select es-ES and then click the SELECT 'ES' button in the bottom-left of the screen, as shown in the next screenshot:

Now you can run the app in the usual way. Here is a screenshot showing the app running in Spanish. I have photoshopped a few images side by side to show a few different screens of the Note to self app:

You can clearly see that our app is mainly translated to Spanish. Obviously, the text that the user enters will be in whatever language they speak – that is not a flaw of our app. However, look at the images closely and you will notice that I have pointed out a couple of places where the text is still in English. We still have some untranslated text in each of our dialog windows.

This...

Summary


We can now go global with our apps, as well as add the more flexible String resources instead of hard-coding all the text.

In the next chapter, we will see how we can add cool animations to our layouts using animations and interpolators.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton