Add support for different languages in the program for BlackBerry
- Tutorial
In this article I will tell you how to add support for various languages in the program written in the previous article .
In our project, create a new package with the name “test.myfirstapp.lang” and create a file in it: Language.rrh
The name “Language” for the file is chosen arbitrarily, for clarity. The .rrh extension means that the file is the header of a language resource.
When creating Language.rrh, the file Language.rrc will automatically be created.
Language.rrc contains the body of the main language resource. There can be several files with the extension .rrc in the resource set. One for each supported language. The header file (.rrh) in the resource is present in only one instance.
Language resources are used to translate the program interface, its name and description into other languages. In the application descriptor, you can specify the language resource that will be used to display the name of the program and its description.

If the program contains language resources, then when changing the system language on the device, the program language will automatically change if the current system language contains the corresponding resource in the program.
If, however, there is no resource corresponding to the system language in the program, then the main string resource, the so-called “Root locale”, will be used.
There is one subtlety, the identification of which I spent a lot of energy when I just started writing for BlackBerry.
At first, I couldn’t even manage to run examples from the JDE suite. I did not change anything in them, just installed JDE, opened examples in it and started compilation. After which I received an error message about the assembly of the project, from which it was impossible to understand what was the reason.
The documentation did not say anything about this anywhere. The support of Research In Motion, to which I turned, met my question with sincere amazement, and I was told that everything should work, and they can’t make a mind of what it is that even their sample projects are not going to me. In short, they could not help me.
As a result, through trial and error, I came to the conclusion that in order to correctly compile a project containing language resources in the form of .rrh and .rrc files in the operating system settings for non-unicode programs, a language other than Russian should be installed. In my case, the options worked with English and German.
If, however, “Russian” is set as the language for non-unicode programs, then when compiling .rrc and .rrh files, files with incorrect characters in the names will be generated, which in turn will lead to a project verification error and a build error.
Later, this problem migrated from the Java Development Environment to the Eclipse JDE plugin, where it lives and lives today. And the newcomer to programming for BlackBerry is completely unclear what the matter is and why even the examples from RIM are not compiled.
Let's get back to our program. Open the “Language.rrh” file and in the window to the right, by clicking on the “Add key” button, create three key-value pairs.
NOTE_APP_TITLE - for the name of the program
NOTE_APP_DESCRIPTION - for a brief description of the program
NOTE_HELLO_WORLD - with the welcome text
Now we have the main language resource with three string values.
Now add an additional file to the language resource to display the program interface in Russian.
The name of this additional file should begin in the same way as the name of the main one, but in addition to it, a language code is assigned.
In our case, the name of this file will be “Language_ru.rrc”
As a result, at the very bottom of the window with the contents of the language resource, an additional tab “ru” will appear next to the “Root” tab.

Having opened which, we will indicate the values for all keys in Russian.

When compiling the project, the .rrc and .rrh files turn into a regular java interface, the name of which is formed from the name of the main language resource (in this case, “Language”) and the word “Resource”, and which must be implemented by the class that uses language resources . This generated interface is not visible explicitly in the project, but it is invisibly present there and it must be implemented in order to access the constants declared inside this interface.
So, make changes to the MyAppScreen class.
By default, the simulator does not include support for the Russian language. We do this to see the changed program in operation.
To add Russian language support to the device simulator (in this case, Storm 9550 and API 5.0.0), open the 9550.xml file in the Eclipse internal folder “plugins \ net.rim.ejde.componentpack5.0.0_5.0.0.25 \ components \ simulator "and add the line to it:
net.rim.blackberry.lang.ru
The figure below shows the changes in the xml-file of the simulator configuration:

That's it, now you can run the application for execution.
The animation below shows the process of launching our program first with English as the system language, after which the language changes to Russian in the settings and the program starts already with Russian as the system language.

You can add other languages to the simulator. The following is a list of xml expressions that must be added to the simulator configuration file to enable support for the corresponding language.
French :net.rim.blackberry.lang.fr
German :net.rim.blackberry.lang.de
Italian :net.rim.blackberry.lang.it
Spanish :net.rim.blackberry.lang.es
English (UK) :net.rim.blackberry.lang.en_GB
English (USA) :net.rim.blackberry.lang.en_US
English (Holland) :net.rim.blackberry.lang.en_NL
Portuguese (Brazil) :net.rim.blackberry.lang.pt_BR
Polish :net.rim.blackberry.lang.pl
In our project, create a new package with the name “test.myfirstapp.lang” and create a file in it: Language.rrh
The name “Language” for the file is chosen arbitrarily, for clarity. The .rrh extension means that the file is the header of a language resource.
When creating Language.rrh, the file Language.rrc will automatically be created.
Language.rrc contains the body of the main language resource. There can be several files with the extension .rrc in the resource set. One for each supported language. The header file (.rrh) in the resource is present in only one instance.
Language resources are used to translate the program interface, its name and description into other languages. In the application descriptor, you can specify the language resource that will be used to display the name of the program and its description.

