實現localresolve介面,定義messageservice bean
/** * 自定義messages服務,指定國際化配置路徑
* @return
*/@bean
(name =
"messagesource"
)public resourcebundlemessagesource getmessageresource()
}
@component
public
class
multilanguageutils
/** * 獲取國際化messages
* @param key
* @return
*/public
static string getmessage
(string key)
/** * 獲取國際化messages
* @param key messages.properties的key
* @param args messages的佔位符{}
* @return
*/public
static string getmessage
(string key, object[
] args)
}
在resources下新建多語言配置檔案,這裡我配置的是message和i18n/message,如果未配置messagesource bean,預設是resource下面的messages
核心就是messagesource.getmessage(key, args, key, localecontextholder.getlocale())
local通過localcontext獲取,localcontext又由localecontexthold獲取
frameworkservlet
localecontext localecontext = buildlocalecontext(request);
dispatcherservlet
重寫buildlocalecontext(request)
public
class
dispatcherservlet
else
}}
locale locale = localecontextholder.getlocale()
public
class
localecontextholder
public
static localecontext getlocalecontext()
return localecontext;
}public
static locale getlocale
(@nullable localecontext localecontext)
}return
(defaultlocale != null ? defaultlocale : locale.
getdefault()
);}}
在springboot中,借助約定大於配置的思想,通過預設配置實現springboot的國際化預設配置。通過messagesourceautoconfiguration實現。
在沒有配置messagesource bean時,預設生成乙個messagesource bean
@configuration
(proxybeanmethods =
false
)// public static final string message_source_bean_name = "messagesource";
@conditionalo****singbean
@autoconfigureorder
(ordered.highest_precedence)
@conditional
(resourcebundlecondition.
class
)@enableconfigurationproperties
public
class
messagesourceautoconfiguration
;@bean
@configurationproperties
(prefix =
"spring.messages"
)public messagesourceproperties messagesourceproperties()
@bean
public messagesource messagesource
(messagesourceproperties properties)
if(properties.
getencoding()
!= null)
messagesource.
setfallbacktosystemlocale
(properties.
isfallbacktosystemlocale()
);duration cacheduration = properties.
getcacheduration()
;if(cacheduration != null)
messagesource.
setalwaysusemessageformat
(properties.
isalwaysusemessageformat()
);messagesource.
setusecodeasdefaultmessage
(properties.
isusecodeasdefaultmessage()
);return messagesource;
}protected
static
class
resourcebundlecondition
extends
springbootcondition
return outcome;
}private conditionoutcome getmatchoutcomeforbasename
(conditioncontext context, string basename)}}
return conditionoutcome.
nomatch
(message.
didnotfind
("bundle with basename "
+ basename)
.atall()
);}private resource[
]getresources
(classloader classloader, string name)
catch
(exception ex)}}
}public
class
messagesourceproperties
github原始碼: springboot多語言國際化配置
locale l new locale en us en表示國家,us表示語言 需要乙個常量檔案,或者列舉檔案關聯properties檔案中的key 列舉 addfail 10000,msg.addfail properties msg.addfail add failed 工具類方法實現方法 an...
android國際化(多語言)
1.很大程度上,為什麼我們能如此方便的實現國際化 解析度匹配等?主要就是得益於 android 中這種獨特的資源管理方式。程式設計師的 可以不直接和資源發生關係。android 中,我們通常通過 r 檔案提供的索引來間接的引用某乙個資源。而如何維護資源索引和真正的資源之間的關係,這個活,卻是 and...
iOS APP多語言開發(國際化)
static dispatch once t oncetoken dispatch once oncetoken,objc setassociatedobject nsbundle mainbundle bundle,language nsbundle bundlewithpath nsbundle...