Java project Internationalization

Source: Internet
Author: User

In response to foreign and Chinese customers, the project requires text conversion through configuration. This process is very simple in Java,

 

The main user must use the following language to define a configuration file:

 

File Content:

Language = zh_cn

 

Put the edited International file under the Java package.

 

The content in resource. properties is:

Please note that a {1} accident occurred at {0} and the vehicle was driving slowly!

The Unicode encoding method. The conversion command is:

Native2ascii-encoding gb2312 resource. properties resource_cn.properties

 

You can use Java. Text. messageformat to render and modify texts in real time.

 

The Code is as follows:

 

Package app. Language. Global;

Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Text. messageformat;
Import java. util. locale;
Import java. util. properties;
Import java. util. resourcebundle;

Public class globallanguageimpl {

Static string defaultmessage;
Static locale currentlocale;
Static resourcebundle messages;
Static inputstream is;
Static Properties prop;

Public static void getprop (){

Is = globallanguageimpl. Class. getclassloader (). getresourceasstream ("app/Language/global/language.txt ");
Prop = new properties ();
Try {
Prop. Load (is );
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
If (is! = NULL)
Is. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Public static string formatmessage (string template, object [] Params, locale currentlocale ){
Messageformat formatter = new messageformat ("");
Formatter. setlocale (currentlocale );
Formatter. applypattern (Template );
String output = formatter. Format (Params );
Return output;
}

Public static void main (string [] ARGs ){
Getprop ();
String [] Resource = (string) prop. Get ("language"). Split ("_");
String Language = Resource [0]. tolowercase ();
String Country = Resource [1]. touppercase ();
Currentlocale = new locale (language, country );
Messages = resourcebundle. getbundle ("app. Language. Global. Resource", currentlocale );
Defaultmessage = messages. getstring ("default_message ");
Object [] Params = {"London golden road", "Road Works "};
String alertinfo = formatmessage (defaultmessage, Params, currentlocale );
System. Out. println ("========================" + language + "_" + country );
System. Out. println ("==========================" + alertinfo );
}
}

 

The generated result is:

=================================== Zh_cn
===================================== Please note that a road works accident occurs in London golden road, and the vehicle is driving slowly!

 

You can modify the content in the configuration file language.txt to change the Display language.

The customer can modify the configuration without re-compiling,

For example, the English country can be changed:

Language = en_us

You can restart the project.

In the process of internationalization, resourcebundle is responsible for searching for internationalized text files with compound conditions. The developer's job is to replace all text files displayed on the background and pages with messages. getstring ("default_message.

 

For JSP pages, the basic structure is as follows:

 

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Taglib prefix = "FMT" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<FMT: bundle basename = "app. Language. Global. Resource">

 

Writing Where text is needed:

<FMT: Message key = 'copyright'/>

<FMT: Message key = 'logout'/>

Very simple, pageencoding = "UTF-8" This can support both Chinese, can also support English, good choice, otherwise internationalization will no meaning.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.