js uses struts2 internationalization processing

Recently, js needs to use the internationalization method, but there is a problem with using struts internationalization tags in separate js, so I found a good way to deal with it: that is to define multiple internationalized js files.
[color=red]注:
In multiple js files, only the internationalized characters needed in js need to be defined. The rest need to be internationalized in the page, still in the properties file, you can use the internationalization tag of struts2 to get it[/color]




1. Get the current language in the interceptor and save it to the session,
//language
String request_locale = request.getParameter("request_locale");
logger.debug("---------request_locale:"+request_locale);
Locale locale = (Locale) request.getSession().getAttribute("i18_local");
if(null !=request_locale)
{
	if(request_locale.equalsIgnoreCase("en_US"))
	{
		locale = Locale.US;
	}
	else if(request_locale.equalsIgnoreCase("zh_TW"))
	{
		local = Local.TAIWAN;
	}
	else
	{
		locale = Locale.CHINA;
	}
}
if(locale==null)
{
	locale = Locale.CHINA;
}
request.getSession().setAttribute("i18_local", locale.toString());
request.getSession().setAttribute("timeStampForJS", System.currentTimeMillis());
ai.getInvocationContext().setLocale(locale);


2. In the page, the referenced js file is obtained according to the current language to prevent the cache from adding timestamps
<s:if test='#session.i18_local == "en_US"'>
<script src="/lang/lang_en_US.js?<s:property value='#session.timeStampForJS'/>" type="text/javascript"></script>
</s:if>
<s:elseif test='#session.i18_local == "zh_TW"'>
<script src="/lang/lang_zh_TW.js?<s:property value='#session.timeStampForJS'/>" type="text/javascript"></script>
</s:elseif>
<s:else>
<script src="/lang/lang_zh_CN.js?<s:property value='#session.timeStampForJS'/>" type="text/javascript"></script>
</s:else>


3. Create 3 internationalized js files
lang_en_US.js:
var js_edittag = "Edit tag";


lang_zh_CN.js :
var js_edittag = "Edit Tag";


lang_zh_TW.js :
var js_edittag = "Edit Tag";



Just use the global variable js_edittag directly in js.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327093640&siteId=291194637