LocaleCodes.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.tools.ngl;
  16. import java.util.HashMap;
  17. import java.util.Locale;
  18. import java.util.Map;
  19. /**
  20. * @author mrTJO
  21. */
  22. public class LocaleCodes
  23. {
  24. Map<String, Locale> _locales = new HashMap<String, Locale>();
  25. public static LocaleCodes getInstance()
  26. {
  27. return SingletonHolder._instance;
  28. }
  29. protected LocaleCodes()
  30. {
  31. loadCodes();
  32. }
  33. private void loadCodes()
  34. {
  35. for (Locale locale : Locale.getAvailableLocales())
  36. {
  37. String language = locale.getLanguage();
  38. // String script = locale.getScript();
  39. String country = locale.getCountry();
  40. String variant = locale.getVariant();
  41. if (language.isEmpty() && country.isEmpty() && variant.isEmpty())
  42. {
  43. continue;
  44. }
  45. StringBuilder lang = new StringBuilder();
  46. lang.append(language);
  47. if (!country.isEmpty())
  48. {
  49. lang.append(country);
  50. }
  51. if (!variant.isEmpty())
  52. {
  53. lang.append('_' + variant);
  54. }
  55. /*
  56. * if (script != "") lang.append('_'+script);
  57. */
  58. _locales.put(lang.toString(), locale);
  59. }
  60. }
  61. public Locale getLanguage(String lang)
  62. {
  63. return _locales.get(lang);
  64. }
  65. private static class SingletonHolder
  66. {
  67. protected static final LocaleCodes _instance = new LocaleCodes();
  68. }
  69. }