[Java] internationalization

background

  • A user program can be used simultaneously in different countries, can greatly reduce development and maintenance costs of the program
  • Google、Youtube、Facebook、Twitter、Ins
  • First, determine the core structure of the project, and which the industry background, after the completion of the core business to consider international
  • Language is not in your code, in a configuration file
  • Select the user according to their profiles, according to the user to achieve dynamic switching languages
  • Needs to be different in different countries of text configuration information, the relevant data is loaded by the configuration file when the program runs
  • How to get the current dynamic country and the language in which the user

 

 Locale class

  • java.util.Locale: can be controlled class national and language switching
    • public static final Locale CHINA: China region
    • public static final Locale US: United States region
    • public Locale (String language, String country): Set the language and city
    • public static Locale getDefault (): Get the current system in which the environment
    • Locale class object is instantiated, the need for language coding and cities
    • IE> Tools> Internet Options> Appearance> Language> Set Language Preferences> Add Language (win7 ago) 

And language acquisition area code

1 public class Locale_Demo {
2     public static void main(String[] args) {
3         Locale loc = Locale.CHINA;
4         System.out.println(loc);
5     }
6 }
View Code

Region and language

1 public class Locale_Demo {
2     public static void main(String[] args) {
3         Locale loc = new Locale("zh","CN");
4         System.out.println(loc);
5     }
6 }
View Code

Get the current default locale and language

1 public class Locale_Demo {
2     public static void main(String[] args) {
3         Locale loc = Locale.getDefault();
4         System.out.println(loc);
5     }
6 }
View Code

 

ResourceBundle class

  • Locale achieve positioning, resource files to achieve text information stored
  • International text stored in the property file (or resource file)
    • Properties file (or resource file) must be saved in "* .properties" suffix file
    • When a resource file is saved in the form of storage class, in the CLASSPATH (consider the package)
    • When locating resource files, all content using "key = value" in the form of declaration
    • Resource file comments with the "#" at the beginning
    • CLASSPATH resource files in the directory can be identified, and can set the package name
    • Eclipse resource files displayed in Chinese, JDK 1.8 before the need for transcoding, the need to configure additional separate plug (JDK 1.9 and default UTF-8)
    • IDEA using UTF-8 encoding
    • No space before and after the equal sign
  • Resource reading
    • java.util.ResourceBundle: achieve reading the specified resource under CLASSPATH
    • public static final ResourceBundle getBundle (String baseName): Gets the specified resource object
    • public static final ResourceBundle getBundle (String baseName, Locale locale): access to resources in the designated target area
    • public final String getString (String key): reading the key value according to the
    • Environment in which the language is Chinese, if no Locale configuration, the default text messages loaded Chinese (zh_CN) resource content to read, if need be assigned by Locale
    • If you find the specified key does not exist, it will automatically find the common resource file, if the key word there is specific language will be loaded resource information file when reading data

 1 import java.util.Locale;
 2 import java.util.ResourceBundle;
 3     
 4 public class Locale_Demo {
 5     public static void main(String[] args) {
 6         Locale loc = Locale.US;
 7 
 8         ResourceBundle resouceBundle = ResourceBundle.getBundle("a.Message",loc);
 9         System.out.println(resouceBundle.getString("welcome.info"));
10         System.out.println(resouceBundle.getString("teacher.info"));
11     }
12 }
View Code

 

Formatting text

  • Dynamic text processing
  • Modify the resource file
    • login.info=success,welcome {0}, Please Access {1} Web Site.
  • java.text.MessageFormat
 1 import java.text.MessageFormat;
 2 import java.util.Locale;
 3 import java.util.ResourceBundle;
 4     
 5 public class Locale_Demo {
 6     public static void main(String[] args) {
 7         Locale loc = Locale.US;
 8 
 9         ResourceBundle resourceBundle = ResourceBundle.getBundle("a.Message",loc);
10         String value = resourceBundle.getString("login.info");
11         System.out.println(MessageFormat.format(value, "aaa","bbb"));
12     }
13 }
View Code

 

Digital Format

  • By international NumberFormat class for data display
  • java.text.NumberFormat
    • public static final NumberFormat getInstance (): get the NumberFormat instance a default Locale
    • public static final NumberFormat getPercentInstance (Locale inLocale): to give a specific example of Locale NumberFormat
    • public static final NumberFormat getPercentInstance (): to give a calculated percentage of the instance default Locale
    • public static final NumberFormat getPercentInstance (Locale inLocale): to give an example to calculate the percentage of the specified Locale
    • public static final NumberFormat getCurrencyInstance (): Get the default Locale instance of currency
    • public static final NumberFormat getCurrencyInstance (Locale inLocale): Gets Money instance of the specified Locale
  • java.text.DecimalFormat: default format, each of the three plus ","
  • Directly through the sub-class instantiation wrong, the best method is to replace the previous code NumberFormat class provides a static method of the internal processing is completed
  • If only captures the object class NumberFormat digital format default format, relying directly NumberFormat class can be completed, but if it is to use some special methods, it must be disposed class object instance DecimalFormat
  • There are some important numbers formatting tags in class DecimalFormat
  • Few developers rarely need special treatment, because we used the NumberFormat getInstance () method in the given format format is already internationally accepted

Digital Object

 1 import java.math.RoundingMode;
 2 import java.text.DecimalFormat;
 3 import java.text.NumberFormat;
 4 
 5 public class Locale_Demo {
 6     public static void main(String[] args) {
 7         DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance();
 8         numberFormat.applyPattern("####,####,####.000"); // 保留三位小数
 9         numberFormat.setRoundingMode(RoundingMode.DOWN); // 不进位
10         numberFormat.setPositivePrefix ( "year revenue water:"); // increase the front explained 
. 11          numberFormat.setMinimumFractionDigits (. 5);   // Reserved Five-digit decimal 
12 is          System.out.println (NumberFormat.format (2826548.264785 ));
 13 is      }
 14 }
View Code

The percentage of target

 1 import java.text.DecimalFormat;
 2 import java.text.NumberFormat;
 3 
 4 public class Locale_Demo {
 5     public static void main(String[] args) {
 6         NumberFormat numberFormat = (DecimalFormat) NumberFormat.getPercentInstance();
 7         System.out.println(numberFormat.format(0.9854623));
 8         // 自定义处理,强制向下转型
 9         DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
10         decimalFormat.setMinimumFractionDigits(5);
11         System.out.println(decimalFormat.format(0.9878546));
12     }
13 }
View Code

Currency Objects

1 import java.text.NumberFormat;
2 import java.util.Locale;
3 
4 public class Locale_Demo {
5     public static void main(String[] args) {
6         NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
7         System.out.println(numberFormat.format(789.46));
8     }
9 }
View Code

 

reference

https://zhidao.baidu.com/question/414732825.html

 

Guess you like

Origin www.cnblogs.com/cxc1357/p/12452666.html