Java 程序支持国际化和本地化的发布,通常我们通过书写 Java Properties 文件来实现,原则上,书写都很随意,但其实还是有一些规则在里面,只是平时没太在意,特总结如下:
常用规范
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.
A property value can span several lines if each line is terminated by a backslash (‘\’) character. For example:
targetCities=\
Detroit,\
Chicago,\
Los Angeles
This is equivalent to targetCities=Detroit,Chicago,Los Angeles (white space at the beginning of lines is ignored).
The characters newline, carriage return, and tab can be inserted with characters \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.
Nucleus properties files also use several special properties types, indicated by a leading $ character.
特殊变量
$class
The component’s Java class.
$scope
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.