All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.plugin.bundle.CheckI18nArtifactsMojo Maven / Gradle / Ivy

package org.nuiton.i18n.plugin.bundle;

/*-
 * #%L
 * I18n :: Maven Plugin
 * %%
 * Copyright (C) 2007 - 2017 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.base.Joiner;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.nuiton.i18n.bundle.I18nBundleUtil;
import org.nuiton.plugin.PluginHelper;

/**
 * Performs some checks on i18n artifacts (remember a i18n artifact is just a maven module using i18n API (it can be
 * your module or any dependency)).
 * 
    *
  • Check convergence : means if a key is present in more than one bundle, then it must have exactly the same translation
  • *
  • Check integrity : means that a key should be present for all locales
  • *
* Created by tchemit on 12/07/17. * * @author Tony Chemit - [email protected] * @since 4.0 */ @Mojo(name = "check-i18n-artifacts", defaultPhase = LifecyclePhase.INITIALIZE) @Execute(goal = "collect-i18n-artifacts") public class CheckI18nArtifactsMojo extends AbstractI18nBundleMojo { /** * To check if i18n artifacts converge. */ @Parameter(property = "i18n.checkConvergence", defaultValue = "true") private boolean checkConvergence; /** * To check i18n artifacts integrity. */ @Parameter(property = "i18n.checkIntegrity", defaultValue = "true") private boolean checkIntegrity; @Override public void init() throws Exception { super.init(); createDirectoryIfNecessary(collectDirectory); } @Override protected boolean checkSkip() { boolean result = super.checkSkip(); if (result && !needInvoke(true, false, getProjectCacheKey())) { getLog().info("Skip - already executed."); result = false; } return result; } @Override protected void doAction() throws Exception { Multimap allKeysByLocale = HashMultimap.create(); Map bundlesCount = new LinkedHashMap<>(); for (Locale locale : locales) { Map keys = new TreeMap<>(); URL[] urls = getCollectI18nResources(locale); bundlesCount.put(locale, urls.length); for (URL url : urls) { Properties p = new Properties(); try (Reader reader = new InputStreamReader(url.openStream(), charset)) { p.load(reader); } for (Map.Entry entry : p.entrySet()) { String key = (String) entry.getKey(); keys.putIfAbsent(key, new I18nKey(locale, key)); I18nKey i18nKey = keys.get(key); i18nKey.addTranslation(url.toString(), (String) entry.getValue()); allKeysByLocale.put(locale, key); } } if (checkConvergence) { getLog().info(String.format("Checking convergence for locale %s", locale)); } if (checkConvergence && keys.values().stream().anyMatch(I18nKey::isNotValid)) { StringBuilder builder = new StringBuilder(); // have some bad keys Set badI18nKeys = keys.values().stream().filter(I18nKey::isNotValid).collect(Collectors.toSet()); for (I18nKey i18nKey : badI18nKeys) { builder.append("\n").append(i18nKey.getKey()); for (Map.Entry entry : i18nKey.getTranslations().entrySet()) { String bundle = entry.getKey(); String translation = entry.getValue(); builder.append(String.format("\n\tbundle %s:\n\t\t%s", bundle, translation)); } } throw new MojoExecutionException(String.format("For locale %s, there is %d divergent i18n key(s):%s", locale, badI18nKeys.size(), builder.toString())); } else { getLog().info(String.format("%d bundle(s) converge - %d translation(s)", urls.length, keys.size())); } } if (checkIntegrity) { Set allKeys = new HashSet<>(allKeysByLocale.values()); for (Map.Entry> entry : allKeysByLocale.asMap().entrySet()) { Locale locale = entry.getKey(); getLog().info(String.format("Checking integrity for locale %s", locale)); Set keys = new TreeSet<>(allKeys); keys.removeAll(entry.getValue()); if (!keys.isEmpty()) { throw new MojoExecutionException(String.format("For locale %s, there is missing %d key(s):\n%s", locale, keys.size(), Joiner.on("\n").join(keys))); } else { getLog().info(String.format("%d bundle(s) are upstanding - %d translation(s)", bundlesCount.get(locale), allKeys.size())); } } } } @Override protected URL[] getCollectI18nResources(Locale locale) throws IOException { File file = getCollectOutputFile(locale, false); if (!file.exists()) { return I18nBundleUtil.EMPTY_URL_ARRAY; } return PluginHelper.getLinesAsURL(file); } /** * Created by tchemit on 12/07/17. * * @author Tony Chemit - [email protected] * @since 4.0 */ public static class I18nKey { private final Locale locale; private final String key; private final Map translations; I18nKey(Locale locale, String key) { this.locale = locale; this.key = key; this.translations = new TreeMap<>(); } void addTranslation(String bundle, String translation) { translations.put(bundle, translation); } public Locale getLocale() { return locale; } public String getKey() { return key; } /** * @return {@code true} if i18n key is valid, means all translations are the same, {@code false} otherwise. */ boolean isValid() { return new HashSet<>(translations.values()).size() == 1; } boolean isNotValid() { return !isValid(); } public Map getTranslations() { return translations; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy