1. 在跟目錄底下建立i18n的package
  2. 把properties檔放進去
  3. 在xml檔進行設定(用工具避免錯字)
  4. 寫出相關方法
  5. 在測試檔呼叫測試





properties檔內容:

program.error=\u8A0A{3}\u606F{1}\u932F{2}\u8AA4{0}


xml檔:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"

    default-init-method="xxx" default-destroy-method="yyy">

    <bean id="messageSource"

    class="org.springframework.context.support.ResourceBundleMessageSource">

       <property name="basename" value="i18n.Message"></property>

    </bean>

   

<bean id="demoService1" class="model.DemoService">

<property name="bundle" ref="messageSource"></property>

</bean>

   

   

</beans>


package model;

 

import java.util.Locale;

 

import org.springframework.context.MessageSource;

 

public class DemoService {

   

 

    private MessageSource bundle;

    public MessageSource getBundle() {

       return bundle;

    }

 

    public void setBundle(MessageSource bundle) {

       this.bundle = bundle;

    }

     

     public void method1(){

        String m1 = bundle.getMessage("program.error", new String[] {"0","1","2","3"},Locale.TAIWAN);

        System.out.println("m1="+m1);

        String m2 = bundle.getMessage("program.error", null,Locale.US);

        System.out.println("m2="+m2);

     }

     

}


package org.iiiedu.samuel.ProjSpringDemo;

 

import model.DemoBean;

import model.DemoService;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class TestDemoBean {

 

    public static void main(String[] args) {

       ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");

      

       DemoService demoService1 = (DemoService) context.getBean("demoService1");

       demoService1.method1();

      

   

    }

}




m1=訊3息1錯2誤0
m2=英文訊息錯誤

創作者介紹
創作者 乙方 的頭像
乙方

學習筆記專區

乙方 發表在 痞客邦 留言(0) 人氣()