001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle; 021 022import java.text.MessageFormat; 023import java.util.Locale; 024import java.util.MissingResourceException; 025import java.util.ResourceBundle; 026 027import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil; 028 029/** 030 * Represents a message that can be localised. The translations come from 031 * message.properties files. The underlying implementation uses java.text.MessageFormat. 032 */ 033public class LocalizedMessage { 034 035 /** The locale to localise messages to. */ 036 private static Locale messageLocale = Locale.getDefault(); 037 038 /** Name of the resource bundle to get messages from. */ 039 private final String bundle; 040 041 /** Class of the source for this message. */ 042 private final Class<?> sourceClass; 043 044 /** 045 * Key for the message format. 046 */ 047 private final String key; 048 049 /** 050 * Arguments for java.text.MessageFormat, that is why type is Object[]. 051 * 052 * <p>Note: Changing types from Object[] will be huge breaking compatibility, as Module 053 * messages use some type formatting already, so better to keep it as Object[]. 054 * </p> 055 */ 056 private final Object[] args; 057 058 /** 059 * Creates a new {@code LocalizedMessage} instance. 060 * 061 * @param bundle resource bundle name 062 * @param sourceClass the Class that is the source of the message 063 * @param key the key to locate the translation. 064 * @param args arguments for the translation. 065 */ 066 public LocalizedMessage(String bundle, Class<?> sourceClass, String key, 067 Object... args) { 068 this.bundle = bundle; 069 this.sourceClass = sourceClass; 070 this.key = key; 071 if (args == null) { 072 this.args = null; 073 } 074 else { 075 this.args = UnmodifiableCollectionUtil.copyOfArray(args, args.length); 076 } 077 } 078 079 /** 080 * Sets a locale to use for localization. 081 * 082 * @param locale the locale to use for localization 083 */ 084 public static void setLocale(Locale locale) { 085 if (Locale.ENGLISH.getLanguage().equals(locale.getLanguage())) { 086 messageLocale = Locale.ROOT; 087 } 088 else { 089 messageLocale = locale; 090 } 091 } 092 093 /** 094 * Gets the translated message. 095 * 096 * @return the translated message. 097 */ 098 public String getMessage() { 099 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}