LanguageControl.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.i18n;
  16. import java.io.BufferedInputStream;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.util.Locale;
  20. import java.util.PropertyResourceBundle;
  21. import java.util.ResourceBundle;
  22. import java.util.ResourceBundle.Control;
  23. /**
  24. * @author KenM
  25. */
  26. public class LanguageControl extends Control
  27. {
  28. public static final String LANGUAGES_DIRECTORY = "../languages/";
  29. public static final LanguageControl INSTANCE = new LanguageControl();
  30. /**
  31. * prevent instancing, allows sub-classing
  32. */
  33. protected LanguageControl()
  34. {
  35. }
  36. @Override
  37. public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IOException
  38. {
  39. if ((baseName == null) || (locale == null) || (format == null) || (loader == null))
  40. {
  41. throw new NullPointerException();
  42. }
  43. ResourceBundle bundle = null;
  44. if (format.equals("java.properties"))
  45. {
  46. format = "properties";
  47. String bundleName = toBundleName(baseName, locale);
  48. String resourceName = LANGUAGES_DIRECTORY + toResourceName(bundleName, format);
  49. try (FileInputStream fis = new FileInputStream(resourceName); BufferedInputStream bis = new BufferedInputStream(fis))
  50. {
  51. bundle = new PropertyResourceBundle(bis);
  52. }
  53. }
  54. return bundle;
  55. }
  56. }