freemarker 国际化

freemarker支持多语言国际化,只要把模板名称按照java资源文件的写法就可以了,也就是name_语言_国家地区.ftl 如果找不到对应的语言,就会用默
认语言的模板。那么就需要定义很多的ftl文件,如果要是改动模版的话,就比较费功夫,

所以,西面这种是 freemarker直接使用 源文件   进行多语言国际化



  publicclass FreeMarkerTest   {    publicstaticvoid main(String[] args)  {         FreeMarkerTest test  = new   FreeMarkerTest();         test.sayHello( " hermit " ,Locale.CHINA);         test.sayHello( " hermit " ,Locale.ENGLISH);     }    publicvoid sayHello(String name,Locale locale)  {         Configuration freemarkerCfg  =   new   Configuration();         freemarkerCfg.setClassForTemplateLoading( this .getClass(),  " / " );         freemarkerCfg.setEncoding(Locale.getDefault(),  " UTF-8 " );        Template template;        Locale.setDefault(Locale.ENGLISH);        try  {            template  = freemarkerCfg.getTemplate( " Hello.ftl " );            template.setEncoding( " UTF-8 " );             HashMap root  =  newHashMap();             root.put( " user " , name);             ResourceBundle RESOURCE_BUNDLE  = ResourceBundle.getBundle( " ApplicationResources " ,locale);             ResourceBundleModel rsbm  = new  ResourceBundleModel(RESOURCE_BUNDLE, new   BeansWrapper());             root.put( " bundle " , rsbm);             StringWriter writer  = newStringWriter();             template.process(root, writer);             System.out.println(writer.toString());         }  catch (Exception e)  {             e.printStackTrace();         }  }   }  





你可能感兴趣的:(freemarker)