Table of contents
1.
Introduction📜
2.
What is Internationalization?
3.
Frequently Asked Questions
3.1.
What is Internationalization?
3.2.
What is the need for Internationalization?
3.3.
What does spring boot's @RestController annotation mean?
3.4.
Can @component take the place of @controller?
3.5.
Can we build a resource using Put rather than POST?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Internationalization of RESTful Services

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📜

Have you ever dealt with the difficult issue of serving your customers in their own language? If so, you might be interested in locating a suitable strategy for accomplishing this.

Internationalization

This blog will explain how to easily add internationalization to our Spring Boot application in simple steps. So without any further delay, let's get started!

What is Internationalization?

When dealing with a project that should serve users from multiple nations with different languages, the issue of application internationalization becomes difficult. In these circumstances, we will need to offer our French-speaking users a response message in French to make everyone comfortable using our programme. 

The process of making web applications and services so that they can automatically support multiple countries and languages without requiring changes to the application is known as internationalization. The word internationalization contains a total of 18 characters, starting with I and ending with N. It is also known as I18N.

Java provides the framework for server applications and desktops. The following crucial functional internationalized areas are listed.

Functional Internationalised Areas   Description
Localization & Locale identification Locale is a Java identifier that can be used to ask for locale-specific functionality in various functional areas. The ResourceBundle class supports localization. Access to locally specific objects, such as strings, is made available by the class.

Handling dates and times 

 

Java comes with several calendars. It enables conversion to and from Date objects independent of the calendar. Java supports all time zones in the world.
Text Representation The Unicode character set, which is the foundation of Java, is implemented by some libraries.
Date and time handling Java provides various calendars. It supports conversion to and from calendar-independent Date objects. Java works fine for all the time zones in the world.
Text processing Character analysis, case mapping, string comparison, word-by-word text analysis, and the formatting of dates, time values, and numbers into strings or parsing them back from strings. These are all examples of text processing.

To internationalise the service, two configurations are required.

🟩LocaleResolver

      🟢Default Locale - Locale.Us

🟩ResourceBundleMessageSource

Default Locale is Locale.US. It returns the default locale if the location is not specified. The ResourceBundle needs to be customised as well. The properties will be kept in ResourceBundle. Spring MVC uses the ResourceBundleMessageSource concept to manage properties. MessageSource and the Accept-Language header will be used after that.

Now, Let us configure the internationalization:

Step 1: Open RestfulWebServicesApplication.java file.

Step 2: Configure a Bean for the default locale.

@Bean  
public  LocaleResolver localeResolver()  
{  
SessionLocaleResolver localeResolver = new SessionLocaleResolver();  
localeResolver.setDefaultLocale(Locale.US);  
return localeResolver;  
}  
You can also try this code with Online Java Compiler
Run Code


Step 3: Now, we need to store the properties in a file called messages.properties.

Click right on the src/main/resources folder -> New -> File -> Provide the file name: messages.properties. It will contain the default locale message.

messages.properties

good.evening.message=Good Afternoon
You can also try this code with Online Java Compiler
Run Code


Step 4: Make a property file with the name messages_fr.properties for the French people. It will contain messages for France.

messages_fr.properties

good.evening.message=Bonsoir  
You can also try this code with Online Java Compiler
Run Code


Step 5: Under the input accepts header, read the properties and modify them. To specify a different Bean for a ResourceBundle, open the RestfulWebServicesApplication.java file.

@Bean  
public ResourceBundleMessageSource bundleMessageSource()  
{  
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();  
messageSource.setBasename("messages");  
return messageSource;  
}  
You can also try this code with Online Java Compiler
Run Code


RestfulWebServicesApplication.java