If the program contains language resources, then when changing the system language on the device, the program language will automatically change if the current system language contains the corresponding resource in the program.
If, however, there is no resource corresponding to the system language in the program, then the main string resource, the so-called “Root locale”, will be used.
There is one subtlety, the identification of which I spent a lot of energy when I just started writing for BlackBerry.
At first, I couldn’t even manage to run examples from the JDE suite. I did not change anything in them, just installed JDE, opened examples in it and started compilation. After which I received an error message about the assembly of the project, from which it was impossible to understand what was the reason.
The documentation did not say anything about this anywhere. The support of Research In Motion, to which I turned, met my question with sincere amazement, and I was told that everything should work, and they can’t make a mind of what it is that even their sample projects are not going to me. In short, they could not help me.
As a result, through trial and error, I came to the conclusion that in order to correctly compile a project containing language resources in the form of .rrh and .rrc files in the operating system settings for non-unicode programs, a language other than Russian should be installed. In my case, the options worked with English and German.
If, however, “Russian” is set as the language for non-unicode programs, then when compiling .rrc and .rrh files, files with incorrect characters in the names will be generated, which in turn will lead to a project verification error and a build error.
Later, this problem migrated from the Java Development Environment to the Eclipse JDE plugin, where it lives and lives today. And the newcomer to programming for BlackBerry is completely unclear what the matter is and why even the examples from RIM are not compiled.
Let's get back to our program. Open the “Language.rrh” file and in the window to the right, by clicking on the “Add key” button, create three key-value pairs.
NOTE_APP_TITLE - for the name of the program
NOTE_APP_DESCRIPTION - for a brief description of the program
NOTE_HELLO_WORLD - with the welcome text
Now we have the main language resource with three string values.
Now add an additional file to the language resource to display the program interface in Russian.
The name of this additional file should begin in the same way as the name of the main one, but in addition to it, a language code is assigned.
In our case, the name of this file will be “Language_ru.rrc”
As a result, at the very bottom of the window with the contents of the language resource, an additional tab “ru” will appear next to the “Root” tab.

Having opened which, we will indicate the values for all keys in Russian.

When compiling the project, the .rrc and .rrh files turn into a regular java interface, the name of which is formed from the name of the main language resource (in this case, “Language”) and the word “Resource”, and which must be implemented by the class that uses language resources . This generated interface is not visible explicitly in the project, but it is invisibly present there and it must be implemented in order to access the constants declared inside this interface.
So, make changes to the MyAppScreen class.
package test.myfirstapp.view;
import test.myfirstapp.lang.LanguageResource;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* При компиляции rrc и rrh файлов автоматически создаётся
* файл LanguageResource.java в котором пары ключ-значение
* объявлены константами.
* Да, вы не ошиблись, это известный anti-pattern "Constant Interface",
* и RIM применяет его для поддержки многоязычности.
*/
public class MyAppScreen extends MainScreen implements LanguageResource {
/**
* BUNDLE_ID и BUNDLE_NAME - автоматически созданные константы в так-же
* автоматически сгенерированном интерфейсе LanguageResource.java
*/
private static ResourceBundle resources =
ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
public MyAppScreen() {
// формируем меню по-умолчанию, с пунктом для закрытия приложения
// для меню по-умолчанию нет необходимости задавать ресурс вручную
// текст для пункта меню будет взят из ресурсов операционной системы
super(DEFAULT_MENU | DEFAULT_CLOSE);
// устанавливаем заголовок в название программы
setTitle(resources.getString(NOTE_APP_TITLE));
// добавляем нередактируемое поле с надписью
this.add(new LabelField(resources.getString(NOTE_HELLO_WORLD)));
}
// обработка попытки закрытия экрана
public boolean onClose() {
// при закрытии экрана завершаем работу приложения
System.exit(0);
// true подтверждает, что экран будет закрыт.
return true;
}
}
By default, the simulator does not include support for the Russian language. We do this to see the changed program in operation.
To add Russian language support to the device simulator (in this case, Storm 9550 and API 5.0.0), open the 9550.xml file in the Eclipse internal folder “plugins \ net.rim.ejde.componentpack5.0.0_5.0.0.25 \ components \ simulator "and add the line to it:
The figure below shows the changes in the xml-file of the simulator configuration:

That's it, now you can run the application for execution.
The animation below shows the process of launching our program first with English as the system language, after which the language changes to Russian in the settings and the program starts already with Russian as the system language.

You can add other languages to the simulator. The following is a list of xml expressions that must be added to the simulator configuration file to enable support for the corresponding language.
French :
German :
Italian :
Spanish :
English (UK) :
English (USA) :
English (Holland) :
Portuguese (Brazil) :
Polish :