wuheng 2 роки тому
батько
коміт
16fbc61084

+ 24 - 0
day04/pom.xml

@@ -54,6 +54,30 @@
             <version>5.3.22</version>
         </dependency>
 
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.13.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>5.1.9.RELEASE</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.7.32</version>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.2.10</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 18 - 0
day04/src/main/java/com/lovecoding/i18n/ConfigDemo.java

@@ -0,0 +1,18 @@
+package com.lovecoding.i18n;
+
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.support.ResourceBundleMessageSource;
+
+import java.util.Locale;
+
+public class ConfigDemo {
+    public static void main(String[] args) {
+        AnnotationConfigApplicationContext context =
+                new AnnotationConfigApplicationContext(SpringConfig.class);
+        Object[] o = new Object[]{
+                "Hello", "World"
+        };
+        String username = context.getMessage("username", o, Locale.CHINA);
+        System.out.println( username );
+    }
+}

+ 8 - 1
day04/src/main/java/com/lovecoding/i18n/I18nDemo.java

@@ -13,11 +13,18 @@ public class I18nDemo {
                 "i18n", Locale.CHINA
         );
         String username = bundle.getString("username");
-        //prioerties 文件是不允许 使用中文的, 因为编码写死在代码里了
+        //properties 文件是不允许 使用中文的, 因为编码写死在代码里了
         //我们可以通过自己转码,或者转成 unicon 编码 解决中文乱码问题
+        //I18N 文件命名规则  basename_语言简写_国家简写.properties
+
         String s = new String(username.getBytes("ISO-8859-1"), "UTF-8");
         System.out.println(  s  );
 
+        ResourceBundle bundle1 = ResourceBundle.getBundle(
+                "i18n", Locale.US
+        );
+        System.out.println( bundle1.getString("username") );
+
     }
 
 }

+ 22 - 0
day04/src/main/java/com/lovecoding/i18n/SpringConfig.java

@@ -0,0 +1,22 @@
+package com.lovecoding.i18n;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.support.ResourceBundleMessageSource;
+import org.springframework.stereotype.Component;
+
+@Configuration
+@Component
+@ComponentScan("com.lovecoding")
+public class SpringConfig {
+
+    @Bean("messageSource")
+    public ResourceBundleMessageSource getResourceBundleMessageSource(){
+        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
+        resourceBundleMessageSource.setBasename("i18n");
+        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
+        return resourceBundleMessageSource;
+    }
+
+}

+ 25 - 0
day04/src/main/java/com/lovecoding/i18n/SpringI18nDemo.java

@@ -0,0 +1,25 @@
+package com.lovecoding.i18n;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.context.support.ResourceBundleMessageSource;
+
+import java.util.Locale;
+
+public class SpringI18nDemo {
+
+    public static void main(String[] args) {
+
+        ClassPathXmlApplicationContext context =
+                new ClassPathXmlApplicationContext("bean.xml");
+        ResourceBundleMessageSource bean = context.getBean(ResourceBundleMessageSource.class);
+        Object[] o = new Object[]{
+                "Hello", "Learing Java"
+        };
+        String i18n = bean.getMessage("username", o, Locale.CHINA);
+        System.out.println( i18n );
+        String i18n1 = bean.getMessage("username", o, Locale.US);
+        System.out.println( i18n1 );
+
+    }
+
+}

+ 5 - 0
day04/src/main/java/com/lovecoding/junit/dao/Test.java

@@ -0,0 +1,5 @@
+package com.lovecoding.junit.dao;
+
+public interface Test {
+    void print();
+}

+ 19 - 0
day04/src/main/java/com/lovecoding/junit/dao/TestImpl.java

@@ -0,0 +1,19 @@
+package com.lovecoding.junit.dao;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class TestImpl implements Test{
+
+    private static final Logger logger = LoggerFactory.getLogger(TestImpl.class);
+
+    @Override
+    public void print() {
+
+        logger.error( "Test print Run" );
+        //System.out.println( "Test print Run" );
+
+    }
+}

+ 6 - 0
day04/src/main/java/com/lovecoding/junit/service/TestService.java

@@ -0,0 +1,6 @@
+package com.lovecoding.junit.service;
+
+public interface TestService {
+    void print();
+    void print2();
+}

+ 21 - 0
day04/src/main/java/com/lovecoding/junit/service/TestServiceImpl.java

@@ -0,0 +1,21 @@
+package com.lovecoding.junit.service;
+
+import com.lovecoding.junit.dao.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class TestServiceImpl implements TestService {
+
+    @Autowired
+    Test test;
+
+    @Override
+    public void print() {
+        test.print();
+    }
+
+    public void print2() {
+        test.print();
+    }
+}

+ 5 - 0
day04/src/main/resources/bean.xml

@@ -7,4 +7,9 @@
 
     </bean>
 
+    <bean id="resourceBundleMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
+        <property name="basename" value="i18n"></property>
+        <property name="defaultEncoding" value="UTF-8"></property>
+    </bean>
+
 </beans>

+ 1 - 0
day04/src/main/resources/i18n.properties

@@ -0,0 +1 @@
+username = {0} Tom {1}

+ 1 - 1
day04/src/main/resources/i18n_en_US.properties

@@ -1 +1 @@
-username = Tom
+username = {0} Tom {1}

+ 1 - 1
day04/src/main/resources/i18n_zh_CN.properties

@@ -1 +1 @@
-username = 张三
+username = {0} 张三  {1}

+ 13 - 0
day04/src/main/resources/log4j.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+    <property name="log.path" value="./" />
+    <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
+    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+    </appender>
+    <logger name="com.lovecoding" level="debug" additivity="false">
+        <appender-ref ref="console"/>
+    </logger>
+</configuration>

+ 28 - 0
day04/src/test/java/com/lovecoding/junit/service/TestServiceImplTest.java

@@ -0,0 +1,28 @@
+package com.lovecoding.junit.service;
+
+import com.lovecoding.i18n.SpringConfig;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.lang.annotation.Target;
+
+import static org.junit.Assert.*;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringJUnitConfig(SpringConfig.class)
+public class TestServiceImplTest {
+    @Autowired
+    TestService testService;
+    @Test
+    public void print() {
+        testService.print();
+    }
+
+    @Test
+    public void print2() {
+        testService.print2();
+    }
+}