{"id":5797,"date":"2024-04-15T23:54:27","date_gmt":"2024-04-15T15:54:27","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5797"},"modified":"2024-04-15T23:54:27","modified_gmt":"2024-04-15T15:54:27","slug":"%e7%ac%ac%e4%b9%9d%e7%ab%a0%e3%80%81%e5%9b%bd%e9%99%85%e5%8c%96%ef%bc%9ai18n","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/15\/%e7%ac%ac%e4%b9%9d%e7%ab%a0%e3%80%81%e5%9b%bd%e9%99%85%e5%8c%96%ef%bc%9ai18n\/","title":{"rendered":"\u7b2c\u4e5d\u7ae0\u3001\u56fd\u9645\u5316\uff1ai18n"},"content":{"rendered":"<h1>\u7b2c\u4e5d\u7ae0\u3001\u56fd\u9645\u5316\uff1ai18n<\/h1>\n<h2>1\u3001\u6982\u8ff0<\/h2>\n<p>1\u3001\u4e86\u89e3\uff1a\u56fd\u9645\u5316\u4e5f\u79f0\u4f5ci18n\uff0c\u5176\u6765\u6e90\u662f\u82f1\u6587\u5355\u8bcd internationalization\u7684\u9996\u672b\u5b57\u7b26i\u548cn\uff0c18\u4e3a\u4e2d\u95f4\u7684\u5b57\u7b26\u6570\u3002\u7531\u4e8e\u8f6f\u4ef6\u53d1\u884c\u53ef\u80fd\u9762\u5411\u591a\u4e2a\u56fd\u5bb6\uff0c\u5bf9\u4e8e\u4e0d\u540c\u56fd\u5bb6\u7684\u7528\u6237\uff0c\u8f6f\u4ef6\u663e\u793a\u4e0d\u540c\u8bed\u8a00\u7684\u8fc7\u7a0b\u5c31\u662f\u56fd\u9645\u5316\u3002<\/p>\n<p>2\u3001\u4e3b\u8981\u662f\u8f6f\u4ef6\u4e2d\u7684\u56fd\u9645\u5316\u662f<strong>\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u5b9e\u73b0<\/strong><\/p>\n<p>3\u3001\u6709\u51e0\u4e2a\u7248\u672c\u7684\u8bed\u8a00\uff0c\u5c31\u8981\u6709\u51e0\u4e2a\u914d\u7f6e\u6587\u4ef6<\/p>\n<h2>2\u3001java\u56fd\u9645\u5316<\/h2>\n<h3>1\u3001\u5185\u5bb9<\/h3>\n<p>1\u3001Java\u81ea\u8eab\u662f\u652f\u6301\u56fd\u9645\u5316\u7684\uff0cjava.util.Locale\u7528\u4e8e\u6307\u5b9a\u5f53\u524d\u7528\u6237\u6240\u5c5e\u7684\u8bed\u8a00\u73af\u5883\u7b49\u4fe1<\/p>\n<p>2\u3001java.util.ResourceBundle\u7528\u4e8e\u67e5\u627e\u7ed1\u5b9a\u5bf9\u5e94\u7684\u8d44\u6e90\u6587\u4ef6\u3002<\/p>\n<p>3\u3001Locale\u5305\u542b\u4e86language\u4fe1\u606f\u548ccountry\u4fe1\u606f\uff0cLocale\u521b\u5efa\u9ed8\u8ba4locale\u5bf9\u8c61\u65f6\u4f7f\u7528\u7684\u9759\u6001\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * This method must be called only for creating the Locale.*\n * constants due to making shortcuts.\n *\/\nprivate static Locale createConstant(String lang, String country) {\n    BaseLocale base = BaseLocale.createInstance(lang, country);\n    return getInstance(base, null);\n}<\/code><\/pre>\n<h3>2\u3001\u914d\u7f6e\u6587\u4ef6\u547d\u540d\u89c4\u5219<\/h3>\n<h4>1\u3001basename_language_country.propertie<\/h4>\n<p>1\u3001\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0a\u7684\u547d\u540d\u89c4\u5219\uff0cjava\u624d\u4f1a\u8bc6\u522b<\/p>\n<p>2\u3001\u5176\u4e2d\uff0cbasename\u662f\u5fc5\u987b\u7684\uff0c\u8bed\u8a00\u548c\u56fd\u5bb6\u662f\u53ef\u9009\u7684\u3002\u8fd9\u91cc\u5b58\u5728\u4e00\u4e2a\u4f18\u5148\u7ea7\u6982\u5ff5\uff0c\u5982\u679c\u540c\u65f6\u63d0\u4f9b\u4e86messages.properties\u548cmessages_zh_CN.propertes\u4e24\u4e2a\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>3\u3001\u5982\u679c\u63d0\u4f9b\u7684locale\u7b26\u5408en_CN\uff0c\u90a3\u4e48\u4f18\u5148\u67e5\u627emessages_en_CN.propertes\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>4\u3001\u5982\u679c\u6ca1\u67e5\u627e\u5230\uff0c\u518d\u67e5\u627emessages.properties\u914d\u7f6e\u6587\u4ef6\u3002\u6700\u540e\uff0c\u63d0\u793a\u4e0b\uff0c\u6240\u6709\u7684\u914d\u7f6e\u6587\u4ef6\u5fc5\u987b\u653e\u5728classpath\u4e2d\uff0c\u4e00\u822c\u653e\u5728resources\u76ee\u5f55\u4e0b<\/p>\n<h3>2\u3001\u7528Java\u6765\u5b9e\u73b0<\/h3>\n<h4>1\u3001ResourceI8n.java<\/h4>\n<pre><code class=\"language-java\">package com.atqt.spring6.i18n;\n\nimport org.junit.jupiter.api.Test;\n\nimport java.util.Locale;\nimport java.util.ResourceBundle;\n\npublic class ResourceI18n {\n    public static void main(String[] args) {\n        System.out.println(ResourceBundle.getBundle(&quot;messages&quot;,\n                new Locale(&quot;en&quot;,&quot;GB&quot;)).getString(&quot;test&quot;));\n\n        System.out.println(ResourceBundle.getBundle(&quot;messages&quot;,\n                new Locale(&quot;zh&quot;,&quot;CN&quot;)).getString(&quot;test&quot;));\n    }\n}\n<\/code><\/pre>\n<h4>2\u3001messages_en_GB.properties<\/h4>\n<pre><code class=\"language-properties\">test=GB test<\/code><\/pre>\n<h4>3\u3001messages_zh_CN.properties<\/h4>\n<pre><code class=\"language-properties\">test=China test<\/code><\/pre>\n<h2>3\u3001Spring6\u56fd\u9645\u5316<\/h2>\n<h3>1\u3001MessageSource\u63a5\u53e3<\/h3>\n<p>1\u3001Spring\u4e2d\u56fd\u9645\u5316\u662f\u901a\u8fc7\u8fd9\u4e2aMessageSource\u63a5\u53e3\u6765\u5b9e\u73b0\u7684<\/p>\n<h4>1\u3001\u5e38\u7528\u5b9e\u73b0\u7c7b<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u540d<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ResourceBundleMessageSource<\/td>\n<td>\u8fd9\u4e2a\u662f\u57fa\u4e8eJava\u7684ResourceBundle\u57fa\u7840\u7c7b\u5b9e\u73b0\uff0c\u5141\u8bb8\u4ec5\u901a\u8fc7\u8d44\u6e90\u540d\u52a0\u8f7d\u56fd\u9645\u5316\u8d44\u6e90<\/td>\n<\/tr>\n<tr>\n<td>ReloadableResourceBundleMessageSource<\/td>\n<td>\u8fd9\u4e2a\u529f\u80fd\u548c\u7b2c\u4e00\u4e2a\u7c7b\u7684\u529f\u80fd\u7c7b\u4f3c\uff0c\u591a\u4e86\u5b9a\u65f6\u5237\u65b0\u529f\u80fd\uff0c\u5141\u8bb8\u5728\u4e0d\u91cd\u542f\u7cfb\u7edf\u7684\u60c5\u51b5\u4e0b\uff0c\u66f4\u65b0\u8d44\u6e90\u7684\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>StaticMessageSource<\/td>\n<td>\u5b83\u5141\u8bb8\u901a\u8fc7\u7f16\u7a0b\u7684\u65b9\u5f0f\u63d0\u4f9b\u56fd\u9645\u5316\u4fe1\u606f\uff0c\u4e00\u4f1a\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u6765\u5b9e\u73b0db\u4e2d\u5b58\u50a8\u56fd\u9645\u5316\u4fe1\u606f\u7684\u529f\u80fd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>4\u3001\u4f7f\u7528Spring\u56fd\u9645\u5316<\/h2>\n<h3>1\u3001\u521b\u5efa\u8d44\u6e90\u6587\u4ef6<\/h3>\n<p>1\u3001\u521b\u5efa\u56fd\u9645\u5316\u6587\u4ef6\u547d\u540d\u683c\u5f0f<\/p>\n<pre><code>\u57fa\u672c\u540d\u79f0 _ \u8bed\u8a00 _ \u56fd\u5bb6.properties\n{0},{1}\u8fd9\u6837\u5185\u5bb9\uff0c\u5c31\u662f\u52a8\u6001\u53c2\u6570<\/code><\/pre>\n<h3>2\u3001\u7b2c\u4e00\u6b65<\/h3>\n<p>1\u3001\u521b\u5efaatguigu_en_US.properties<\/p>\n<pre><code>www.atqt.com=welcome {0},\u65f6\u95f4:{1}<\/code><\/pre>\n<p>2\u3001\u521b\u5efaatguigu_zh_CN.properties<\/p>\n<pre><code>www.atqt.com=\u6b22\u8fce {0},\u65f6\u95f4:{1}<\/code><\/pre>\n<h3>3\u3001\u7b2c\u4e8c\u6b65<\/h3>\n<p>1\u3001\u521b\u5efaspring\u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6eMessageSource<\/p>\n<p>2\u3001\u5c31\u662f\u521b\u5efabean.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd&quot;&gt;\n\n    &lt;bean id=&quot;messageSource&quot;\n          class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;\n        &lt;property name=&quot;basenames&quot;&gt;\n            &lt;list&gt;\n                &lt;value&gt;atqt&lt;\/value&gt;\n            &lt;\/list&gt;\n        &lt;\/property&gt;\n        &lt;property name=&quot;defaultEncoding&quot;&gt;\n            &lt;value&gt;utf-8&lt;\/value&gt;\n        &lt;\/property&gt;\n    &lt;\/bean&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<h3>4\u3001\u7b2c\u4e09\u6b65<\/h3>\n<p>1\u3001\u521b\u5efa\u6d4b\u8bd5\u7c7bResourceI18n.java<\/p>\n<pre><code class=\"language-java\">package com.atqt.spring6.springI18n;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\nimport java.util.Date;\nimport java.util.Locale;\nimport java.util.ResourceBundle;\n\npublic class ResourceI18n {\n    public static void main(String[] args) {\n        ApplicationContext context = new ClassPathXmlApplicationContext(&quot;bean.xml&quot;);\n\n        \/\/\u4f20\u9012\u52a8\u6001\u53c2\u6570\uff0c\u4f7f\u7528\u6570\u7ec4\u5f62\u5f0f\u5bf9\u5e94{0} {1}\u987a\u5e8f\n        Object[] objs = new Object[]{&quot;atqt&quot;,new Date().toString()};\n\n        \/\/www.atguigu.com\u4e3a\u8d44\u6e90\u6587\u4ef6\u7684key\u503c,\n        \/\/objs\u4e3a\u8d44\u6e90\u6587\u4ef6value\u503c\u6240\u9700\u8981\u7684\u53c2\u6570,Local.CHINA\u4e3a\u56fd\u9645\u5316\u4e3a\u8bed\u8a00\n        String str=context.getMessage(&quot;www.atqt.com&quot;, objs, Locale.CHINA);\n        System.out.println(str);\n    }\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e5d\u7ae0\u3001\u56fd\u9645\u5316\uff1ai18n 1\u3001\u6982\u8ff0 1\u3001\u4e86\u89e3\uff1a\u56fd\u9645\u5316\u4e5f\u79f0\u4f5ci18n\uff0c\u5176\u6765\u6e90\u662f\u82f1\u6587\u5355\u8bcd internationa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-5797","post","type-post","status-publish","format-standard","hentry","category-spring-spring"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5797"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5797\/revisions"}],"predecessor-version":[{"id":5798,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5797\/revisions\/5798"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5797"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}