Creating applications that understand the user in his native language is a critical stage of development under Android. An intuitive interface in Russian significantly increases audience confidence and improves user experience. Many novice developers are wondering how to Android Studio make the Russian language accessible to the end user without hardcoding lines inside the code.
The process of adapting an application to different languages โโis called localization. This is not just a translation of text, but the correct organization of project resources. If you simply write Russian words directly into the code or XML markup, your application will never be able to automatically switch to English or another language when you change phone settings. The correct approach requires the use of a resource system strings.xml.
In this article we will examine in detail the mechanism for creating localized strings. You will learn how to create a separate folder for Russian-language resources, how to correctly export values โโfrom an English template, and how to test the translation in an emulator or on a real device. Following these rules will make your product professional and ready for publication on Google Play.
Principles of working with string resources
The basis of any localization in the ecosystem Android SDK is a file strings.xml. By default, it is located in the directory app/src/main/res/values/. This is where all text constants that are displayed in the interface are stored. It is important to understand that this file serves as a base (default) set, which is usually written in English.
When the system Android starts the application, it automatically detects the language set in the device's operating system settings. If the language matches one of those available in the project, the corresponding set of strings is loaded. If there are no matches, the application uses the values โโfrom the folder values without suffixes. Therefore, it is critical to keep the base file up to date.
โ ๏ธ Warning: Never use hardcoded strings (Hardcode) in interface layouts, for example
android:text="Hello". This will make it impossible to translate the application in the future and will violate accessibility guidelines.
To add support for a new language, you must create a specialized directory with a language qualifier. In our case, to make the Russian language available, we will create a folder values-ru. The suffix -ru is a standard ISO 639-1 code for the Russian language. The project structure should remain logical and predictable for other developers.
Always use meaningful names for string resources, such as @string/login_button_text rather than @string/string1. This will simplify code support in the future.
Creating a localized folder for the Russian language
The process of adding a new language to Android Studio is maximally automated thanks to built-in tools. You don't need to create folders manually through Explorer, which eliminates the risk of misspelled names. The best way is to use the context menu in the project window.
Find the folder valueslocated inside the directory resin the project tree. Right-click on it and select New, and then Android Resource File. The create a new resource dialog box will open, where you will need to specify the resource type and availability qualifiers.
Leave the File name field to strings default. In the block Available qualifiers find in the list Locale and click the add button (right arrow). After that, in the menu that appears Language select Russian or enter the code ru. The system will automatically generate a folder name values-ru.
- ๐ Make sure that the new folder appears next to
valuesivalues-night. - ๐ The structure of the files inside the new folder must completely match the base one.
- ๐ Check that there are no extra spaces or incorrect encoding characters.
After pressing the button OK Android Studio will create a file strings.xml inside a new directory. Initially it will contain only the root tag resources. Your task is to fill it with the same keys as in the English file, but with translated values. This ensures that the application will not crash with an error Resources$NotFoundException when trying to find a missing key.
Copying and translating string resources
The most reliable way to fill a file values-ru/strings.xml is to copy the structure from the base file. Open the file values/strings.xml and copy all tags string with their attributes name. Paste them into the Russian file. Now you have a complete framework, where each English key corresponds to an empty or duplicate value.
Now you need to replace the contents of the tags with Russian text. Pay attention to special characters. If there are apostrophes in the text (for example, "Don't"), they must be escaped in XML with a backslash: Don\'t. Also, less than (<) and greater than (>) characters may conflict with XML syntax, so it is better to replace them with HTML entities or use CDATA sections.
Welcome to the application!
Network connection error
For long texts or paragraphs containing a lot of special markup, it is convenient to use the construction CDATA. It tells the parser to ignore the contents of the block as plain text. This is especially useful when laying out complex product descriptions or terms of a license agreement in Russian.
โ ๏ธ Attention: When translated, the length of lines may change significantly. Russian text is often 30-40% longer than English. Be sure to check how the text fits on the screen so you donโt end up with cropped text.
Donโt forget about line formatting. If the original uses placeholders like %1$s (for string substitution) or %d (for numbers), their order and quantity in the translation must be strictly observed. However, you can swap them around in a Russian sentence to maintain grammatical structure, since in the code you will pass arguments by index.
Using the Android Localizationer plugin
Manual copying and translation is effective for small projects, but becomes a chore when working with large applications. To optimize the process, there is an excellent plugin Android Localizationerthat significantly speeds up the work with translations in the environment Android Studio.
You can install the plugin through the menu File โ Settings โ Plugins (or Android Studio โ Preferences on macOS). In the search, enter "Android Localizationer" and click Install. After restarting the IDE, a new tab will appear in the bottom menu, opening a convenient table editor of all project lines.
The plugin interface is a table, where the rows are resource keys and the columns are languages. You can see the English original and Russian translation side by side. The plugin automatically highlights missing translations in red, which helps to quickly find gaps in localization before compilation.
โ๏ธ Setting up the Localizationer plugin
One of the most useful features is the ability to export and import strings in the format CSV or XLS. This allows you to send the table to a professional translator who does not own XMLand then easily return the finished file back to the project. The plugin itself will update the files in the folders values i values-ru.
Working with plurals and formatting
The Russian language has a complex system of declensions that differs from English. In English there are only singular and plural (one/other), while in Russian there are three forms to consider: one subject, several subjects (2-4) and many subjects (5-20, etc.). To do this, Android uses the tag plurals.
In the resource file, you must define three keys: one, few and many. If you skip any of them, the application may not work correctly or use the default value, which will hurt the user's hearing. An example of a correct implementation for displaying the number of notifications:
<plurals name="notifications_count">%d notification
%d notifications
%d notifications
</plurals>
Also worth mentioning formatting dates and currencies. Although the main strings are translated manually, for dynamic data it is better to use classes DateFormat and NumberFormat, which automatically adapt to the device locale. Do not try to hardcode date formats like "DD.MM.YYYY" in string resources, as this will break the logic of the application for other regions.
| Resource type | Description | Usage example |
|---|---|---|
string |
Normal text | Button name, title |
plurals |
Plural | "5 minutes ago","1 minute ago" |
string-array |
List of strings | Drop-down list elements (Spinner) |
formatted |
Text with parameters | Hi, %1$s! |
Why can't Google Translate be used directly in code?
Automatic translation often distorts the context of technical terms and does not take into account plural grammar, resulting in unreadable interface.
Testing and debugging of localization
After you have added the Russian language to the project, you need to make sure that it is correctly picked up by the system. The fastest way to check is to change the system language on the emulator or test device. Go to Android settings, select System โ Languages & input and add Russian, making it the main language.
When you restart the application, the interface should instantly change. If you see English text, check the logs via Logcat. A common mistake is a typo in the name of a resource key or a lack of translation for a specific key in the file values-ru. In this case, the system automatically rolls back to the default value from values.
To speed up testing, you can force the locale in the application code (for debugging only!) or use the developer settings in Android Studio itself. It is also useful to test the application on devices with different screen sizes, since long Russian words can break the layout on narrow displays.
โ ๏ธ Attention: Service interfaces and APIs may change. Always check the latest localization requirements in the official Google Developers documentation before release.
Don't forget to test the keyboard. When entering text in Russian into input fields (EditText), hints (Hints) and labels must also be in Russian. Check whether specific Cyrillic characters are displayed correctly and whether there are any problems with fonts if you use custom font types.
High-quality localization is not just translating words, but adapting the interface to the cultural and linguistic characteristics of the user, including line length and grammar.
Common errors and their solutions
One of the common problems is the โmixingโ of languages in one interface. This happens when some of the strings are transferred to resources, and some remain hardcoded in the Java/Kotlin code. The user sees a patchwork of English and Russian text, which looks unprofessional. Audit the project by searching for string literals.
Another error is incorrect file encoding. Although Android Studio uses UTF-8by default, when importing files from external sources (for example, from translators in Excel), the encoding may be lost. If instead of Russian letters you see krakozyabry, check the file encoding in the lower right corner of the editor and, if necessary, convert.
- ๐ซ Error: Using different keys for the same word on different screens.
- โ Solution: Create global strings for frequently used words (OK, Cancel, Back).
- โ ๏ธ Error: Ignoring context when translating polysemantic words.
Remember that some words in English may have the same meaning, but in Russian require different translations depending on the context (for example, โRunโ is โRunโ an application or โRunโ in a game). In such cases, create separate keys with clarifying suffixes, for example btn_run_app and action_run_character.
How to update the translation without releasing a new version of the application?
This is not possible using standard Android tools, since the resources are protected in the APK. Dynamically updating texts requires integration with the server and loading JSON files with translations on the fly.
In conclusion, proper localization lays the foundation for the international success of your application. By devoting time to a competent structure of resources and high-quality translation, you will save yourself from many problems when scaling the project to other markets in the future. Android Studio lays the foundation for the international success of your application. By devoting time to a competent structure of resources and high-quality translation, you will save yourself from many problems when scaling the project to other markets in the future.
Where are the strings.xml files physically stored after compilation?
In the compiled APK file, the resources are in binary format (resources.arsc). Text strings are retrieved by the system at application runtime depending on the selected locale.
Can you use HTML tags inside resource strings?
Yes, you can use a limited set of HTML tags (for example <b>, <i>, <u>) to format text. However, the < and > characters need to be escaped or CDATA used.
What to do if the translator sent a file in the .po format?
The .po format is used in the gettext system and is not directly supported by Android. You will need to convert it to XML format using utilities like po2xml or specialized online converters.
How to force the Russian language to be enabled only for tests?
In the Activity code you can call the method Locale.setDefault(new Locale("ru")) and recreate the resource configuration to call setContentView, but this should only be done in debug builds.
Why does the application crash on startup after translation?
Most likely, the Russian file strings.xml has an XML syntax error (unclosed tag, extra character) or there is no required key that is used in the code when the application starts.