User Manual 3.3 Errors management and internationalization : Différence entre versions

De Wiki
Aller à : navigation, rechercher
Ligne 13 : Ligne 13 :
 
Here is a use case of  the Locale object :
 
Here is a use case of  the Locale object :
  
(% style="color: rgb(64, 128, 128);" %)~/~/ French locale
+
<syntaxhighlight lang="java">
Locale france <pre style="color: rgb(102, 102, 102);">=</pre> (% style="font-weight: bold; color: rgb(0, 128, 0);" %)new(%%) Locale<pre style="color: rgb(102, 102, 102);">(<pre style="color: rgb(186, 33, 33);">"fr"</pre><pre style="color: rgb(102, 102, 102);">,</pre> <pre style="color: rgb(186, 33, 33);">"FR"</pre>(% style="color: rgb(102, 102, 102);" %));</pre>
+
// French locale
\\(% style="color: rgb(64, 128, 128);" %)~/~/ French locale language
+
Locale france = new Locale("fr", "FR");
(%%)Locale french <pre style="color: rgb(102, 102, 102);">=</pre> (% style="font-weight: bold; color: rgb(0, 128, 0);" %)new(%%) Locale<pre style="color: rgb(102, 102, 102);">(<pre style="color: rgb(186, 33, 33);">"fr"</pre>(% style="color: rgb(102, 102, 102);" %));</pre>
+
\\(% style="color: rgb(64, 128, 128);" %)~/~/ Belgium locale
+
(%%)Locale belgium <pre style="color: rgb(102, 102, 102);">=</pre> (% style="font-weight: bold; color: rgb(0, 128, 0);" %)new(%%) Locale<pre style="color: rgb(102, 102, 102);">(<pre style="color: rgb(186, 33, 33);">"fr"</pre><pre style="color: rgb(102, 102, 102);">,</pre> </pre><pre style="color: rgb(186, 33, 33);">"BE"</pre>(% style="color: rgb(102, 102, 102);" %));
+
  
 +
// French locale language
 +
Locale french = new Locale("fr");
 +
 +
// Belgium locale
 +
Locale belgium = new Locale("fr", "BE");
 +
</syntaxhighlight>
  
 
Moreover, some locales are defined as constants of the class. This is the case for French language (Locale.FRENCH), French locale (Locale.FRENCH) or English language (Locale.ENGLISH).
 
Moreover, some locales are defined as constants of the class. This is the case for French language (Locale.FRENCH), French locale (Locale.FRENCH) or English language (Locale.ENGLISH).
Ligne 31 : Ligne 34 :
 
For example, in the following code :
 
For example, in the following code :
  
 
+
<syntaxhighlight lang="java">
 
+
ResourceBundle bundle = ResourceBundle.getBundle("message");
ResourceBundle bundle <pre style="color: rgb(102, 102, 102);">= ResourceBundle</pre><pre style="color: rgb(102, 102, 102);">.</pre><pre style="color: rgb(125, 144, 41);">getBundle</pre><pre style="color: rgb(102, 102, 102);">(</pre><pre style="color: rgb(186, 33, 33);">"message"</pre>(% style="color: rgb(102, 102, 102);" %));
+
</syntaxhighlight>
 
+
  
 
If the current locale (for example the default language of the operating system) is fr_FR, here is the order of the files research :
 
If the current locale (for example the default language of the operating system) is fr_FR, here is the order of the files research :
  
1. message_fr_FR.propertie,
+
3.1. message_fr_FR.propertie,<br>
1. message_fr.properties,
+
3.2. message_fr.properties,<br>
1. message.properties.
+
3.3. message.properties.
  
 
It is possible to get back a particular ResourceBundle by specifying explicitly the locale to be used :
 
It is possible to get back a particular ResourceBundle by specifying explicitly the locale to be used :
  
 
+
<syntaxhighlight lang="java">
 
+
ResourceBundle bundle = ResourceBundle.getBundle("message", Locale.ENGLISH);
ResourceBundle bundle <pre style="color: rgb(102, 102, 102);">= ResourceBundle<pre style="color: rgb(102, 102, 102);">.</pre><pre style="color: rgb(125, 144, 41);">getBundle</pre><pre style="color: rgb(102, 102, 102);">(</pre><pre style="color: rgb(186, 33, 33);">"message"</pre><pre style="color: rgb(102, 102, 102);">,</pre> Locale</pre><pre style="color: rgb(102, 102, 102);">.</pre><pre style="color: rgb(125, 144, 41);">ENGLISH</pre>(% style="color: rgb(102, 102, 102);" %));
+
</syntaxhighlight>
 
+
  
 
Afterwards, in order to get an internationalized message, for example a message associated to the key “HelloWorld” in the properties file, the method getString(String key) must be used :
 
