M
M
Mimocodil2021-07-30 18:26:02
Java
Mimocodil, 2021-07-30 18:26:02

What is the best way to organize support for multiple languages?

Right now my project supports multiple languages ​​with a giant singleton with a single public method:

public String getMessage(String ietfCode, String target) {
  if (!verifyLangTarget(target))
    throw new IllegalArgumentException("Invalid target \"" + target + "\"");
  switch (ietfCode) {
  case "ru" -> ruList.get(target);
  case "ua" -> uaList.get(target);
  case "be" -> beList.get(target);
  case "pl" -> plList.get(target);
  default -> enList.get(target);
  }
}

Here ruList, uaList etc. are the HashMap instances that I populate when I run the program from private functions like initRu(){}, initUa(){}, etc., called when the class is created.

It works, but I would like to make it possible to connect new and change existing languages ​​​​without editing the code and generally remove all lines from the code.

At the moment, I'm considering this option: create a public repository in GitHub, which will contain a folder with a configuration file (what languages ​​are there) and a folder that contains all languages ​​in the form of json files (ua.json, pl.json, etc. .). The JSON format itself is easy to read and modify + anyone can contribute by improving the translation or adding a new language.
Since my program works on the Internet, it will always be able to read the contents of this repository. It first reads the configuration file and then loads the language files.

How good is this solution? Maybe there is a better option?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question