View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.text.MessageFormat;
23  import java.util.Locale;
24  import java.util.MissingResourceException;
25  import java.util.ResourceBundle;
26  
27  import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
28  
29  /**
30   * Represents a message that can be localised. The translations come from
31   * message.properties files. The underlying implementation uses java.text.MessageFormat.
32   */
33  public class LocalizedMessage {
34  
35      /** The locale to localise messages to. */
36      private static Locale messageLocale = Locale.getDefault();
37  
38      /** Name of the resource bundle to get messages from. */
39      private final String bundle;
40  
41      /** Class of the source for this message. */
42      private final Class<?> sourceClass;
43  
44      /**
45       * Key for the message format.
46       */
47      private final String key;
48  
49      /**
50       * Arguments for java.text.MessageFormat, that is why type is Object[].
51       *
52       * <p>Note: Changing types from Object[] will be huge breaking compatibility, as Module
53       * messages use some type formatting already, so better to keep it as Object[].
54       * </p>
55       */
56      private final Object[] args;
57  
58      /**
59       * Creates a new {@code LocalizedMessage} instance.
60       *
61       * @param bundle resource bundle name
62       * @param sourceClass the Class that is the source of the message
63       * @param key the key to locate the translation.
64       * @param args arguments for the translation.
65       */
66      public LocalizedMessage(String bundle, Class<?> sourceClass, String key,
67              Object... args) {
68          this.bundle = bundle;
69          this.sourceClass = sourceClass;
70          this.key = key;
71          if (args == null) {
72              this.args = null;
73          }
74          else {
75              this.args = UnmodifiableCollectionUtil.copyOfArray(args, args.length);
76          }
77      }
78  
79      /**
80       * Sets a locale to use for localization.
81       *
82       * @param locale the locale to use for localization
83       */
84      public static void setLocale(Locale locale) {
85          if (Locale.ENGLISH.getLanguage().equals(locale.getLanguage())) {
86              messageLocale = Locale.ROOT;
87          }
88          else {
89              messageLocale = locale;
90          }
91      }
92  
93      /**
94       * Gets the translated message.
95       *
96       * @return the translated message.
97       */
98      public String getMessage() {
99          String result;
100         try {
101             // Important to use the default class loader, and not the one in
102             // the GlobalProperties object. This is because the class loader in
103             // the GlobalProperties is specified by the user for resolving
104             // custom classes.
105             final ResourceBundle resourceBundle = getBundle();
106             final String pattern = resourceBundle.getString(key);
107             final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT);
108             result = formatter.format(args);
109         }
110         catch (final MissingResourceException ignored) {
111             // If the Check author didn't provide i18n resource bundles
112             // and logs audit event messages directly, this will return
113             // the author's original message
114             final MessageFormat formatter = new MessageFormat(key, Locale.ROOT);
115             result = formatter.format(args);
116         }
117         return result;
118     }
119 
120     /**
121      * Obtain the ResourceBundle. Uses the classloader
122      * of the class emitting this message, to be sure to get the correct
123      * bundle. Property files are read as UTF-8 by the JDK since Java 9,
124      * so no custom {@code ResourceBundle.Control} is needed.
125      *
126      * @return a ResourceBundle.
127      */
128     private ResourceBundle getBundle() {
129         return ResourceBundle.getBundle(bundle, messageLocale, sourceClass.getClassLoader());
130     }
131 
132 }