package com.codingninjas.server.main;  
import java.util.Locale;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.support.ResourceBundleMessageSource;  
import org.springframework.web.servlet.LocaleResolver;  
import org.springframework.web.servlet.i18n.SessionLocaleResolver;  
@SpringBootApplication  
public class RestfulWebServicesApplication   
{  
public static void main(String[] ar)   
{  
SpringApplication.run(RestfulWebServicesApplication.class, ar);  
}  
/* configuring default locale */ 
@Bean  
public  LocaleResolver localeResolver()  
{  
SessionLocaleResolver localeResolver = new SessionLocaleResolver();  
localeResolver.setDefaultLocale(Locale.US);  
return localeResolver;  
}  
/* configuring the ResourceBundle */ 
@Bean  
public ResourceBundleMessageSource messageSource()  
{  
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();  
messageSource.setBasename("messages");  
return messageSource;  
}  
}  
You can also try this code with Online Java Compiler
Run Code


Step 6: To use these sources, update the service. Autowire the MessageSource in the HelloWorldController.java file and open it.

@Autowired   
private MessageSource messageSource;  
You can also try this code with Online Java Compiler
Run Code


HelloWorldController.java

package com.codingninjas.server.main.helloworld;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestHeader;  
import org.springframework.web.bind.annotation.RestController;  
import java.util.Locale;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.MessageSource;  
import org.springframework.context.annotation.Configuration;  
@Configuration  
/*Controller*/  
@RestController  
public class HelloWorldController   
{  
@Autowired   
private MessageSource messageSource;  
/* using hello-world */ 
@GetMapping(path="/hello-world")  
public String helloWorld()  
{  
return "Hello World";  
}  
@GetMapping(path="/hello-world-bean")  

public HelloWorldBean helloWorldBean()  
{  
return new HelloWorldBean("Hello World");
/* HelloWorldBean constructor */ 
}  
@GetMapping(path="/hello-world/path-variable/{name}")  
public HelloWorldBean helloWorldPathVariable(@PathVariable String name)  
{  
return new HelloWorldBean(String.format("Hello World, %s",name));   
}  
/* internationalization */ 
@GetMapping(path="/hello-world-internationalized")  
public String helloWorldInternationalized(@RequestHeader(name="Accept-Language", required=false) Locale locale)  
{  
return messageSource.getMessage("good.evening.message", null, locale);  
}  
}  
You can also try this code with Online Java Compiler
Run Code


Step 7: Open Postman, a REST client. Pick the GET request option. Enter http://localhost:8080/hello-world-internationalized. Type the following into the Headers tab:

              Key                 Value
Content-Type application/json
Accept-Language us

Now, Click on the Send button.

good evening

It will return US locale message, i.e., Good Evening.

We now issue another GET request while changing the RequestHeader from us to fr.

              Key                 Value
Content-Type application/json
Accept-Language us
Bonsoir

So, now it returns the message Good Evening in French, i.e., Bonsoir.

Frequently Asked Questions

What is Internationalization?

The process of making web applications or services so that they can automatically support multiple countries and languages without requiring changes to the application is known as internationalisation.

What is the need for Internationalization?

When dealing with a project that should serve users from multiple nations with different languages, the issue of application internationalisation becomes difficult. In these circumstances, we will need to offer our French-speaking users a response message in French to make everyone comfortable using our programme.

What does spring boot's @RestController annotation mean?

The @Controller and @ResponseBody annotations are already present in the Spring RestController annotation, which is a convenience annotation. Applying this annotation to a class designates it as a request handler. The Spring MVC annotation, Spring RestController, is used to build RESTful web services.

Can @component take the place of @controller?

There is no difference between @Service@Component@Controller, and @Repository. The generic annotation @Component is used to represent the component of our MVC.

Can we build a resource using Put rather than POST?

You can build or edit a resource using either POST or PUT, as both can be used to submit data. Because PUT is idempotent, it is preferred by many web developers for creating a resource on the server. No matter how often you call PUT, the resource's condition won't be in danger.

Conclusion

In this article, we have extensively discussed internationalisation. We have also learnt the need for internationalisation and how to do it step by step.

If you want to learn more, then check our articles on the topics Implementing DELETE Method to Delete a User ResourceTechnological Services in Ready APIWhat Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2pyand  XML in Web2py.

Happy Coding!

Live masterclass