{"id":899,"date":"2023-03-11T06:58:05","date_gmt":"2023-03-10T22:58:05","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=899"},"modified":"2023-04-29T15:53:41","modified_gmt":"2023-04-29T07:53:41","slug":"spring-boot-internationalization-message-source-usage","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-internationalization-message-source-usage\/","title":{"rendered":"Spring Boot\u56fd\u9645\u5316MessageSource\u4f7f\u7528"},"content":{"rendered":"<h2>\u8d44\u6e90\u6587\u4ef6\u914d\u7f6e<\/h2>\n<p>resources\/i18n\/Messages.properties<\/p>\n<p><!-- more --><\/p>\n<pre><code>welcome.url=www.appblog.cn\nwelcome.msg=\u6b22\u8fce {0} \u5149\u4e34\uff01<\/code><\/pre>\n<p>resources\/i18n\/Messages_zh.properties<\/p>\n<pre><code>welcome.url=www.appblog.cn\nwelcome.msg=\u6b22\u8fce {0} \u5149\u4e34\uff01<\/code><\/pre>\n<p>resources\/i18n\/Messages_en.properties<\/p>\n<pre><code>welcome.url=www.appblog.cn\nwelcome.msg=Welcome {0}!<\/code><\/pre>\n<h2>application.properties\u4e2d\u914d\u7f6e\u8d44\u6e90\u6587\u4ef6\u8def\u5f84<\/h2>\n<pre><code>#\u8d44\u6e90\u6587\u4ef6\u7684\u540d\u79f0\nspring.messages.basename=i18n\/Messages,i18n\/Pages<\/code><\/pre>\n<h2>AbstractBaseController&#8211;\u8d44\u6e90\u6587\u4ef6messages\u8bfb\u53d6<\/h2>\n<pre><code class=\"language-java\">public abstract class AbstractBaseController {\n\n    @Resource\n    private MessageSource messageSource;\n\n    public String getMessage(String key, String ...args) {\n        return this.messageSource.getMessage(key, args, Locale.getDefault());\n    }\n\n    public String getMessage(String key, Locale locale, String ...args) {\n        return this.messageSource.getMessage(key, args, locale);\n    }\n\n}<\/code><\/pre>\n<h2>\u8d44\u6e90\u6587\u4ef6\u914d\u7f6e\u4f7f\u7528<\/h2>\n<pre><code class=\"language-java\">@RestController\npublic class MessageController extends AbstractBaseController {\n\n    @GetMapping(&quot;\/echo&quot;)\n    public String echo(String name) {\n        System.out.println(&quot;[***\u8bbf\u95ee\u5730\u5740***]: &quot; + super.getMessage(&quot;welcome.url&quot;));\n        return super.getMessage(&quot;welcome.msg&quot;, name);\n    }\n\n    @GetMapping(&quot;\/echo\/{locale}&quot;)\n    public String echo(@PathVariable(&quot;locale&quot;) String locale, String name) {\n        System.out.println(&quot;[***\u8bbf\u95ee\u5730\u5740***]: &quot; + super.getMessage(&quot;welcome.url&quot;, new Locale(locale)));\n        return super.getMessage(&quot;welcome.msg&quot;, new Locale(locale), name);\n    }\n\n}<\/code><\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<p>\u8bbf\u95ee\uff1a<code>http:\/\/127.0.0.1:8080\/echo?name=Joe.Ye<\/code>\uff0c\u8fd4\u56de\uff1a<code>\u6b22\u8fce Joe.Ye \u5149\u4e34\uff01<\/code><br \/>\n\u8bbf\u95ee\uff1a<code>http:\/\/127.0.0.1:8080\/echo\/zh?name=Joe.Ye<\/code>\uff0c\u8fd4\u56de\uff1a<code>\u6b22\u8fce Joe.Ye \u5149\u4e34\uff01<\/code><br \/>\n\u8bbf\u95ee\uff1a<code>http:\/\/127.0.0.1:8080\/echo\/en?name=Joe.Ye<\/code>\uff0c\u8fd4\u56de\uff1a<code>Welcome Joe.Ye!<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d44\u6e90\u6587\u4ef6\u914d\u7f6e resources\/i18n\/Messages.properties welcome.url=w [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[55],"class_list":["post-899","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-internationalization"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}