Java 程序支持国际化和本地化的发布,通常我们通过书写 Java Properties 文件来实现,原则上,书写都很随意,但其实还是有一些规则在里面,只是平时没太在意,特总结如下:
常用规范
The properties files read by Nucleus must follow a format that is recognized by the classjava.util.Properties
. The rules for the format are as follows:
-
Entries are generally expected to be a single line of the form, one of the following:
-
propertyName
=
propertyValue
-
propertyName
:
propertyValue
-
-
White space that appears between the property name and property value is ignored, so the following are equivalent.
name=Stephen
name = Stephen
White space at the beginning of the line is also ignored.
-
Lines that start with the comment characters
!
or#
are ignored. Blank lines are also ignored. -
The property value is generally terminated by the end of the line. White space following the property value is not ignored, and is treated as part of the property value.
-
A property value can span several lines if each line is terminated by a backslash (‘\’) character. For example:
targetCities=\
Detroit,\
Chicago,\
Los AngelesThis is equivalent to
targetCities=Detroit,Chicago,Los Angeles
(white space at the beginning of lines is ignored). -
The characters newline, carriage
\n
,\r
, and\t
, respectively. -
The backslash character must be escaped as a double backslash. For example:
path=c:\\docs\\doc1
Note: It is not necessary to escape backslashes in property values when you use the ATG Control Center Components window; the ATG Control Center handles the escape characters automatically.
-
UNICODE characters can be entered as they are in a Java program, using the
\u
prefix. For example,\u002c
.
Nucleus properties files also use several special properties types, indicated by a leading $ character.
特殊变量
Nucleus properties files use several special properties that are indicated by a leading $ character:
| The component’s Java class. |
| The scope of the component (global, session, or request). The default value is global. See theComponent Scopes section in this chapter. |
A brief description of the component, for display in the ATG Control Center Components task areas. |
You can set the $scope
and $description
properties
实例特殊说明
Each line in a .properties file normally stores a single property. Several formats are possible for each line, includingkey=value
, key = value
, key:value
, and key value
. Note that single-quotes or double-quotes are considered part of the string. Trailing space is significant and presumed to be trimmed as required by the consumer.
Comment lines in .properties files are denoted by thenumber sign (#) or the exclamation mark (!) as the first non blank character, in which all remaining text on that line is ignored. The backwards slash is used to escape a character. An example of a properties file is provided below.
# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
# The key and element characters #, !, =, and : are written with
# a preceding backslash to ensure that they are properly loaded.
website = http\://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009
In the example above, website would be a key, and its corresponding value would behttp://en.wikipedia.org/. While the number sign (#) and the exclamation mark (!) marks text as comments, it has no effect when it is part of a property. Thus, the keymessage has the value Welcome to Wikipedia! and not Welcome to Wikipedia. Note also that all of the whitespace in front of Wikipedia! is excluded completely.