[導(dǎo)入][AppFuse] AppFuse使用手記--資源文件中文問題(五) [原]
AppFuse使用“mvn native2ascii:native2ascii”進(jìn)行文件編碼格式的轉(zhuǎn)換。但是轉(zhuǎn)換后,界面顯示仍然是亂碼。打開ApplicationResources_zh_CN.properties文件,里面是中文貌似已經(jīng)轉(zhuǎn)換為ascii。但是仔細(xì)看,ApplicationResources_zh_CN.properties文件仍然是GB18030。
在網(wǎng)上找了找,如果在%PROJECT_HOME%運行:
AppFuse的pom.xml里明明制定了UTF-8,為什么文件就變成了GB18030呢?
查看JDK的native2ascii文檔:
原來執(zhí)行native2ascii時,默認(rèn)取系統(tǒng)的file.encoding做為編碼格式。試著輸出file.encoding,果然是GB18030。難道是AppFuse執(zhí)行“native2ascii:native2ascii”時,沒有制定編碼?
反編譯repository\org\codehaus\mojo\native2ascii-maven-plugin\1.0-alpha-1\native2ascii-maven-plugin-1.0-alpha-1.jar!\org\codehaus\mojo\native2ascii\Native2AsciiMojo.class
修改pom.xml,嘗試增加encoding節(jié)點
另外pom.xml只配置了ApplicationResources_zh*.properties的字符轉(zhuǎn)換,AppFuse還有displaytag_zh.properties也需要轉(zhuǎn)換,我們可以把pom.xml里ApplicationResources_zh*.properties都換成*_zh*.properties就可以了。
文章來源: http://heyday.blogcn.com/diary,15137426.shtml
在網(wǎng)上找了找,如果在%PROJECT_HOME%運行:
native2ascii -encoding UTF-8 .\src\main\resources\ApplicationResources_zh_CN.properties .\src\main\webapp\WEB-INF\classes\ApplicationResources_zh_CN.properties
則顯示正常。AppFuse的pom.xml里明明制定了UTF-8,為什么文件就變成了GB18030呢?
1
<plugin>
2
<groupId>org.codehaus.mojo</groupId>
3
<artifactId>native2ascii-maven-plugin</artifactId>
4
<version>1.0-alpha-1</version>
5
<configuration>
6
<dest>target/classes</dest>
7
<src>src/main/resources</src>
8
</configuration>
9
<executions>
10
<execution>
11
<id>native2ascii-utf8</id>
12
<goals>
13
<goal>native2ascii</goal>
14
</goals>
15
<configuration>
16
<encoding>UTF8</encoding>
17
<includes>
18
ApplicationResources_ko.properties,
19
ApplicationResources_no.properties,
20
ApplicationResources_tr.properties,
21
ApplicationResources_zh*.properties
22
</includes>
23
</configuration>
24
</execution>
25
<execution>
26
<id>native2ascii-8859_1</id>
27
<goals>
28
<goal>native2ascii</goal>
29
</goals>
30
<configuration>
31
<encoding>8859_1</encoding>
32
<includes>
33
ApplicationResources_de.properties,
34
ApplicationResources_fr.properties,
35
ApplicationResources_nl.properties,
36
ApplicationResources_pt*.properties
37
</includes>
38
</configuration>
39
</execution>
40
</executions>
41
</plugin>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

查看JDK的native2ascii文檔:
-encoding encoding_name
Specify the encoding name which is used by the conversion procedure. The default encoding is taken from System property file.encoding. The encoding_name string must be taken from the first column of the table of supported encodings in the Supported Encodings document.
Specify the encoding name which is used by the conversion procedure. The default encoding is taken from System property file.encoding. The encoding_name string must be taken from the first column of the table of supported encodings in the Supported Encodings document.
原來執(zhí)行native2ascii時,默認(rèn)取系統(tǒng)的file.encoding做為編碼格式。試著輸出file.encoding,果然是GB18030。難道是AppFuse執(zhí)行“native2ascii:native2ascii”時,沒有制定編碼?
反編譯repository\org\codehaus\mojo\native2ascii-maven-plugin\1.0-alpha-1\native2ascii-maven-plugin-1.0-alpha-1.jar!\org\codehaus\mojo\native2ascii\Native2AsciiMojo.class
1
protected void executeAnt()
2
{
3
Project antProject = new Project();
4
antProject.setName("native2ascii");
5
Native2Ascii antTask = new Native2Ascii();
6
antTask.setProject(antProject);
7
antTask.setSrc(src);
8
antTask.setDest(dest);
9
antTask.setEncoding(encoding);
10
antTask.setExt(ext);
11
antTask.setExcludes(excludes);
12
antTask.setIncludes(includes);
13
antTask.execute();
14
}
代碼也很簡單,調(diào)用Ant的API實現(xiàn)轉(zhuǎn)換。估計Ant也不會出現(xiàn)什么問題,為什么會使用系統(tǒng)默認(rèn)的編碼呢?試著輸出encoding,發(fā)現(xiàn)encoding居然為null!會不會是從pom.xml里沒有取到encoding的設(shè)置呢?
2



3

4

5

6

7

8

9

10

11

12

13

14

修改pom.xml,嘗試增加encoding節(jié)點
1
<groupId>org.codehaus.mojo</groupId>
2
<artifactId>native2ascii-maven-plugin</artifactId>
3
<version>1.0-alpha-1</version>
4
<configuration>
5
<dest>target/classes</dest>
6
<src>src/main/resources</src>
7
<encoding>UTF-8</encoding>
8
</configuration>
再運行“mvn native2ascii:native2ascii”,發(fā)現(xiàn)encoding有值了,但是生成的文件編碼還是GB18030,不過運行服務(wù),中文亂碼沒有了。看來和文件編碼沒有太大關(guān)系,應(yīng)該是native2ascii轉(zhuǎn)換時采用默認(rèn)的GB18030編碼轉(zhuǎn)換導(dǎo)致的。
2

3

4

5

6

7

8

另外pom.xml只配置了ApplicationResources_zh*.properties的字符轉(zhuǎn)換,AppFuse還有displaytag_zh.properties也需要轉(zhuǎn)換,我們可以把pom.xml里ApplicationResources_zh*.properties都換成*_zh*.properties就可以了。
1
<configuration>
2
<encoding>UTF8</encoding>
3
<includes>
4
ApplicationResources_ko.properties,
5
ApplicationResources_no.properties,
6
ApplicationResources_tr.properties,
7
*_zh*.properties
8
</includes>
9
</configuration>
10
11


12
13
<resource>
14
<directory>src/main/resources</directory>
15
<excludes>
16
<exclude>ApplicationResources_de.properties</exclude>
17
<exclude>ApplicationResources_fr.properties</exclude>
18
<exclude>ApplicationResources_ko.properties</exclude>
19
<exclude>ApplicationResources_nl.properties</exclude>
20
<exclude>ApplicationResources_no.properties</exclude>
21
<exclude>ApplicationResources_pt*.properties</exclude>
22
<exclude>ApplicationResources_tr.properties</exclude>
23
<exclude>*_zh*.properties</exclude>
24
<exclude>applicationContext-resources.xml</exclude>
25
<exclude>struts.xml</exclude>
26
</excludes>
27
<filtering>true</filtering>
28
</resource>

2

3

4

5

6

7

8

9

10

11



12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

文章來源: http://heyday.blogcn.com/diary,15137426.shtml