Skip to content

Commit 1f98a18

Browse files
qxoCalixte
authored andcommitted
Pull #265: violation message locale switchable without restart eclipse
1 parent 2d5bd92 commit 1f98a18

File tree

6 files changed

+127
-20
lines changed

6 files changed

+127
-20
lines changed

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/CheckstylePlugin.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class CheckstylePlugin extends Plugin {
4545
/** Extension point id for Checkstyle addon providers. */
4646
private static final String ADDON_PROVIDER_EXT_PT_ID = PLUGIN_ID + ".checkstyleAddonProvider"; //$NON-NLS-1$
4747

48+
/**
49+
* Platform Locale.
50+
*/
51+
private static Locale platformLocale;
52+
4853
/** The shared instance. */
4954
private static CheckstylePlugin sPlugin;
5055

@@ -103,15 +108,20 @@ public static IWorkspace getWorkspace() {
103108
* @return the platform locale
104109
*/
105110
public static Locale getPlatformLocale() {
111+
if (platformLocale == null) {
112+
final String language = Platform.getNL();
113+
final String[] parts = language.split("_");
114+
if (parts.length > 0) {
115+
platformLocale = new Locale(parts[0]);
116+
} else {
117+
platformLocale = Locale.getDefault();
118+
}
119+
}
120+
return platformLocale;
121+
}
106122

107-
String nl = Platform.getNL();
108-
String[] parts = nl.split("_"); //$NON-NLS-1$
109-
110-
String language = parts.length > 0 ? parts[0] : ""; //$NON-NLS-1$
111-
String country = parts.length > 1 ? parts[1] : ""; //$NON-NLS-1$
112-
String variant = parts.length > 2 ? parts[2] : ""; //$NON-NLS-1$
113-
114-
return new Locale(language, country, variant);
123+
public static void setPlatformLocale(final Locale locale) {
124+
platformLocale = locale;
115125
}
116126

117127
/**

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/CheckstylePluginPrefs.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* Class for handling preferences of the <code>net.sf.eclipsecs.core</code> plugin.
35-
*
35+
*
3636
* @author Lars Ködderitzsch
3737
*/
3838
public class CheckstylePluginPrefs extends AbstractPreferenceInitializer {
@@ -66,6 +66,11 @@ public class CheckstylePluginPrefs extends AbstractPreferenceInitializer {
6666
/** Default value for the marker limitation. */
6767
public static final int MARKER_LIMIT = 100;
6868

69+
/**
70+
* Preference checkstyle rule language.
71+
*/
72+
public static final String PREF_LOCALE_LANGUAGE = "checkstyle_rule_language";
73+
6974
/**
7075
* {@inheritDoc}
7176
*/
@@ -86,9 +91,21 @@ public void initializeDefaultPreferences() {
8691
}
8792
}
8893

94+
/**
95+
* Returns a string preference for the given preference id.
96+
*
97+
* @param prefId
98+
* the preference id
99+
* @return the string result
100+
*/
101+
public static String getString(String prefId) {
102+
final IPreferencesService prefs = Platform.getPreferencesService();
103+
return prefs.getString(CheckstylePlugin.PLUGIN_ID, prefId, null, null);
104+
}
105+
89106
/**
90107
* Returns a boolean preference for the given preference id.
91-
*
108+
*
92109
* @param prefId
93110
* the preference id
94111
* @return the boolean result
@@ -101,7 +118,7 @@ public static boolean getBoolean(String prefId) {
101118

102119
/**
103120
* Returns an integer preference for the given preference id.
104-
*
121+
*
105122
* @param prefId
106123
* the preference id
107124
* @return the integer result
@@ -112,9 +129,27 @@ public static int getInt(String prefId) {
112129
return prefs.getInt(CheckstylePlugin.PLUGIN_ID, prefId, 0, null);
113130
}
114131

132+
/**
133+
* Set a string preference for the given preference id.
134+
*
135+
* @param prefId
136+
* the preference id
137+
* @param value
138+
* the string value
139+
* @throws BackingStoreException
140+
* if this operation cannot be completed due to a failure in the
141+
* backing store, or inability to communicate with it.
142+
*/
143+
public static void setString(final String prefId, final String value)
144+
throws BackingStoreException {
145+
final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(CheckstylePlugin.PLUGIN_ID);
146+
prefs.put(prefId, value);
147+
prefs.flush();
148+
}
149+
115150
/**
116151
* Set a boolean preference for the given preference id.
117-
*
152+
*
118153
* @param prefId
119154
* the preference id
120155
* @param value
@@ -132,7 +167,7 @@ public static void setBoolean(String prefId, boolean value) throws BackingStoreE
132167

133168
/**
134169
* Set a int preference for the given preference id.
135-
*
170+
*
136171
* @param prefId
137172
* the preference id
138173
* @param value

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckerFactory.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.puppycrawl.tools.checkstyle.PropertyResolver;
3333
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
3434
import com.puppycrawl.tools.checkstyle.api.Configuration;
35+
import com.puppycrawl.tools.checkstyle.LocalizedMessage;
3536

3637
import java.io.UnsupportedEncodingException;
3738
import java.net.URL;
@@ -41,6 +42,7 @@
4142
import java.util.concurrent.ConcurrentHashMap;
4243

4344
import net.sf.eclipsecs.core.CheckstylePlugin;
45+
import net.sf.eclipsecs.core.CheckstylePluginPrefs;
4446
import net.sf.eclipsecs.core.config.CheckstyleConfigurationFile;
4547
import net.sf.eclipsecs.core.config.ICheckConfiguration;
4648
import net.sf.eclipsecs.core.config.configtypes.IContextAware;
@@ -128,6 +130,8 @@ public static Checker createChecker(ICheckConfiguration config, IProject project
128130
Long modified = Long.valueOf(configFileData.getModificationStamp());
129131
sCheckerMap.put(cacheKey, checker);
130132
sModifiedMap.put(cacheKey, modified);
133+
} else {
134+
setLocaleIfChanged(checker);
131135
}
132136

133137
return checker;
@@ -230,11 +234,7 @@ private static Checker createCheckerInternal(InputSource input, PropertyResolver
230234
CheckstylePluginException.rethrow(e);
231235
}
232236

233-
// set the eclipse platform locale
234-
Locale platformLocale = CheckstylePlugin.getPlatformLocale();
235-
checker.setLocaleLanguage(platformLocale.getLanguage());
236-
checker.setLocaleCountry(platformLocale.getCountry());
237-
237+
setLocale(checker, getLocale());
238238
checker.configure(configuration);
239239

240240
// reset the basedir if it is set so it won't get into the plugins way
@@ -245,4 +245,38 @@ private static Checker createCheckerInternal(InputSource input, PropertyResolver
245245

246246
return checker;
247247
}
248+
249+
private static void setLocaleIfChanged(final Checker checker) {
250+
final String lc = getLocale();
251+
if (lc != null && !lc.equals(CheckstylePlugin.getPlatformLocale().getLanguage())) {
252+
setLocale(checker, lc);
253+
}
254+
}
255+
256+
private static void setLocale(final Checker checker, final String lang) {
257+
final String lastLocale;
258+
if (lang != null) {
259+
lastLocale = lang;
260+
checker.setLocaleLanguage(lang);
261+
checker.setLocaleCountry("");
262+
final Locale locale = new Locale(lang);
263+
LocalizedMessage.setLocale(locale);
264+
CheckstylePlugin.setPlatformLocale(locale);
265+
} else {
266+
// set the eclipse platform locale
267+
final Locale platformLocale = CheckstylePlugin.getPlatformLocale();
268+
lastLocale = platformLocale.getLanguage();
269+
checker.setLocaleLanguage(lastLocale);
270+
checker.setLocaleCountry(platformLocale.getCountry());
271+
LocalizedMessage.setLocale(platformLocale);
272+
}
273+
}
274+
275+
private static String getLocale() {
276+
String lang = CheckstylePluginPrefs.getString(CheckstylePluginPrefs.PREF_LOCALE_LANGUAGE);
277+
if (lang != null && (lang.isEmpty() || "default".equals(lang))) {
278+
lang = null;
279+
}
280+
return lang;
281+
}
248282
}

net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/Messages.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public final class Messages extends NLS {
174174

175175
public static String CheckstylePreferencePage_txtSuggestRebuild;
176176

177+
public static String CheckstylePreferencePage_lblLocaleLanguage;
178+
179+
public static String CheckstylePreferencePage_lblLocaleLanguages;
180+
177181
public static String CheckstylePreferencePage_version;
178182

179183
public static String CheckstylePropertyPage_btnActivateCheckstyle;
@@ -490,7 +494,7 @@ private Messages() {
490494
public static String CheckstylePreferenceTransfer_name;
491495

492496
public static String MarkerPropertyPage_Issue;
493-
497+
494498
public static String MarkerPropertyPage_Module;
495499

496500
public static String MarkerPropertyPage_SuppressionHint;

net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/messages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ CheckstylePreferencePage_titleRebuild = Rebuild suggested
148148

149149
CheckstylePreferencePage_txtSuggestRebuild = Note: Changes to this option only become visible\nafter a full rebuild of your projects.
150150

151+
CheckstylePreferencePage_lblLocaleLanguage= Rule message language
152+
CheckstylePreferencePage_lblLocaleLanguages= default,de,en,es,fi,fr,ja,pt,tr,zh
153+
151154
CheckstylePreferencePage_version = The plugin uses Checkstyle version {0}.
152155
CheckstylePropertyPage_btnActivateCheckstyle = Checkstyle active for this project
153156

net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/preferences/CheckstylePreferencePage.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class CheckstylePreferencePage extends PreferencePage implements IWorkben
8686

8787
private Button mLimitCheckstyleMarkers;
8888

89+
private Combo mLanguageIf;
90+
8991
private Text mTxtMarkerLimit;
9092

9193
private Button mBackgroundFullBuild;
@@ -176,6 +178,23 @@ private Composite createGeneralContents(Composite parent) {
176178
gridLayout.numColumns = 1;
177179
generalComposite.setLayout(gridLayout);
178180

181+
final Composite langComposite = new Composite(generalComposite, SWT.NULL);
182+
gridLayout = new GridLayout(3, false);
183+
gridLayout.marginHeight = 0;
184+
gridLayout.marginWidth = 0;
185+
langComposite.setLayout(gridLayout);
186+
langComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
187+
188+
final Label lblLanguage = new Label(langComposite, SWT.NULL);
189+
lblLanguage.setText(Messages.CheckstylePreferencePage_lblLocaleLanguage);
190+
mLanguageIf = new Combo(langComposite, SWT.READ_ONLY);
191+
mLanguageIf.setItems(Messages.CheckstylePreferencePage_lblLocaleLanguages.split("[, ;]+"));
192+
final String lang = CheckstylePluginPrefs.getString(CheckstylePluginPrefs.PREF_LOCALE_LANGUAGE);
193+
final int selectedLang = mLanguageIf.indexOf(lang == null || lang.isEmpty() ? "default" : lang);
194+
if (selectedLang != -1) {
195+
mLanguageIf.select(selectedLang);
196+
}
197+
179198
//
180199
// Create a combo with the rebuild options
181200
//
@@ -299,7 +318,6 @@ private Composite createGeneralContents(Composite parent) {
299318
mBackgroundFullBuild.setText(Messages.CheckstylePreferencePage_txtBackgroundFullBuild0);
300319
mBackgroundFullBuild.setSelection(
301320
CheckstylePluginPrefs.getBoolean(CheckstylePluginPrefs.PREF_BACKGROUND_FULL_BUILD));
302-
303321
return generalComposite;
304322
}
305323

@@ -350,6 +368,9 @@ public boolean performOk() {
350368
//
351369
mWorkingSet.store();
352370

371+
CheckstylePluginPrefs.setString(CheckstylePluginPrefs.PREF_LOCALE_LANGUAGE,
372+
mLanguageIf.getItem(mLanguageIf.getSelectionIndex()));
373+
353374
//
354375
// Save the general preferences.
355376
//

0 commit comments

Comments
 (0)