Aside from just the text in your app, dates and numbers should be localized as well. In this video, learn how to format dates and numbers so they appear as the users in your new target locales will expect.
- [Instructor] There are other things aside from strings that users expect to be different depending on their locale, specifically dates and numbers. Let's take a look at how to set up your code to ensure users see what they expect. First, let's talk about formatting dates. We've already looked at an easy way to do this using Java's dateformat.getinstance method, to get a pre-configured date formatter. But if you need more control than just the short, medium, and long options, there's another class you can use, simple date format. In our home activity on the main screen, we display the current date, but provide a little more information than what's available in the built-in date format methods.
We provide the day of the week, along with the month, day, and year. And this is done here on line 38 with a date format string. The documentation for these strings is very comprehensive. Here's the Java doc for the SimpleDateFormat class. If you scroll down a bit, you can see this table where it spells out practically any date information you can think of, and the character to use to get it to come out in the formatting. Even things like the am/pm marker, the day of the week and the month, and more common things like the month and the year.
Now back in Android Studio, let's look at number formatting. Numbers are formatted differently for different locales as well. Back in the app, on the home screen, we display a large number where digits are separated with a comma. And this is the digits separator in the U.S. In other locales it might be a period or even a space. So let's see how to set this up in code. Back in our home activity, we can override that text with a little demo magic. I've exposed the string through the static field, demo.subscriptiontext.
Java has a class called number format that provides an easy formatter. So we'll get a reference to the formatter here. You can see that there are two different number format classes, one in the Java.text package, and one in the Android.icu text package. Android.icu is something that's provided in nougat and later, so if you're supporting versions earlier than that, you want to use the Java.text version. So create our number format object. And we'll get a reference to it called NumberFormat.getInstance.
And we'll use the default locale, since we're displaying it to the user. Then we can use that formatter. We'll un-comment the subscription text. We'll use the formatter and we'll use getString, along with the string resource. And we'll format the number, NumberFormat.format. The number that we want to display, on that screen.
Now we'll run the app and see that everything still looks as expected. The separators are still commas, 'cause we're still running on our English emulator. But now let's run it on our Spanish emulator. Now that the app is loaded, we can see the long number displayed as its digits separated by periods in our Spanish locale.
- The localization process
- Basic internationalization
- Choosing target markets
- Preparing your app for internationalization
- Translating your app
- Testing and releasing your translated app