"Java" uses Java to implement the internationalization of Java programs for configuration file operations

Source: Internet
Author: User
Tags i18n locale naming convention

Not only the famous Struts2, even a small Java program can be internationalized, it is not as much as the internet said, non-western characters, such as our Chinese, island of Japanese and so on, must use Native2ascii transcoding, Frank, Internationalized String configuration file Xx_ language. Propreties does not support non-western characters and can only be transcoded, but you can take advantage of the approach I described in the Java profile concept, Java Operations on profiles (click to open link), Use Java programs to complete this internationalized configuration file. Java will help you transcode automatically.


I. BASIC OBJECTIVES

Complete the internationalization of the famous Hello,world program


Some people see here started dissatisfied, this rotten program can write three Java files, change the output on the line, such as the Chinese Hello,world, not this diao kind?

public class HelloWorld {public static void main (string[] args) {System.out.println ("Hello, World");}}

Indeed, I also advocate this approach, how many languages of the national language to write how many Java, but if some brain-crippled boss asked you to use xx_ language. Propreties realize internationalization, say what project is easy to manage, have technical content, then you are ignorant.


Second, the basic idea

In fact, in xx_ language. Propreties to achieve internationalization is really not much good, how many languages of the country to write how many xx_ Language. propreties internationalization file, but it is not necessarily good where, that is, the value of the key file in the Key-value is changed to the language of different countries. Because the machine does not understand the human language at all, it is not possible to abstract into a circular structure, and ultimately to be translated by humans themselves.


At the same time, the xx_ must be Language. Propreties in the same folder as the. class that was compiled with the source Java program, you say you want to specify a folder specifically for such internationalized files, and then it is impossible to develop an internationalized path in Java, and in practice find that Java will not be able to find an internationalized file.

The nationalization document must strictly abide by the xx_ language. Propreties such a naming convention, xx you can easily change, for example, I changed to mess, that is, the meaning of message, Java Search for internationalization of the process of the document, such as, the process is from specific to abstract, first Simplified Chinese- > Chinese-Volkswagen language mess. The language of the letter is specific to the final appendix, you know a few of the door is very good.


Third, the production process

A Java file description is complete:

The output of the configuration file must be in the package import java.io.*;//contains the configuration file with the Internationalized import java.util.*;p ublic class i18n {public static void main (string[] args ) throws IOException {//The following is the process of Chinese internationalization properties Properties = new properties ();//Set Hello to the corresponding value for "Hello, world!" "Properties.setproperty (" Hello "," Hello, World! ") "), or//stored in the. \bin\ with the same directory, because if you use eclipse to compile this original file, the program's. Class will be generated into the bin//General bin is to store the executable file is the final program, publishers generally publish the folder under the bin content is OK, SRC is the source code of the//.class file can be run through the cmd inside the command, oh, do not use more eclipse forgot//If you are running with the cmd command, this. Class will be generated to the same directory as. Java// Anyway, it is saved two times simply to ensure that the mess_zh_cn.properties is stored in the same directory as the. class file of this program Properties.store (The New FileOutputStream ("Mess_zh_ Cn.properties ")," i18n ");p Roperties.store (New FileOutputStream (". \\bin\\mess_zh_CN.properties ")," i18n ");// The above is the process of Chinese internationalization//The following is the process of Japanese internationalization properties=new Properties ();p roperties.setproperty ("Hello", "こんにちは, World (せかい)!" ");p Roperties.store (New FileOutputStream (" Mess_ja_jp.properties ")," i18n ");p Roperties.store (New FileOutputStream ( ". \\bin\\mess_ja_JP.properties"), "i18n"), or//above は, Japanese National//The following is the process of English localization properties=new properties (); Properties.setproperTy ("Hello", "hello,world!"); Properties.store (New FileOutputStream ("Mess_en.properties"), "i18n");p Roperties.store (New FileOutputStream (". \ \") Bin\\mess_en.properties ")," i18n "),//above is internationilizing under english//to load the internationalized file, the first parameter is the internationalized file" Xx_ language. Propreties "The part of XX,///The second parameter is the language, Locale.getdefault () means to take the language of the operating system ResourceBundle resourcebundle=resourcebundle.getbundle (" mess ", Locale.getdefault ());//And then get Key:hello in the corresponding property value values, print it out System.out.println (resourcebundle.getstring (" Hello ") //locale.japan can forcibly specify the result of taking out Japanese internationalization, and then print it out resourcebundle=resourcebundle.getbundle ("mess", Locale.japan); System.out.println (resourcebundle.getstring ("Hello"));//English Empathy Resourcebundle=resourcebundle.getbundle ("mess", Locale.english); System.out.println (resourcebundle.getstring ("Hello"));}}
Everyone to observe, whether and I have the same feeling, rather than write three Java files, the output part of the inside to change the appropriate language appropriate, but, it is said that this can make the translator and the programmer to separate, translators can focus on translation, you have your program contained in the string output to Excel, They will be able to focus on the translation of Excel, you can write a full language of Excel read into the program, and then transform it into an international file, you will be able to complete the large program of the national language N version of the work.


Iv. Appendices


"Java" uses Java to implement the internationalization of Java programs for configuration file operations

Related Article

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.