- Локализация. חתול מדען (хатуль мадан) — кот учёный
- Просмотр и создание локализованных строк в режиме дизайна
- Непереводимая игра слов с использованием местных идиоматических выражений
- Поддержка письма справа налево
- Несколько локалей
- Android Localization: Getting Started
- Version
- Getting Started
- Localization
- How the System Knows What to Display
- Adding Strings Files for Other Languages
- Creating a New Resource Directory
- Creating a New Strings File
- Previewing in Android Studio
- Testing on an Emulator
- Tips for Getting Good Translations
- Right-To-Left Languages
- Mirroring UI
- Creating Custom Layouts for Locales
- Testing for RTL Locales
- Developer Options
- Pseudolocales
- Drawables
- Localization Checklist
- Where to Go From Here?
Локализация. חתול מדען (хатуль мадан) — кот учёный
В статье, посвященной ресурсам, мы уже затрагивали эту тему с теоретической точки зрения. Разберем теперь практическую часть. Мы уже умеем пользоваться строковыми ресурсами и меняли строчки, например, Hello, World на Здравствуй, Мир! Но на самом деле это не совсем правильный подход. Для профессионально написанного приложения лучше использовать другой подход. А именно, локализацию.
Что это значит? Мы не будем менять строчку Hello, World, которая будет считаться строкой по умолчанию, а создадим новый локализованный ресурс. Это даст нам дополнительное преимущество, если пользователь запустит приложение на телефоне с английской локалью, то он увидит текст на знакомом ему языке. Если приложение запустит русский пользователь, то он увидит текст на русском языке. Согласитесь — очень удобно.
Напишем приложение, которое будет поддерживать английскую и русскую локализации. По умолчанию будет использоваться английская локализация. В этом случае, если на телефоне не будет найден нужный локализованный ресурс, то будет применяться английский вариант.
Чтобы не усложнять материал, воспользуемся стандартным приложением, который мы изучали в самом начале. Создадим новый проект «LocaleApp», но на этот раз мы не будем трогать файл strings.xml в каталоге res/values.
Для русской локализации необходимо создать новый подкаталог values-ru в том же каталоге res. Если вы планируете создавать локализацию только для русского языка, то название каталога не трудно запомнить и вы можете создать папку вручную через res | New | Directory. Но вряд ли вы запомните названия других папок. Поэтому покажу способ создания с помощью мастера студии.
Щёлкаем правой кнопкой мыши на папке res и выбираем New | Android resource directory. В диалоговом окне в левой части Available qualifiers: выбираем пункт Locale и переносим его в правую часть Chosen qualifiers: с помощью кнопки с двумя стрелками вправо. В появившейся третьей колонке выбираем нужные языки, например, русский. Вы увидите, что в поле Directory name автоматически появится нужное название папки. Дополнительно вы можете указать и регион в колонке Specific Region Only
В режиме Android вы можете не увидеть созданную папку, поэтому временно переключитесь в режим Project. Скопируйте файл res/values/strings.xml и вставьте его в новую папку. Можете вернуться в прежний режим. Если папка не пуста, то она уже видна.
Изменим его содержание.
В английской версии им соответствуют ресурсы
Запустим приложение и увидим, что текст выводится на русском, если у вас на эмуляторе или реальном устройстве выбраны русские настройки.
Как перейти на американскую локаль? Идём в Настройки→Язык и клавиатура→Выбрать язык. Выбираем английский язык English (United States). Снова запускаем приложение в эмуляторе или на телефоне. Теперь наше приложение будет выглядеть так:
Ресурсы из каталога res/values считаются ресурсами по умолчанию. Они отображаются в том случае, когда система не найдёт ресурсы для текущей локали устройства. В нашем случае, при выборе любого языка, кроме русского будет использоваться res/values. Если мы захотим разделить англичан и американцев, тогда придётся создавать два каталога: res/values-en-rUK и res/values-en-rUS. Обратите внимание, что перед кодом второй части ставится буква ‘r’. Иными словами, мы определяем не только язык, но и регион.
Проведем несколько экспериментов. Удалим из файла res/values-ru/strings.xml строковый ресурс hello_world с текстом Здравствуй, Мир!. Сохраним изменения и запустим проект на устройстве с русским языком. Платформа заменила отсутствующий в локализации ресурс дефолтным английским.
Продолжим опыты и поступим наоборот — удалим теперь ресурс из файла res/values/strings.xml. На устройстве выберем какой-нибудь другой язык, например, итальянский. В этом случае вы увидите что-то непонятное, скорее всего вы получите сообщение об ошибке.
Отсюда можно сделать вывод, что вам необходимо следить за ресурсами по умолчанию. Старайтесь проконтролировать, чтобы ваше приложение всегда содержало все необходимые ресурсы по умолчанию, а уже потом можете начинать локализацию приложения. Для этого вам достаточно будет скопировать файл в новый подкаталог и отредактировать его.
На самом деле локализовать можно не только строковые ресурсы. В разных странах принято рисовать какой-нибудь объект в соответствии со своими традициями, например, изображение почтового ящика. В этом случае вы можете создать каталог /res/drawable-de, в котором будут находиться изображения, предназначенные для немцев.
В коде вы обращаетесь к нужным ресурсам следующим образом:
Система сама подставит нужную строку. Явно указать, что хочется использовать ресурс на конкретном языке, у вас не получится.
Спустя продолжительное время один из читателей подсказал, как всё-таки задействовать локальный ресурс. Объяснять код не буду, сами поймёте, когда наберётесь опыта.
Просмотр и создание локализованных строк в режиме дизайна
Чтобы увидеть, как меняется текст в различных локализованных версиях вашего приложения, необязательно запускать приложение в эмуляторе. Можно в студии на панели инструментов щёлкнуть на значке глобуса и выбрать из выпадающего списка нужный вам пункт (он появится после того, как вы создадите необходимые локализованные ресурсы), и все локализованные строчки сразу поменяются на экране активности.
Можно сразу создать локализованный файл через удобный мастер. Там же в меню есть пункт Edit Translations. , который выводит диалоговое окно со списком уже имеющихся строковых ресурсов из вашей программы (раньше). Вам нужно выбрать язык для перевода, а затем заполнить в таблице нужную колонку.
После того, как вы завершите перевод всех ресурсов, нажимаете на кнопку OK и у вас появится готовая папка с новым файлом strings.xml для выбранной локализации.
Сейчас в студии вместо диалогового окна выводится окно редактора, в котором можно редактировать текст.
Непереводимая игра слов с использованием местных идиоматических выражений
Если вы решили использовать локализацию на разных языках, то можете встретиться с одной проблемой. Предположим, вы перевели только несколько строковых ресурсов и в один прекрасный день решили выложить программу в магазин приложений. Android Studio при сборке подписанного приложения может заругаться, что вы перевели не все строки. Есть два способа обхода проблемы. Вы можете использовать атрибут translatable=»false», чтобы указать среде разработке, что данная строка не требует перевода:
В окне редактора это сделать проще через флажки в столбце Untranslatable.
Второй вариант — явно указать, что все строки в файле не нужно переводить с помощью tools:ignore=»MissingTranslation».
Есть ещё один вариант, когда непереводимые строки размещаются не в ресурсе strings.xml, а в другом файле под именем donottranslate.xml.
Когда вы исправите строковые ресурсы, то избавьтесь от ошибок через Project | Clean. и попытайтесь собрать приложение заново.
Поддержка письма справа налево
Арабы и некоторые другие народы используют непривычную для европейцев схему написания текстов справа налево. В Android 4.2 появилась поддержка данного режима. В блоге (англ.) Native RTL support in Android 4.2 рассказано о некоторых вещах по этой теме.
Теперь вы можете писать приложение для израильских военкоматов. Русские призывники в интервью офицерам душевного здоровья часто используют сочетание хатуль мадан (חתול מדען). Теперь вы знаете, как локализовать строки пушкинского стихотворения про кота учёного.
Несколько локалей
В Android 7.0 (API 24) пользователь может установить несколько локалей. В этом случае, если не будут найдены ресурсы для первой локали, то будут искаться ресурсы для второй локали. Таким образом, если француз знает два языка, например, французский и испанский, а в приложение не поддерживает французского, но поддерживает испанский, то пользователь увидит текст на испанском, а не на английском.
Источник
Android Localization: Getting Started
Android runs on many devices in many regions. To reach the most users, your app should handle content to reflect the locales where your app is used.
Version
- Kotlin 1.3, Android 8.0, Android Studio 3.4
When building apps, it’s important to think about your users. After all, they’re the ones who’ll be using your apps!
You should consider if your app would benefit from multi-lingual support to appeal to the widest range of users. You might also find that different images or layouts work better for different locales.
In this tutorial you’ll learn how to:
- Support multiple languages.
- Test your layouts for different languages, including Right-to-Left languages.
- Add resources for different locales.
Getting Started
Start by downloading the starter project using the Download Materials button at the top or bottom of this tutorial.
Open the project in Android Studio and get familiar with the files. Then build the project and run it on a device to see how the app looks.
The app is a simple avatar generator. The user can customize their avatar by selecting hair, clothes and shoe options from four Spinners. The Share button brings up a Share Sheet so the user can share a text description of their customized avatar through an app of their choice.
This starter project was built to appeal to an American audience. It displays options which make sense to those familiar with American English. There’s also a fun illustration which is very America-centric which displays the American flag.
In its current state, this app is fine for an American audience. But suppose you wanted to put this app in front of a British audience. The pants option takes on quite a more risque meaning!
You should add locale support for these users before making the app available to them. This tutorial will show you how to do that.
Localization
Before you dive in, keep the following things in mind. Your life will be much easier if you decide to localize later on.
- Keep your strings in a strings.xml file: Instead of using hardcoded strings, which are added directly where needed in the code, define each string as a key value pair in a separate resource file called strings.xml. Here you assign an id to each string which can be referenced wherever you want to use that text in your project.
This means all your strings are easy to find. You’ll see how that makes adding translations straightforward in a minute.
This file should be kept in the values directory in the resources section of your project. It’ll be your default strings file.
Once you’ve developed your app with all your strings in the strings.xml file, adding localization support is quite straightforward. The Android system does much of work for you.
How the System Knows What to Display
When a user opens your app, the system uses the device locale to determine in which directory it can find the resources to display. Directories contain qualifiers in their names. The system uses these qualifiers to select the correct directory for each locale.
It’s important to provide a default resource file for each kind of resource, such as strings and drawables. If the system can’t find a directory to match the configuration of the device, then it’ll fall back to the default file. For example, if the user’s primary device locale is Spanish but there is no values-es directory present in the project, it’ll use the default.
If the system can’t find a default value to map a resource id to, then your app will crash. So, make sure to always define a default for all of your resources. The default file will live in a resource directory without any qualifiers in the name.
For each new language you wish to support, you’ll need to put a new strings.xml file containing your translations inside a new resource directory with a locale qualifier. The new file should contain translated strings paired with the same id used in the default file, as this id is the one which will be referred to in the code.
Don’t worry if this is a bit confusing at the moment. You’ll go through this step-by-step in the next section when you add British English translations to the sample app.
The following table, taken from the developer.android.com documentation, shows an example of the resolution strategy Android uses for versions greater than Android 7.
Here, the user has their device’s primary language set to fr_CH, but the app hasn’t explicitly supported this language. Failing to find an explicit match, the system looks for directories supporting other regions of the language. In this case, it found and displayed fr_FR.
If this directory hadn’t been present, it would’ve fallen back to the default. The default locale for Android devices is usually en-rUS.
For more explanation and examples of system resource resolution, consult the official Android documentation.
That seems like quite a lot to take in at once, but in practice it’s a fairly straightforward process. Time to get started localizing your app!
Adding Strings Files for Other Languages
In the sample project, open strings.xml. Most of the strings make sense on both sides of the pond. But, there are some which you should translate for a British audience:
Creating a New Resource Directory
Before moving on, make sure you have Android Studio set to the Project view, and not Android. This will let you see all directories as they appear on disk. That’ll come in handy when when you add new resource files later.
Create a new resource directory to store the GB translations. In Android Studio, navigate in the Project panel to the res folder. Right click on it, then choose New ▸ Android Resource Directory.
This opens the New Resource Directory window. From there, you can select the resource type and qualifiers you want to create. For localization of strings, add a new directory with a values resource type and a Locale qualifier.
Select Locale from the list of qualifiers and move it into the Chosen qualifiers section by pressing the >> button in the middle of the window. This will open new panels showing all the available options for your chosen qualifier. Select en: English as the language and GB: United Kingdom as the specific region.
Notice the directory name has been pre-filled with the resource type and qualifiers you selected. Don’t change the directory name because this format is required by the system to find your resources.
Press OK to confirm and close the window. You’ll see a new values directory appear in the Project panel named values-en-rGB. Put the strings file that contains all the translations for British users in this directory.
Creating a New Strings File
Create a new strings.xml file in this directory by right clicking on the directory. Then choose New ▸ Values resource file.
In the window that pops up, enter the name strings.xml and press OK. It’s important to give this file the same name as your default strings file, in the values directory, so the system can swap them out as needed.
You’ll see the new strings.xml has a little Union Jack to help you identify it in the Project panel and open file tabs.
In this new file, you can add any translations you want to show when the device is set to British English. Remember, you don’t need to provide translations for every string in the app here. As long as the string is defined in the default strings.xml, any strings which haven’t been defined in the British file will fall back to their default value.
Add the following translations to the new strings.xml between the resources tags. The string names exactly match the ones already defined in the default file. Only the value has been translated.
And that’s it— you’ve added translations to your app! But how do you know if it worked? You can test in Android Studio before you run it on a device.
Previewing in Android Studio
Open activity_main.xml in Android Studio. At the bottom of the file, make sure you’ve selected the Text tab so you can see the xml code.
You’ll see a Preview panel on the right of the screen. You may need to expand it from the side panel if isn’t already.
In the Preview pane’s toolbar there’s a Locale for Preview dropdown which shows Default (en-us) . This is your default strings file.
Now that the project has multiple locale directories, you can change this option in the preview to see how it looks. Select English (en) in United Kingdom (GB) from the dropdown menu.
One snag— nothing’s changed!
The three translations were all for values used in the Spinners. These get added to the code programmatically rather than in the xml file. The xml layout file doesn’t know about these values and can’t populate them in the preview.
For the sake of this tutorial, add another string translation to your en-rGB/strings.xml for the title at the top of the screen. This value gets assigned to the TextView in the xml code so Android Studio knows how to populate it for the preview.
This translation is a temporary addition for testing purposes. You’re not going to ship with it. So, for now, add a string you’ll recognize to show it worked in preview:
Go back to activity_main.xml preview. With English(en/GB) still selected, you should now see the temporary title in preview. Congratulations, it worked!
This preview is useful because it shows you how your layouts look with different values. Some languages may have longer or shorter translations than your defaults. This is a good way to catch any problems with your layouts early on.
Before you forget, go back and remove the temporary string you added.
Testing on an Emulator
Next you should test on an emulator. Build and run the app. Initially, nothing should look any different than when you first ran this project.
The emulator is like a real device. You can change the primary language in the same way you would on your Android phone.
On the emulator, open the Settings app and navigate to System ▸ Languages & input ▸ Languages. If it’s not already there, add English (United Kingdom) as a language to your Language Preferences. Move it to the top of the list.
Reopen the sample app in the emulator. You’ll now see the spinner options have changed to display the translated values.
The app is now pulling in the translated strings from the GB resource directory to comply with the primary device language set by the user. Your app now supports multiple locales!
Tips for Getting Good Translations
Knowing how to support multiple languages in your app is all well and good. But to provide that support to your users, you need to actually get translations for the text in your app.
Unless you’re a master linguist, it’s unlikely you can translate all your strings into all the languages you want to provide support for. So, you’ll need to find someone who can translate it for you.
Maybe you have international friends you trust to provide local translations for you. Failing that, there are many companies that provide translation services.
However you obtain your translations, in most cases, you’re going to send them across strings.xml. So, the more context you can provide about where those strings are used, the better.
Some of the words will be straightforward to translate. Others may have different translations with different meanings depending on the context in which they’re used.
In strings.xml, you should provide comments with the context for each string. Look in the sample app and you’ll see there are comments providing context about what the strings are for and where they’re used in the app.
Other things to consider:
- Untranslatable strings: If you don’t want a string to be translated, you can add translatable=”false” to the string declaration after you define the name.
- Formatters: If you’re not sure what formatters are, check the formatting and styling documentation. With the broad range of regional support Android provides, there can be differences in formats for things like dates and times within the same locale.
You should use formatted strings rather than hardcoded values or method calls to convert numbers or dates to strings in your app logic. This lets the system format the numbers with the correct set of digits according to the region. Read more about this here.
Right-To-Left Languages
Supporting multiple languages can sometimes require extra consideration. Some languages, for instance Arabic or Hebrew, read from right-to-left, RTL, rather than left-to-right, LTR, like the English languages this tutorial has covered so far.
Mirroring UI
To support RTL languages in your app, you should test your layouts to ensure your UI functions as intended when mirrored.
For simple layouts, following Android best practices when creating your layouts should go a long way. You should always build according to best practices to minimize the effort required to make changes down the line. Key things to keep in mind are:
- Always use xml attributes referring to the start and end of views instead of left and right , unless your layout strictly requires this for all locales. If the device is set to RTL, the right hand side of the screen is considered the start instead of the left. So, these attributes help display your layout as expected when mirrored.
The left hand side of the screen remains on the left even when mirrored. So the layout doesn’t change when mirrored when you use this attribute to position a view.
Don’t restrict both android:layout_width and android:layout_height of a TextView to fixed values. Words and phrases may differ in length when translated into different languages. The text may be cut off if you have fixed your view to a size which is too small.
It’s most common to set either android:layout_width or android:layout_height to wrap_content to allow it to wrap to the size it needs. If your layout really does need fixed sizes, you should look at utilizing Autosizing TextViews.
Creating Custom Layouts for Locales
In most cases, following the above tips should enable you to use the same layout file to support both LTR and RTL. But for complex layouts or particularly custom UIs, you may wish to create separate layout files to optimize layouts for different layout directions.
Store the RTL optimized layouts in a resource directory named layout-ldrtl , keeping the layout file name the same as the one you wish to replace for RTL configurations. You can create this directory in Android Studio the same way you created a new directory for different String locales earlier.
This time, select layout as the resource type and use Layout Direction as the qualifier. Pick RTL as the layout direction for this directory.
Testing for RTL Locales
The sample project currently doesn’t contain any RTL language files. But you can still test to see if the app is ready to support them.
First, confirm the app has enabled RTL support by checking in AndroidManifest.xml that application contains android:supportsRtl=”true” .
While developing your layout in Android Studio, you can use the layout preview pane to quickly see how it would look in RTL mode.
Earlier, you learned how to view the localized strings in the layout preview. You might have noticed the Right to Left preview in the same menu. Toggling Preview Right to Left in this menu mirrors the layout in the preview.
Once you’re happy with your layout, you should also test on an Android emulator. Luckily, the developer options on the device have some useful tools for testing different types of languages. So, you can test your layout in RTL mode without learning to read Arabic first!
Developer Options
If you haven’t already, enable developer options on your emulator. To do this, open the Settings app and find the Build number.
This number’s location can vary for different devices, but you’ll likely find it at System ▸ About emulated device. Tap the row containing the build number seven times.
Congratulations, you’re now a developer! :]
With your shiny new developer status, you now have access to Developer Options, a new menu in the System area of Settings.
Click the Developer options menu and ensure it’s turned on.
Pseudolocales
Next, enable pseudolocales for your debug builds. In the sample project you need to add a new configuration to your app build.gradle. Add the following code inside the buildTypes section to add a new debug build type and enable pseudolocales.
Your build.gradle should look like this:
Since you edited build.gradle, you’ll need to sync. Press the Sync Now button in the banner at the top of the file. Then build and run the app. You’re ready to test on the emulator.
With the latest debug version of the app loaded on the device, all that’s left to do to test for RTL locales is to change the device language to a pseudolocale. There are two available pseudolocales:
- English([XA]): This is a LTR locale which reads like English, but adds accents to characters and expands text to aide with exposing layout issues.
- Arabic([XB]): This is an RTL locale. It triggers the device to go into mirrored UI mode to better support the RTL text direction. It also reverses the characters of English strings to help mimic an RTL locale. This helps expose layouts that aren’t mirrored properly and strings that can’t be translated or have other punctuation or number character ordering issues.
You can find the pseudolocales in the Languages section of Settings.
Select cibarA([XB]) from the languages list and move it to the top of your language preferences. The device will automatically switch to RTL mode. You’ll also see that the system string character ordering is reversed to emulate a RTL directional locale.
Open the app and you’ll see how it looks in a RTL locale.
The app looks good mirrored and is ready to support RTL languages!
Drawables
You’ve already covered a lot in this tutorial, but there’s still one thing left to address. The app still shows the US flag to users of all locales!
By now, you’ve probably got the hang of adding resource files for different configurations, but here are the steps one more time.
Create a new resource directory where you’ll add a new image to display to British users. As before, right click on the res package and select New ▸ Android resource directory. You need to create a new drawable folder for the en-GB locale. Select drawable as the resource type, locale as the qualifier, en:English as the language and GB:United Kingdom as the region.
When you add a new image to this directory, you’ll need to make sure to save it with the same name as the default image. That way, when the device switches locale it can use the same filename referenced in the code to get the image.
For the sample app, you should save the new image with the filename img_flag.png. You can either save this image directly into the folder using a File Explorer on your machine, or drag and drop the image file into the folder in Android Studio.
Feel free to find your own image to use here. But here’s a cute little Union Jack in the same style as the existing US flag you can use. Right click and save it into your new GB resource directory.
You’ve now added a localized drawable. Build and run the app again. Change your emulator or device’s primary language to English (United Kingdom) and open the app again to see the new image displayed on the screen.
Localization Checklist
Google provides much of support for developers through the Google Play store. For localization, they’ve provided a handy Localization Checklist to help get your app ready to support new locales.
The checklist contains useful tips to ensure you haven’t forgotten anything and covers the whole process from start to finish. It’s a very useful resource and worth checking out both before and after you decide to target multiple locales.
If you’ve covered everything in that checklist, you should be ready to go!
Where to Go From Here?
Download the final project using the Download Materials button at the top or bottom of this tutorial.
As you’ve seen, there are many things to consider when localizing your apps to target different audiences. But Android makes it fairly simple to add all these different configurations to your project through resource directories.
If you want to try increasing localization support further in your apps, look at providing alternative resources for other aspects of your app, such as custom layouts, multiple currencies or audio files. As a starting point for adding audio files, read the documentation on MediaPlayer.
The skills you have gained in this tutorial for making new resource files with qualifying identifiers can help you make your app more robust in other areas, too. Try playing around with providing other qualifiers for resource files, such as screen orientation or size. You’ll find a list of all available qualifiers here.
Hopefully, this tutorial covered enough to get you started on the road to supporting as many users as possible in ways that appeal to them. If you have any comments or questions, use the forum below.
Источник