Speaking Spanish4:24 with Ben Deitch
In this video we'll update our app to use the right Strings for a Spanish audience!
<resources> <string name="app_name">Rey de Pintura</string> <string name="buy">Compra</string> <string name="purchase_message">Orden Recibida: %1$s</string> <string name="black">Negro</string> <string name="blue">Azul</string> <string name="gray">Gris</string> <string name="green">Verde</string> <string name="orange">Anaranjado</string> <string name="purple">Morado</string> <string name="red">Rojo</string> <string name="white">Blanco</string> </resources>
[MUSIC] 0:00 Hi I'm Ben and 0:04 in this workshop we're going to learn all about localization in Android. 0:05 If you're making an app that will be used in more than one country or 0:10 even in just more than one language then you'll need to localize your app 0:13 to make sure that all of your users have the same wonderful experience. 0:17 In Android we've got a few tools to help us localize our apps, and 0:22 we're going to see how to use those tools by localizing an app for 0:25 a fictional paint company named Paint King. 0:29 As it stands the app already has a navigation drawer 0:32 where we can select the color, and when a color is selected 0:35 the user will be given an option to buy paint in that color. 0:38 The only issue is the app is only in one language. 0:42 English. 0:45 But we've recently started to sell paint in a couple of countries that don't speak 0:45 English, specifically Spain and Egypt. 0:49 If we want to be able to keep our business growing, we better localize our app. 0:52 Let's start with localizing the app for Spain. 0:57 But first if you'd like to follow along make sure to download the app 1:00 from the teacher's notes below. 1:03 Once you've got the app set up, let's run it and see what we're working with. 1:05 As promised, the app is called Paint king. 1:14 And if we click on one of the colors on the navigation drawer, 1:17 then we get the option to buy paint in that color. 1:20 And if we click on the buy button we get a toast to let us know that our order was 1:23 received and also tells us what time it was received. 1:26 If we want to buy a different color of paint we can open the navigation drawer by 1:30 sliding in from the left edge and then picking the next color we need. 1:34 Now that we understand a little more about how this app is put together 1:38 let's talk about localizing the app for a Spanish speaking audience. 1:41 The first step to localizing our app is Is to make sure that any user facing strings 1:44 are declared in the strings resource file. 1:49 This way supporting a new language is as 1:51 easy as just swapping one string's resource for another. 1:54 Over in the project pane if we go ahead and 1:57 open up our strings.xml file we can see that we've already done this. 1:59 We've got the apps name, the text for 2:05 the buy button, the purchase message with a placeholder for the date and then we've 2:07 got our eight-color strings which we use to populate a string array named colors. 2:12 All right. 2:17 Since we're already using strings.xml for 2:18 all of our user-facing strings all we need to do to support Spanish 2:21 is to create a new strings file containing Spanish translations of our strings. 2:25 Let's start by creating a new strings file. 2:30 So right click on values, pick new values resource file and 2:33 then let's name it strings. 2:37 Next under available qualifiers let's pick locale and 2:41 then double click the double greater than sign to add it. 2:45 Finally for the language let's pick es Spanish and then hit OK. 2:48 And now we've got a Spanish version of our strings file, perfect. 2:55 Now we just need to populate it with the Spanish terms we'd like to use. 3:01 Down below in the teacher's notes go ahead and copy and 3:04 paste then the Spanish translations. 3:07 Then let's run the app. 3:11 And of course it's still in English. 3:15 So let's go to our settings and set the emulator. 3:18 Scroll down to the languages option, which for me is languages and input. 3:24 And then let's change the language to Spanish, specifically from Spain. 3:30 Then let's switch back to the app by using the recent apps button. 3:38 And sweet, now it's in Spanish. 3:43 But wait a minute, 3:45 it looks like on Anaranjado is a little bit longer than we anticipated. 3:46 When you're making an app that will be translated into other languages 3:50 it's usually a good idea to give a little more room for your text views. 3:54 Back in the code, let's open up activitymain.xml and 3:58 at the bottom let's add a little more room for our drawer layout. 4:03 Let's add 20 dp of width by changing 220 dp to 240 dp. 4:06 And if we run the app now it looks a lot better, but we're still missing something. 4:11 In the next video we'll see how we can format numbers 4:18 to match the locales of our users. 4:22
You need to sign up for Treehouse in order to download course files.Sign up