It is necessary to create an object (It is possible even for each language to have a separate class) which stores all the messages in it. If necessary, display a message to the user depending on the language selected in the settings.

My application is going to support 3 languages: English Russian and Ukraine.

I decided to portray them like this.

public enum Language { RUSSIAN(1), ENGLISH(2), UKRAINE(3); private Integer id; Language(Integer id) { this.id = id; } public static Language reLang(Integer id) { switch (id) { case 1: return RUSSIAN; case 2: return ENGLISH; case 3: return UKRAINE; default: return RUSSIAN; } } public Integer getId() { return id; } } 

I store the selected user language as a numeric identifier: Russian - 1 English - 2 Ukrainian - 3

An example of how I get the user language.

 User user = new User("UserName"); user.getLang(); // Получаю язык пользователя(enum). (Язык берется из базы данных Mysql). 

The next stage I go through is the output of the message to the user. Here I have problems because I have never done this before (Yes, and I don’t have the best type of implementation).

I created a class under

 public class UtilChat { public static void message(Language lang, User user, Messages messages){ //Знаю можно было получать язык юзера здесь. а не в методе его вводить //отдельно } } 

By this statistical method, I am going to display a message to the user in the language of his choice. Here I have a question.

What is the best way I can store each message language?

There was an option for each language to create its own class, but it never came to me how to get the right messages from the class (taking into account the language).

Can eat somewhere already similar implementation?

  • And what is the difficulty? 3 options for each type of message. Stored in the same place where you have all the messages. At a certain parameter, one of them is selected. - Drakonoved

2 answers 2

How to make support for multiple languages?

The main thing in development is not to reinvent the wheel.

For language support in Java, the Locale class is responsible.

What is a locale?

More about Locale

Similar question on ru.stackoverflow: Multiple language support

Accordingly, it is necessary to describe the supported languages ​​in the configurations / properties.

Where to store the text of the application in different languages?

For this are used templates. For example, Apache Velocity .

That is, a set of template files is created for each language. And depending on the current Locale , the templates with the desired language are pulled.

Why is all this necessary?

The described approach allows us to separate the implementation of the business logic from language dependence. And also allows you to easily and quickly connect new languages ​​without changing the business logic.

  • Where is the solution? Where is the presentation? There is nothing, only water. - And
  • Solution by reference in response to a similar question. Performance in the circus. In response, a set of components to study the question. - Mikhail Grebenev
 enum Lang { RUSSIAN(new String[]{ "сообщение1", "сообщение2" }), ENGLISH(new String[]{ "message1", "message2" }); final String[] msg; private Lang(final String[] str) { this.msg = str; } } 

In some main class, we make a property that will change:

 private static Lang type = Lang.RUSSIAN; 

And the method that will return messages:

 public static String msg(final int key) { return type.msg[key]; } 

We use:

 System.out.println(msg(0)); // сообщение1 
  • one
    In my opinion you are trying to invent a bicycle on square wheels. And it will go? :) And if there are several thousand messages, how convenient will it be to edit them in this Enum and work with faceless message numbers through the index number in Array? - Mikhail Grebenev
  • @MikhailGrebenev, it’s unlikely that the TCA will have more than 50 texts, or even less. About thousands, you have bent - And