Afterwards, in order to get an internationalized message, for example a message associated to the key “HelloWorld” in the properties file, the method getString(String key) must be used :
  
String message <pre style="color: rgb(102, 102, 102);">=  bundle</pre><pre style="color: rgb(102, 102, 102);">.</pre><pre style="color: rgb(125, 144, 41);">getString</pre><pre style="color: rgb(102, 102, 102);">(</pre><pre style="color: rgb(186, 33, 33);">"HelloWorld"</pre>(% style="color: rgb(102, 102, 102);" %));
+
<syntaxhighlight lang="java">
 +
String message =  bundle.getString("HelloWorld");
 +
</syntaxhighlight>
  
 
=== Javadoc ===
 
=== Javadoc ===
{{specialInclusion prefix=$theme_sub section="Javadoc"/}}
 
  
 
=== Links ===
 
=== Links ===
{{specialInclusion prefix=$theme_sub section="Links"/}}
 
  
 
=== Useful Documents ===
 
=== Useful Documents ===
{{specialInclusion prefix=$theme_sub section="UsefulDocs"/}}
 
  
 
=== Package Overview ===
 
=== Package Overview ===
{{specialInclusion prefix=$theme_sub section="PkgOverview"/}}
 
  
 
== Features Description ==
 
== Features Description ==
Ligne 79 : Ligne 78 :
 
Example (see the Commons Math library for a complete example) :
 
Example (see the Commons Math library for a complete example) :
  
 
+
<syntaxhighlight lang="java">
(((
+
public class MyFutileException extends Exception implements ExceptionContextProvider {
(% style="font-weight: bold; color: rgb(0, 128, 0);" %)public (% style="font-weight: bold; color: rgb(0, 128, 0);" %)class(%%) (% style="font-weight: bold; color: rgb(0, 0, 255);" %)MyFutileException(%%) (% style="font-weight: bold; color: rgb(0, 128, 0);" %)extends(%%) Exception (% style="font-weight: bold; color: rgb(0, 128, 0);" %)implements(%%) ExceptionContextProvider <pre style="color: rgb(102, 102, 102);">{</pre>
+
   ...
   <pre style="color: rgb(102, 102, 102);">...</pre>
+
   private ExceptionContext exceptionContext ; /** compulsory variable */
   (% style="font-weight: bold; color: rgb(0, 128, 0);" %)private(%%) ExceptionContext exceptionContext <pre style="color: rgb(102, 102, 102);">;</pre> <pre style="color: rgb(64, 128, 128);">/~*~* compulsory variable*/</pre>
+
   ...
   <pre style="color: rgb(102, 102, 102);">...</pre>
+
   MyFutileException(final Locale locale,final Object ... args) {
   MyFutileException<pre style="color: rgb(102, 102, 102);">((% style="font-weight: bold; color: rgb(0, 128, 0);" %)final</pre> Locale locale<pre style="color: rgb(102, 102, 102);">,(% style="font-weight: bold; color: rgb(0, 128, 0);" %)final</pre> Object <pre style="color: rgb(102, 102, 102);">...</pre> args<pre style="color: rgb(102, 102, 102);">)</pre> <pre style="color: rgb(102, 102, 102);">{</pre>
+
       exceptionContext.addMessage(locale,PatriusMessages.PDB_FUTILE_EXCEPTION,args)
       exceptionContext<pre style="color: rgb(102, 102, 102);">.<pre style="color: rgb(125, 144, 41);">addMessage</pre><pre style="color: rgb(102, 102, 102);">(</pre>locale<pre style="color: rgb(102, 102, 102);">,</pre>PatriusMessages<pre style="color: rgb(102, 102, 102);">.</pre><pre style="color: rgb(125, 144, 41);">PDB_FUTILE_EXCEPTION</pre>(% style="color: rgb(102, 102, 102);" %),</pre>args<pre style="color: rgb(102, 102, 102);">)</pre>
+
   }
   <pre style="color: rgb(102, 102, 102);">}</pre>
+
   ...
   <pre style="color: rgb(102, 102, 102);">...</pre>
+
}
(% style="color: rgb(102, 102, 102);" %)}
+
</syntaxhighlight>
)))
+
  
 
In this case, a unique identifier as well as the original text of the message and its translations are also needed in the translation files.
 
In this case, a unique identifier as well as the original text of the message and its translations are also needed in the translation files.
  
 
== Getting Started ==
 
== Getting Started ==
{{specialInclusion prefix=$theme_sub section="GettingStarted"/}}
 
  
 
== Contents ==
 
== Contents ==
 
=== Interfaces ===
 
=== Interfaces ===
{{specialInclusion prefix=$theme_sub section="Interfaces"/}}
 
  
 
=== Classes ===
 
=== Classes ===
{{specialInclusion prefix=$theme_sub section="Classes"/}}
 
  
 
