好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot REST国际化的实现代码

本指南将向您展示如何轻松只需几个简单的步骤即可实现spring boot应用的 国际化 ,从而总是在一个地方处理语言环境问题。

我们将讨论如何在现有的spring boot项目中添加国际化。当您处理应该为来自不同国家/地区的用户提供不同语言服务的项目时,app国际化的问题变得很常见。比如,你需要向中国用户提供中文回复信息,并向法国用户提供法语信息,那么让我们来看看如何在spring boot中实现它。

让我们使用spring initializer创建项目 ,这使得项目的创建更容易。选择web,security,jpa,actuator,devtools等模块。

下载项目后,解压缩,并用打开intellij idea打开。

第一件事是创建customlocaleresolver类,它将负责定义用户的语言环境。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

@configuration

<b> public </b> <b> class </b> customlocaleresolver

        <b> extends </b> acceptheaderlocaleresolver

        implements webmvcconfigurer {

 

   list<locale> locales = arrays.aslist(

      <b> new </b> locale(<font> "en" </font><font>),

      <b> new </b> locale(</font><font> "fr" </font><font>));

 

   @override

   <b> public </b> locale resolvelocale(httpservletrequest request) {

    string headerlang = request.getheader(</font><font> "accept-language" </font><font>);

    <b> return </b> headerlang == <b> null </b> || headerlang.isempty()

       ? locale.getdefault()

       : locale.lookup(locale.languagerange.parse(headerlang), locales);

   }

 

   @bean

   <b> public </b> resourcebundlemessagesource messagesource() {

    resourcebundlemessagesource rs = <b> new </b> resourcebundlemessagesource();

    rs.setbasename(</font><font> "messages" </font><font>);

    rs.setdefaultencoding(</font><font> "utf-8" </font><font>);

    rs.setusecodeasdefaultmessage(<b> true </b>);

    <b> return </b> rs;

   }

}

</font>

这里告诉我们项目中支持2个语言环境:en和fr。在名为[ accept-language ] 的http的header中传递语言环境。因此,如果header存在这个变量名且它不为空,我们将使用它的语言环境,否则 - 我们将使用默认语言环境,即en。

接下来让我们创建一个类,负责根据指定的语言环境选择正确的语言信息。我将其称为translator,它将有一个单独的方法,它将接受应翻译的信息代码。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@component

<b> public </b> <b> class </b> translator {

 

   <b> private </b> <b> static </b> resourcebundlemessagesource messagesource;

 

   @autowired

   translator(resourcebundlemessagesource messagesource) {

    translator.messagesource = messagesource;

   }

 

   <b> public </b> <b> static </b> string tolocale(string msgcode) {

    locale locale = localecontextholder.getlocale();

    <b> return </b> messagesource.getmessage(msg, <b> null </b>, locale);

   }

}

messagesource.getmessage(...)接受入参[msg]。但这并不是应该翻译的信息,它只是信息代码。现在我们还没有任何信息代码定义,所以现在定义信息代码。

在resources文件夹下,创建两个文件:messages.properties和messages_fr.properties。

这是messages.properties的内容:

?

1

2

3

hello=hello world!

 

welcome=welcome to this guide!

这里是messages_fr.properties的内容:

?

1

2

3

hello=bonjour le monde!

 

welcome=bienvenue dans ce guide!

在这里我们已经定义了我们的消息代码。他们是[ hellp ]和[ welcome ]。现在你可以指导我们应该将哪些代码传递给tolocale(string msgcode)方法,这样才能根据用户的语言环境获取适当的消息。

可能最后一步是创建简单的控制器,让我们将它命名为maincontroller,它只有一个端点,它将接受消息代码,我们将其作为请求参数传递给http请求。

?

1

2

3

4

5

6

7

8

9

@restcontroller

@requestmapping (value =[/ api])

<b> public </b> <b> class </b> maincontroller {

 

@getmapping ()

<b> public </b> string getmessage( @requestparam ([msg])string msg){

<b> return </b> translator。tolocale(msg) ;

}

}

现在已经完成!

使用curl发出简单的请求:

curl -x get -h "accept-language: fr" 'http://localhost:8080/api?msg-welcome'

这个将返回法语的welcome信息:

bienvenue dans ce guide!

再发出请求:

curl -x get -h "accept-language: en" 'http://localhost:8080/api?msg-welcome'

这个将返回英语的welcome信息:

welcome to this guide!

正如你看到:响应会根据请求中传递的[ accept-language ]标头的值而有所不同。这样,我们不需要检查每个控制器方法中请求中传递的内容,然后将其进一步传递给服务层。我们现在可以在一个单独的地方执行此操作,即customlocaleresolver类。

源码: github

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://HdhCmsTestjdon测试数据/50541

查看更多关于Spring Boot REST国际化的实现代码的详细内容...

  阅读:14次