本指南將向您展示如何輕松只需幾個簡單的步驟即可實現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://www.jdon.com/50541