I am developing a multi-lingual page with JSTL, when I run the code, I got the following output:
English German Spanish
???label.greeting???
???label.firstname??? John
???label.lastname??? Doe
???label.welcome???
Here is the JSP page:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<c:set var="theLocale"
value="${not empty param.theLocale ? param.theLocale : pageContext.request.locale}"
scope="session" />
<fmt:setLocale value="${theLocale}" />
<fmt:setBundle basename="com.mycompany.multilingual.i18n.resources.mylabels" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="i18n-messages-test.jsp?theLocale=en_US">English </a>
<a href="i18n-messages-test.jsp?theLocale=de_DE">German </a>
<a href="i18n-messages-test.jsp?theLocale=es_ES">Spanish</a>
<hr/>
<fmt:message key="label.greeting" /> <br/> <br/>
<fmt:message key="label.firstname" /> <i>John</i> <br/>
<fmt:message key="label.lastname" /> <i>Doe</i> <br/><br/>
<fmt:message key="label.welcome" /> <br/>
</body>
</html>
The resources files are located like this: Project
CodePudding user response:
Very likely there is no localized value available for the UI to display, therefore the library tries to show you which key to look for.
- Check your browser for the configured (preferred) language.
- Check the localization for that name and the key visible in the UI.
- Provide a better value, rebuild and restart the system and try again.
CodePudding user response:
Internationalization is done as follows:
1 - Create three file called messages_en.properties, messages_es.properties, messages_de.properties,respectively, under /src/main/resources in your project, add the following lines in it, and save the file:
addProduct.name = Name
addProduct.price = Price
addProduct.description = Description
furthermore, do the same things for other file languages, translated to the corresponding language.
2 - Open your web application context and add
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new
SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("en"));
return resolver;
}
3- add an interceptor to your web application context to permit control over languages that have been set it
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new
ProcessingTimeLogInterceptor());
LocaleChangeInterceptor localeChangeInterceptor = new
LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
registry.addInterceptor(localeChangeInterceptor);
}
4- don't forget to add taglib on your jsp page
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<a href="?language=en" >English</a>|<a href="?
language=nl" >Dutch</a>
<label>
<spring:message code="put properties that you have defined on your
application properties">
</label>
finally and last, testing your translation that you've made using lang in your URL localhost:8080/yourproject/index?lang=de, es, or en