You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultI18n.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.core.i18n;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.base.Preconditions;
  23. import java.io.Closeable;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.text.DateFormat;
  27. import java.text.DecimalFormat;
  28. import java.text.MessageFormat;
  29. import java.text.NumberFormat;
  30. import java.util.Collection;
  31. import java.util.Date;
  32. import java.util.Enumeration;
  33. import java.util.HashMap;
  34. import java.util.Locale;
  35. import java.util.Map;
  36. import java.util.MissingResourceException;
  37. import java.util.ResourceBundle;
  38. import java.util.Set;
  39. import javax.annotation.CheckForNull;
  40. import javax.annotation.Nullable;
  41. import org.apache.commons.io.IOUtils;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import org.sonar.api.Startable;
  45. import org.sonar.api.utils.SonarException;
  46. import org.sonar.api.utils.System2;
  47. import org.sonar.core.platform.PluginInfo;
  48. import org.sonar.core.platform.PluginRepository;
  49. import static java.nio.charset.StandardCharsets.UTF_8;
  50. public class DefaultI18n implements I18n, Startable {
  51. private static final Logger LOG = LoggerFactory.getLogger(DefaultI18n.class);
  52. private static final String BUNDLE_PACKAGE = "org.sonar.l10n.";
  53. private final PluginRepository pluginRepository;
  54. private final ResourceBundle.Control control;
  55. private final System2 system2;
  56. // the following fields are available after startup
  57. private ClassLoader classloader;
  58. private Map<String, String> propertyToBundles;
  59. public DefaultI18n(PluginRepository pluginRepository, System2 system2) {
  60. this.pluginRepository = pluginRepository;
  61. this.system2 = system2;
  62. // SONAR-2927
  63. this.control = new ResourceBundle.Control() {
  64. @Override
  65. public Locale getFallbackLocale(String baseName, Locale locale) {
  66. Preconditions.checkNotNull(baseName);
  67. Locale defaultLocale = Locale.ENGLISH;
  68. return locale.equals(defaultLocale) ? null : defaultLocale;
  69. }
  70. };
  71. }
  72. @Override
  73. public void start() {
  74. doStart(new I18nClassloader(pluginRepository));
  75. }
  76. @VisibleForTesting
  77. protected void doStart(ClassLoader classloader) {
  78. this.classloader = classloader;
  79. this.propertyToBundles = new HashMap<>();
  80. initialize();
  81. LOG.debug("Loaded {} properties from l10n bundles", propertyToBundles.size());
  82. }
  83. protected void initialize() {
  84. // org.sonar.l10n.core bundle is provided by sonar-core module
  85. initPlugin("core");
  86. Collection<PluginInfo> infos = pluginRepository.getPluginInfos();
  87. for (PluginInfo plugin : infos) {
  88. initPlugin(plugin.getKey());
  89. }
  90. }
  91. protected void initPlugin(String pluginKey) {
  92. try {
  93. String bundleKey = BUNDLE_PACKAGE + pluginKey;
  94. ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, this.classloader, control);
  95. Enumeration<String> keys = bundle.getKeys();
  96. while (keys.hasMoreElements()) {
  97. String key = keys.nextElement();
  98. propertyToBundles.put(key, bundleKey);
  99. }
  100. } catch (MissingResourceException e) {
  101. // ignore
  102. }
  103. }
  104. @Override
  105. public void stop() {
  106. if (classloader instanceof Closeable closeable) {
  107. IOUtils.closeQuietly(closeable);
  108. }
  109. classloader = null;
  110. propertyToBundles = null;
  111. }
  112. @Override
  113. @CheckForNull
  114. public String message(Locale locale, String key, @Nullable String defaultValue, Object... parameters) {
  115. String bundleKey = propertyToBundles.get(key);
  116. String value = null;
  117. if (bundleKey != null) {
  118. try {
  119. ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleKey, locale, classloader, control);
  120. value = resourceBundle.getString(key);
  121. } catch (MissingResourceException e1) {
  122. // ignore
  123. }
  124. }
  125. if (value == null) {
  126. value = defaultValue;
  127. }
  128. return formatMessage(value, parameters);
  129. }
  130. @Override
  131. public String age(Locale locale, long durationInMillis) {
  132. DurationLabel.Result duration = DurationLabel.label(durationInMillis);
  133. return message(locale, duration.key(), null, duration.value());
  134. }
  135. @Override
  136. public String age(Locale locale, Date fromDate, Date toDate) {
  137. return age(locale, toDate.getTime() - fromDate.getTime());
  138. }
  139. @Override
  140. public String ageFromNow(Locale locale, Date date) {
  141. return age(locale, system2.now() - date.getTime());
  142. }
  143. /**
  144. * Format date for the given locale. JVM timezone is used.
  145. */
  146. @Override
  147. public String formatDateTime(Locale locale, Date date) {
  148. return DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, locale).format(date);
  149. }
  150. @Override
  151. public String formatDate(Locale locale, Date date) {
  152. return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);
  153. }
  154. @Override
  155. public String formatDouble(Locale locale, Double value) {
  156. NumberFormat format = DecimalFormat.getNumberInstance(locale);
  157. format.setMinimumFractionDigits(1);
  158. format.setMaximumFractionDigits(1);
  159. return format.format(value);
  160. }
  161. @Override
  162. public String formatInteger(Locale locale, Integer value) {
  163. return NumberFormat.getNumberInstance(locale).format(value);
  164. }
  165. /**
  166. * Only the given locale is searched. Contrary to java.util.ResourceBundle, no strategy for locating the bundle is implemented in
  167. * this method.
  168. */
  169. String messageFromFile(Locale locale, String filename, String relatedProperty) {
  170. String result = null;
  171. String bundleBase = propertyToBundles.get(relatedProperty);
  172. if (bundleBase == null) {
  173. // this property has no translation
  174. return null;
  175. }
  176. String filePath = bundleBase.replace('.', '/');
  177. if (!"en".equals(locale.getLanguage())) {
  178. filePath += "_" + locale.getLanguage();
  179. }
  180. filePath += "/" + filename;
  181. InputStream input = classloader.getResourceAsStream(filePath);
  182. if (input != null) {
  183. result = readInputStream(filePath, input);
  184. }
  185. return result;
  186. }
  187. private static String readInputStream(String filePath, InputStream input) {
  188. String result;
  189. try {
  190. result = IOUtils.toString(input, UTF_8);
  191. } catch (IOException e) {
  192. throw new SonarException("Fail to load file: " + filePath, e);
  193. } finally {
  194. IOUtils.closeQuietly(input);
  195. }
  196. return result;
  197. }
  198. public Set<String> getPropertyKeys() {
  199. return propertyToBundles.keySet();
  200. }
  201. public Locale getEffectiveLocale(Locale locale) {
  202. Locale bundleLocale = ResourceBundle.getBundle(BUNDLE_PACKAGE + "core", locale, this.classloader, this.control).getLocale();
  203. locale.getISO3Language();
  204. return bundleLocale.getLanguage().isEmpty() ? Locale.ENGLISH : bundleLocale;
  205. }
  206. @CheckForNull
  207. private static String formatMessage(@Nullable String message, Object... parameters) {
  208. if (message == null || parameters.length == 0) {
  209. return message;
  210. }
  211. return MessageFormat.format(message.replaceAll("'", "''"), parameters);
  212. }
  213. }