From acbc8698b387189a33d48db598d4c1769e4db6c7 Mon Sep 17 00:00:00 2001 From: thecapdan Date: Thu, 22 Sep 2016 16:40:15 +0100 Subject: [PATCH 1/3] fixes 1736. ensure check-i18n command outputs a utf-8 file to support non ascii languages --- .../plugin/checki18n/CheckI18nCommand.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java b/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java index adc74072d..9b87a25c3 100644 --- a/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java +++ b/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java @@ -1,9 +1,11 @@ package org.bladerunnerjs.plugin.checki18n; import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.FileOutputStream; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; @@ -19,6 +21,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.io.FileUtils; import org.bladerunnerjs.api.App; import org.bladerunnerjs.api.Aspect; import org.bladerunnerjs.api.Asset; @@ -143,17 +146,35 @@ private void generateAllTranslationsCSV(String appName) { File file = brjs.storageFile(this.getClass().getSimpleName(), appName + "-tokens.csv" ); file.getParentFile().mkdirs(); - try(Writer writer = new BufferedWriter(new FileWriter(file));) { + try{ + Writer writer = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream(file), "UTF-8")); + writer.append("** the 'IsUsed' column only relates to tokens identified in their entirety and" + " will not include tokens which are concatenated e.g. i18n('a.token.' + value);\n"); for(List row : rows){ writer.append(StringUtils.join(row, ",")+"\n"); - } - } catch (IOException e) { - e.printStackTrace(); + } + writer.flush(); + writer.close(); + + String content = FileUtils.readFileToString(file, "UTF-8"); + FileUtils.write(file, content, "UTF-8"); + } + catch (UnsupportedEncodingException e) + { + System.out.println(e.getMessage()); + } + catch (IOException e) + { + System.out.println(e.getMessage()); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + logger.println(Messages.APP_CHECK_I18N_CONSOLE_MSG, appName, file.getAbsolutePath()); } - logger.println(Messages.APP_CHECK_I18N_CONSOLE_MSG, appName, file.getAbsolutePath()); - } private void logMissingLocalesToConsole(String appName, String localeToBeChecked, Entry> tokensList) { String missingTokensMessage = tokensList.getValue().size() == 0 ? " has no missing translations" : " has no translations defined for the following tokens:"; From 9cc842f516e065e8a6e794a4bd3d5b296d463018 Mon Sep 17 00:00:00 2001 From: thecapdan Date: Thu, 22 Sep 2016 17:04:22 +0100 Subject: [PATCH 2/3] improving punctuation in browser-modules comment --- brjs-sdk/sdk/libs/javascript/browser-modules/browser-modules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brjs-sdk/sdk/libs/javascript/browser-modules/browser-modules.js b/brjs-sdk/sdk/libs/javascript/browser-modules/browser-modules.js index b193ed7e8..4c3ffb0d8 100644 --- a/brjs-sdk/sdk/libs/javascript/browser-modules/browser-modules.js +++ b/brjs-sdk/sdk/libs/javascript/browser-modules/browser-modules.js @@ -256,7 +256,7 @@ throw new Error("No definition for module " + id + " has been loaded."); } - // For closer spec compliance we should define id as a nonconfigurable, nonwritable + // For closer spec compliance, we should define id as a nonconfigurable, nonwritable // property, but this at least works OK in non-es5 browsers (like ie8). var module = { id: id, exports: new ModuleExports() }; this.incompleteExports[id] = module; From eb4209eef0dd3ab8a6154ee80d546500188e5bfd Mon Sep 17 00:00:00 2001 From: thecapdan Date: Fri, 23 Sep 2016 10:31:27 +0100 Subject: [PATCH 3/3] correcting indentation and printing of stacktrace to be consistent with the rest of the class #1736 --- .../plugin/checki18n/CheckI18nCommand.java | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java b/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java index 9b87a25c3..5479177b4 100644 --- a/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java +++ b/plugins/brjs-check-i18n-plugin/src/main/java/org/bladerunnerjs/plugin/checki18n/CheckI18nCommand.java @@ -51,13 +51,13 @@ import org.bladerunnerjs.api.logging.Logger; public class CheckI18nCommand extends JSAPArgsParsingCommandPlugin { - + public class Messages { public static final String APP_CHECK_I18N_CONSOLE_MSG = "I18N tokens for the app '%s' available at '%s'"; public static final String APP_DOES_NOT_EXIST_EXCEPTION = "The app '%s' does not exist"; public static final String NO_LOCALE_FOR_APP = "The app specified does not contain a default locale"; } - + private BRJS brjs; private Logger logger; private String locale; @@ -72,7 +72,7 @@ public class Messages { public String getCommandName() { return "check-i18n"; } - + @Override protected void configureArgsParser(JSAP argsParser) throws JSAPException { argsParser.registerParameter(new UnflaggedOption("app-name").setRequired(true).setHelp("the application to search for missing translations")); @@ -88,49 +88,49 @@ public String getCommandDescription() { protected int doCommand(JSAPResult parsedArgs) throws CommandArgumentsException, CommandOperationException { String appName = parsedArgs.getString("app-name"); locale = parsedArgs.getString("locale"); - + listMissingTokens(appName); generateAllTranslationsCSV(appName); return missingTokensExist ? -1 : 0; } - + private void listMissingTokens(String appName) throws CommandArgumentsException { App app = brjs.app(appName); - + try { appLocales = app.appConf().getLocales(); } catch (ConfigException e) { e.printStackTrace(); } - + if(!app.dirExists()) throw new CommandArgumentsException( String.format(Messages.APP_DOES_NOT_EXIST_EXCEPTION, appName), this ); - + findMissingTranslationsForAppWithLocale(app); - + for (Entry> tokensList : missingTokensMap.entrySet()) { - logMissingLocalesToConsole(appName, tokensList.getKey(), tokensList); + logMissingLocalesToConsole(appName, tokensList.getKey(), tokensList); } } private void generateAllTranslationsCSV(String appName) { logger.println("\ngenerating CSV\n"); - + TreeSet localeNames = new TreeSet(); for(Locale locale : appLocales ){ localeNames.add(locale.getLanguageCode()); } - + List headings = new ArrayList(); headings.add("Token"); for(String localeName : localeNames){ headings.add(localeName); } headings.add("IsUsed"); - + List> rows = new ArrayList>(); rows.add(headings); - + for(Entry> translationMap : allExistingTokens.entrySet()){ List newEntry = new ArrayList(); newEntry.add(translationMap.getKey()); @@ -142,10 +142,10 @@ private void generateAllTranslationsCSV(String appName) { newEntry.add(translationMap.getValue().get("used")); rows.add(newEntry); } - + File file = brjs.storageFile(this.getClass().getSimpleName(), appName + "-tokens.csv" ); file.getParentFile().mkdirs(); - + try{ Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "UTF-8")); @@ -159,23 +159,20 @@ private void generateAllTranslationsCSV(String appName) { writer.close(); String content = FileUtils.readFileToString(file, "UTF-8"); - FileUtils.write(file, content, "UTF-8"); - } - catch (UnsupportedEncodingException e) - { - System.out.println(e.getMessage()); - } - catch (IOException e) - { - System.out.println(e.getMessage()); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - } - logger.println(Messages.APP_CHECK_I18N_CONSOLE_MSG, appName, file.getAbsolutePath()); + FileUtils.write(file, content, "UTF-8"); } - + catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + catch (IOException e){ + e.printStackTrace(); + } + catch (Exception e) { + e.printStackTrace(); + } + logger.println(Messages.APP_CHECK_I18N_CONSOLE_MSG, appName, file.getAbsolutePath()); + } + private void logMissingLocalesToConsole(String appName, String localeToBeChecked, Entry> tokensList) { String missingTokensMessage = tokensList.getValue().size() == 0 ? " has no missing translations" : " has no translations defined for the following tokens:"; String firstLogLine = "\n" + "For the locale " + tokensList.getKey() + ", " + appName + missingTokensMessage + "\n"; @@ -189,7 +186,7 @@ private void findMissingTranslationsForAppWithLocale(App app) { for(Aspect aspect : app.aspects()) { logger.println("checking " + aspect.getName() + " aspect"); checkMissingLocalsForBundlableNode(aspect); - + checkMissingLocales(aspect); } for(Bladeset bladeset : app.bladesets()) { @@ -236,7 +233,7 @@ private void checkBundletForMissingTokens(BundleSet bundleSet) { } checkAssetForMissingTokens(bundleSet, content, I18N_HTML_XML_TOKEN_PATTERN); } - + List sourcceModules = bundleSet.sourceModules(); for(SourceModule sourceModule : sourcceModules){ String srcContent = null; @@ -269,7 +266,7 @@ private void matchTokensForLocale(String content, Pattern pattern, Map 1){ tokenIsNotComplete = i18nTokenMatcher.group(2) != null && i18nTokenMatcher.group(2).indexOf('+') != -1; propertiesFileContainPartialMatch = mapContainsPartialToken(propertiesMap, i18nTokenMatcher.group(1)) && tokenIsNotComplete; @@ -292,7 +289,7 @@ else if (keyReplacement != null) { else{ missingTokensMap.get(localeCode).addAll(missingTokens); } - + addUnusedTokensToAllTokensMap(propertiesMap, localeCode); } @@ -301,7 +298,7 @@ private void addUnusedTokensToAllTokensMap(Map propertiesMap, St { String i18nKey = i18nPair.getKey(); String translation = i18nPair.getValue(); - + if(!allExistingTokens.containsKey(i18nKey)){ populateAllTokensMap(localeCode, i18nKey, translation, false); } @@ -320,10 +317,10 @@ private void populateAllTokensMap(String localeCode, String i18nKey, String keyR private boolean mapContainsPartialToken(Map propertiesMap, String partialToken) { for(Entry entry : propertiesMap.entrySet()) { - String i18nToken = entry.getKey(); - if(i18nToken.contains(partialToken)) - return true; - } + String i18nToken = entry.getKey(); + if(i18nToken.contains(partialToken)) + return true; + } return false; }