Servlet Internationalization

Internationalization (often abbreviated as i18n) is the process of designing software so it can be adapted to various languages and regions without requiring engineering changes to the source code.

In this tutorial, you'll learn how to internationalize a simple greeting message in a servlet.

Step 1: Create a new dynamic web project

Create a new dynamic web project in your IDE.

Step 2: Create a new Servlet

Create a new servlet in your project. We'll name it "InternationalizationServlet" for this tutorial.

Step 3: Create property files for different languages

Java uses properties files for internationalization. The properties files contain key-value pairs where the key remains the same across all languages, and the value is translated into the respective languages.

  1. Create a file called messages.properties in your src directory. This will be your default properties file. It should look something like this:

    greeting=Hello
    
  2. Create a properties file for each language you want to support. For example, to support Spanish, you would create a file called messages_es.properties:

    greeting=Hola
    

Step 4: Write the Servlet code

In the created servlet, write the following code:

package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/InternationalizationServlet")
public class InternationalizationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // Get the user's language
        String language = request.getParameter("lang");
        Locale locale = null;

        // Set the locale based on the user's language
        if (language != null && language.equals("es")) {
            locale = new Locale("es", "ES");
        } else {
            locale = Locale.getDefault();
        }

        // Get the appropriate ResourceBundle for the user's locale
        ResourceBundle bundle = ResourceBundle.getBundle("com.example.messages", locale);

        // Get the greeting message from the ResourceBundle and print it
        String greeting = bundle.getString("greeting");
        out.println("<h1>" + greeting + "</h1>");
    }
}

In this servlet, we're getting a "lang" parameter from the request to determine the user's language. We then create a Locale object based on the user's language and use it to get the appropriate ResourceBundle. The ResourceBundle gives us access to the correct properties file based on the Locale. We then get the greeting message from the ResourceBundle and print it.

Step 5: Running the Servlet

Run the servlet on your server.

You should now be able to see different greetings in response to different requests. For example, if the server is running on your local machine, you can access your servlet at http://localhost:8080/InternationalizationServlet/InternationalizationServlet?lang=es to see the greeting in Spanish.

This is a basic example. In a real-world application, you might get the user's language from a variety of sources, such as the Accept-Language header, a user preference stored in a database, or a URL parameter as shown here. You would also likely have more complex messages, possibly with variables or plural forms.

  1. Using resource bundles in Servlets: Utilize Java resource bundles to store localized content.

    ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
    String greeting = bundle.getString("greeting");
    
  2. Java Servlet locale setting: Set the locale for a Servlet.

    • Use request.setLocale() to change the locale.
    request.setLocale(new Locale("fr", "FR")); // Set French locale
    
  3. Parameterized messages in Servlet i18n: Use parameterized messages in i18n for dynamic content.

    • Include placeholders in messages and replace them at runtime.
    String welcomeMessage = MessageFormat.format(bundle.getString("welcome"), username);
    
  4. Message formatting in Servlet internationalization: Format messages based on the locale.

    • Use MessageFormat or Formatter for localized message formatting.
    String formattedDate = DateFormat.getDateInstance(DateFormat.FULL, locale).format(new Date());