LocalizationManager.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.localization;

  13. import com.typesafe.config.Config;
  14. import com.typesafe.config.ConfigFactory;
  15. import org.apache.log4j.Logger;
  16. import org.heigit.ors.util.StringUtility;
  17. import org.springframework.core.io.Resource;
  18. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;

  24. public class LocalizationManager {
  25.     protected static final Logger LOGGER = Logger.getLogger(LocalizationManager.class);

  26.     private final Map<String, LanguageResources> langResources;
  27.     private static LocalizationManager mInstance = null;

  28.     private LocalizationManager() throws Exception {
  29.         langResources = new HashMap<>();
  30.         loadLocalizations();
  31.     }

  32.     public static LocalizationManager getInstance() throws Exception {
  33.         if (null == mInstance) {
  34.             synchronized (LocalizationManager.class) {
  35.                 mInstance = new LocalizationManager();
  36.             }
  37.         }
  38.         return mInstance;
  39.     }

  40.     private void loadLocalizations() throws Exception {
  41.         PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(this.getClass().getClassLoader());

  42.         String filePattern = "ors_(.*?)(\\.default)?.resources";
  43.         String resourcePattern = "/resources/**/ors_*.resources";

  44.         Resource[] resources = resourcePatternResolver.getResources(resourcePattern);
  45.         Pattern pattern = Pattern.compile(filePattern);

  46.         if (resources.length == 0)
  47.             throw new Exception("Localization resources can not be found.");

  48.         for (Resource res : resources) {
  49.             Matcher matcher = pattern.matcher(res.getFilename());
  50.             if (matcher.find()) {
  51.                 loadLocalization(matcher.group(1).toLowerCase(), res, matcher.group(2) != null);
  52.             }
  53.         }
  54.     }

  55.     private void loadLocalization(String langTag, Resource resource, boolean isDefault) {
  56.         String langCode = langTag.substring(0, 2);
  57.         LanguageResources localLangResources = new LanguageResources(langTag);
  58.         try {
  59.             Config allConfig = ConfigFactory.parseURL(resource.getURL());
  60.             allConfig.entrySet().forEach(entry -> localLangResources.addLocalString(entry.getKey(), StringUtility.trim(entry.getValue().render(), '\"')));
  61.             this.langResources.put(langTag, localLangResources);
  62.             if (isDefault || !this.langResources.containsKey(langCode)) {
  63.                 this.langResources.put(langCode, localLangResources);
  64.             }
  65.         } catch (Exception ex) {
  66.             LOGGER.error("Unable to load localization from %s".formatted(resource));
  67.         }
  68.     }

  69.     public LanguageResources getLanguageResources(String langCode) {
  70.         return langResources.get(langCode.toLowerCase());
  71.     }

  72.     public boolean isLanguageSupported(String langCode) {
  73.         return langResources.containsKey(langCode.toLowerCase());
  74.     }

  75.     public String[] getLanguages() {
  76.         String[] langs = new String[langResources.size()];
  77.         int i = 0;
  78.         for (Map.Entry<String, LanguageResources> entry : langResources.entrySet()) {
  79.             langs[i] = entry.getKey();
  80.             i++;
  81.         }
  82.         Arrays.sort(langs);
  83.         return langs;
  84.     }
  85. }