== Tutorials ==
 
== Tutorials ==
 
=== Tutorial 1 ===
 
=== Tutorial 1 ===
{{specialInclusion prefix=$theme_sub section="Tuto1"/}}
 
  
 
=== Tutorial 2 ===
 
=== Tutorial 2 ===
{{specialInclusion prefix=$theme_sub section="Tuto2"/}}
 
  
 
== [[File:lightBulb.png]] Tips & Tricks ==
 
== [[File:lightBulb.png]] Tips & Tricks ==
{{specialInclusion prefix=$theme_sub section="Tips"/}}
 

Version du 16 mars 2018 à 08:51


Introduction

Scope

A locale is a set of 2 parameters, one to define the user’s language, the other one to define the country. A locale is used to identify a country, a language or a dialect.

The language parameter is composed by 2 lower cases whose list is defined by the ISO 639-2 Langage Code List [R1]. For instance, “fr” means French whereas “en” means English.

The country parameter is composed by 2 upper cases whose list is defined by the ISO 3166 French Country Names and Code Elements [R2]. France is designated by “FR”, Belgium by “BE” and United Kingdom by “GB”.

The association of a language parameter and a country parameter leads to a full definition of all language varieties. As a result, the fr_FR locale points out French language spoken in France whereas the fr_BE locale points out French language spoken in Belgium.

Here is a use case of the Locale object :

// French locale
Locale france = new Locale("fr", "FR");
 
// French locale language 
Locale french = new Locale("fr");
 
// Belgium locale 
Locale belgium = new Locale("fr", "BE");

Moreover, some locales are defined as constants of the class. This is the case for French language (Locale.FRENCH), French locale (Locale.FRENCH) or English language (Locale.ENGLISH).

The second Java component i18n is the ResourceBundle. It is in charge of retrieving a given locale. However, the ResourceBundle is an abstract class, therefore a concrete implementation of the ResoureBundle, namely the PropertyResourceBundle, is used. This implementation is based on a basic file name, “properties”, and, for a given locale, it goes for the translation if it is available. To do that, it checks if a properties file, whose name is <basic file name>_<language code>_<country code>, exists.

If necessary it goes for a translation that is more common, that is to say only based on the language and whose properties file would be named <basic file name>_<language code>.

If the need still arises, it goes for the basic file to retrieve a translation.

For example, in the following code :

ResourceBundle bundle = ResourceBundle.getBundle("message");

If the current locale (for example the default language of the operating system) is fr_FR, here is the order of the files research :

3.1. message_fr_FR.propertie,
3.2. message_fr.properties,
3.3. message.properties.

It is possible to get back a particular ResourceBundle by specifying explicitly the locale to be used :

ResourceBundle bundle = ResourceBundle.getBundle("message", Locale.ENGLISH);

Afterwards, in order to get an internationalized message, for example a message associated to the key “HelloWorld” in the properties file, the method getString(String key) must be used :

String message =  bundle.getString("HelloWorld");

Javadoc

Links

Useful Documents

Package Overview

Features Description

Exception and internationalization

As indicated in the SRS document (requirements PBD-LOG_850), we should create an error message only when it is the only option i.e. nor Java basic errors neither Commons Math or Orekit errors suit. In this case, it is mandatory to add a particular error message, the procedure is the following one :

  • Find a unique identifier (one key, see the following chapter for the definition rule) for the message and a short and relevant text,
  • Modify the PatriusMessages class and add it to the enumeration, in US English which is the default language, e;g. FOO_MESSAGE("foo message for testing purpose") or BAR_MESSAGE("another foo message for testing purpose {0} {1}") with 2 expected arguments,
  • Add this message to the translation files PatriusMessages_<language>.properties, one per language, in the directory …/src/main/resources/META-INF/localization, with the following form “key=text”. The text is translated in the required language, one has to be carful with the arguments location if there are some of them (they can be inverted).

N.B. : For languages which use accents like French, each accented character has to be written with a specific coding. The list of the characters and their equivalent coding is available on the following web site : http://www.utf8-chartable.de/

If a class is necessary for a specific message type, it has to inherit from the one of the following java classes : Exception or RuntimeException.

Example (see the Commons Math library for a complete example) :

public class MyFutileException extends Exception implements ExceptionContextProvider {
   ...
   private ExceptionContext exceptionContext ; /** compulsory variable */
   ...
   MyFutileException(final Locale locale,final Object ... args) {
      exceptionContext.addMessage(locale,PatriusMessages.PDB_FUTILE_EXCEPTION,args)
   }
   ...
}

In this case, a unique identifier as well as the original text of the message and its translations are also needed in the translation files.

Getting Started

Contents

Interfaces

Classes

Tutorials

Tutorial 1

Tutorial 2

LightBulb.png Tips & Tricks