05. Resource bundles (i18n)

 1. Introduction

There are several ways to use resource bundles. By now, let's use java instead of xml (<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>)

1. First lay the resource files (main_language.properties) in the folder src/main/resources/i18n


2. Create a jsp file

3. Add a new page directive with the includes (Locale y ResourceBundle)

4. Get the locale from the request and the resource bundle from the relative path "i18n/main"

5. Assign the localized value using <%=bundle.getString("key") %>

The source code is


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.io.*,java.util.Locale, java.util.ResourceBundle" %>        
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>JSP-02</title>
  </head>

  <body>
    <% 
      Locale locale = request.getLocale();
      ResourceBundle bundle = ResourceBundle.getBundle("i18n/main", locale);
    %>
    <p id="pInstruct"><%=bundle.getString("instruccions") %></p>
  </body>
</html>


Comentarios

Entradas populares de este blog

15 Typescript (3) json ..

10. Some Javascript & JQuery concepts

14 Typescript (2) with jquery, bootstrap and other module stuff