Internationalization and Localization
https://www.cnblogs.com/Evsward/archive/2017/11/25/localizer.html
1 2 3 4 5 6 7 8 9 10
| mvn clean package -pl war -am -DskipTests -Dfindbugs.skip
export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
mvn -DskipTests=true package hpi:run
mvn hudson-dev:run
|
https://jenkins.io/doc/developer/internationalization/i18n-source-code/
Jenkins uses the Localizer library and Maven plugin to implement internationalization.
localizer也是由kohsuke(Hudson&Jenkins的作者)写的一个属性文件本地化工具
1 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class LocaleTest extends TestCase {
public void testResourceBundleHolder() { ResourceBundleHolder h = new ResourceBundleHolder(LocaleTest.class); ResourceBundle rb = h.get(Locale.CANADA);
Enumeration<String> keys = rb.getKeys(); while (keys.hasMoreElements()){ String s = keys.nextElement(); System.out.println(s); } } public void testLocalizable() { ResourceBundleHolder holder = ResourceBundleHolder.get(LocalizableTest.class); ResourceBundle resourceBundle = holder.get(Locale.SIMPLIFIED_CHINESE); Enumeration<String> keys = resourceBundle.getKeys(); while (keys.hasMoreElements()){ String s = keys.nextElement(); System.out.println("key = " + s); } String arg1 = resourceBundle.getString("arg1"); System.out.println("arg1 = " + arg1); String val1 = MessageFormat.format(arg1, "格式化占位符"); System.out.println("val1 = " + val1);
Localizable localizable = new Localizable(holder, "arg", (Object) "my args"); String s = localizable.toString(Locale.SIMPLIFIED_CHINESE); assertEquals("参数: my args", s); } }
主要的就是 类 ResourceBundleHolder 和 Localizable Localizable类就是一个针对本地资源包国家地区数据的一个封装类,该类有一个ResourceBundleHolder的私有成员对象。然后比较重要的就是它的toString(Locale locale)方法: 先获取到对应的key的值,然后进行占位符的替换。 public String toString(Locale locale) { try { return MessageFormat.format(holder.get(locale).getString(key),(Object[])args); } catch (MissingResourceException e) { throw new RuntimeException("Failed to localize key="+key+",args="+ asList(args),e); } }
public String toString() { return toString(LocaleProvider.getLocale()); }
|