diff --git a/zanata-cli/src/test/java/org/zanata/client/ZanataClientTest.java b/zanata-cli/src/test/java/org/zanata/client/ZanataClientTest.java index 02b64399..2b8e0235 100644 --- a/zanata-cli/src/test/java/org/zanata/client/ZanataClientTest.java +++ b/zanata-cli/src/test/java/org/zanata/client/ZanataClientTest.java @@ -3,6 +3,7 @@ import java.io.StringWriter; import java.net.URL; +import org.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -183,6 +184,7 @@ Mockito. anyObject())).thenReturn( client.processArgs(command, "--url", url, "--project", project, "--project-version", version); + assertThat(err.toString(), CoreMatchers.is("")); verify(mockOptions).setUrl(new URL(url)); verify(mockOptions).setProj(project); verify(mockOptions).setProjectVersion(version); diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/AbstractPushPullOptionsImpl.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/AbstractPushPullOptionsImpl.java index 19c35f42..ab43ef30 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/AbstractPushPullOptionsImpl.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/AbstractPushPullOptionsImpl.java @@ -52,8 +52,9 @@ public abstract class AbstractPushPullOptionsImpl * Override the parent method as the push and pull commands can have locales * specified via command line. * - * @return The locale map list taking into account the global locales in - * zanata.xml as well as the command line argument ones. + * @return The locale map list taking into account the global locales + * configured as well as the command line argument + * ones. */ @Override public LocaleList getLocaleMapList() { diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/OptionsUtil.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/OptionsUtil.java index 4bd5852f..af8f7e24 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/OptionsUtil.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/OptionsUtil.java @@ -2,6 +2,7 @@ import java.io.File; import java.net.URISyntaxException; +import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -15,14 +16,19 @@ import org.zanata.client.config.ConfigUtil; import org.zanata.client.config.FileMappingRule; import org.zanata.client.config.LocaleList; +import org.zanata.client.config.LocaleMapping; import org.zanata.client.config.ZanataConfig; import org.zanata.client.exceptions.ConfigException; +import org.zanata.rest.client.ProjectIterationLocalesClient; import org.zanata.rest.client.RestClientFactory; +import org.zanata.rest.dto.LocaleDetails; import org.zanata.util.VersionUtility; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Question; import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Warning; @@ -42,6 +48,7 @@ public class OptionsUtil { */ public static void applyConfigFiles(ConfigurableOptions opts) throws ConfigurationException, JAXBException { + boolean shouldFetchLocalesFromServer = false; if (opts instanceof ConfigurableProjectOptions) { ConfigurableProjectOptions projOpts = (ConfigurableProjectOptions) opts; @@ -59,6 +66,15 @@ public static void applyConfigFiles(ConfigurableOptions opts) // zanata.ini, // so we apply it first applyProjectConfig(projOpts, projectConfig); + boolean localesDefinedInFile = + projectConfig.getLocales() != null + && !projectConfig.getLocales().isEmpty(); + if (localesDefinedInFile) { + ConsoleInteractorImpl console = new ConsoleInteractorImpl(); + console.printfln(Warning, _("locales.in.config.deprecated")); + } else { + shouldFetchLocalesFromServer = true; + } } else { log.warn("Project config file '{}' not found; ignoring.", projectConfigFile); @@ -77,6 +93,34 @@ public static void applyConfigFiles(ConfigurableOptions opts) opts.getUserConfig()); } } + // we have to wait until user config has been applied + if (shouldFetchLocalesFromServer) { + ConfigurableProjectOptions projectOptions = + (ConfigurableProjectOptions) opts; + LocaleList localeMappings = fetchLocalesFromServer(projectOptions); + projectOptions.setLocaleMapList(localeMappings); + } + } + + private static LocaleList fetchLocalesFromServer( + ConfigurableProjectOptions projectOpts) { + LocaleList localeList = new LocaleList(); + ProjectIterationLocalesClient projectIterationLocalesClient = + createClientFactoryWithoutVersionCheck(projectOpts) + .getProjectLocalesClient(projectOpts.getProj(), + projectOpts.getProjectVersion()); + List localeMappings = + Lists.transform(projectIterationLocalesClient.getLocales(), + new Function() { + @Override + public LocaleMapping apply(LocaleDetails input) { + return input == null ? null : new LocaleMapping( + input.getLocaleId().getId(), + input.getAlias()); + } + }); + localeList.addAll(localeMappings); + return localeList; } /** @@ -101,8 +145,8 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts, } applySrcDirAndTransDirFromProjectConfig(opts, config); applyIncludesAndExcludesFromProjectConfig(opts, config); - LocaleList locales = config.getLocales(); - opts.setLocaleMapList(locales); + LocaleList localesInFile = config.getLocales(); + opts.setLocaleMapList(localesInFile); if (opts.getCommandHooks().isEmpty() && config.getHooks() != null) { opts.setCommandHooks(config.getHooks()); diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/PushPullCommand.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/PushPullCommand.java index 5425839d..ac620ad0 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/PushPullCommand.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/PushPullCommand.java @@ -170,13 +170,15 @@ protected List getDocListForProjectIterationFromServer() { * Filters the project's list of locales * * @param projectLocales - * locales defined by the project's zanata.xml + * locales defined by the project on the server or in zanata.xml + * (only for backward compatibility) * @param locales * locales requested by the user (eg Maven param, command line * option) * @return the filtered list of locales * @throws ConfigException - * if one of the locales was not found in zanata.xml + * if one of the requested locales was not found on the server + * or in zanata.xml (only for backward compatibility) */ public static LocaleList getLocaleMapList(LocaleList projectLocales, String[] locales) { @@ -200,7 +202,8 @@ public static LocaleList getLocaleMapList(LocaleList projectLocales, if (!foundLocale) { throw new ConfigException("Specified locale '" + locale - + "' was not found in zanata.xml!"); + + "' was not found! Available locales: " + + projectLocales); } } return effectiveLocales; diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PullOptionsImpl.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PullOptionsImpl.java index 85ad86bb..42e22c4c 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PullOptionsImpl.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/pull/PullOptionsImpl.java @@ -73,7 +73,7 @@ public String getCommandDescription() { @Option(aliases = { "-l" }, name = "--locales", metaVar = "LOCALE1,LOCALE2", usage = "Locales to pull from the server.\n" - + "By default all locales in zanata.xml will be pulled.") + + "By default all locales configured will be pulled.") public void setLocales(String locales) { this.locales = locales.split(","); } diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextDirStrategy.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextDirStrategy.java index 7f0e843d..9ee9eb89 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextDirStrategy.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextDirStrategy.java @@ -45,7 +45,7 @@ List findLocales(String srcDocName) { LocaleList localeListInConfig = getOpts().getLocaleMapList(); if (localeListInConfig == null || localeListInConfig.isEmpty()) { - log.warn("No locale list in configuration (check zanata.xml)"); + log.warn("No locale list in configuration (check your server settings)"); return localesFoundOnDisk; } @@ -62,7 +62,7 @@ List findLocales(String srcDocName) { if (localesFoundOnDisk.size() == 0) { log.warn( "'pushType' is set to '{}', but none of the configured locale " - + "files was found (check zanata.xml)", getOpts() + + "files was found (check your server settings)", getOpts() .getPushType()); } diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextPushStrategy.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextPushStrategy.java index b4ae0fba..dcc63126 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextPushStrategy.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/GettextPushStrategy.java @@ -30,7 +30,7 @@ List findLocales(String srcDocName) { final LocaleList localeListInConfig = getOpts().getLocaleMapList(); if (localeListInConfig == null || localeListInConfig.isEmpty()) { - log.warn("No locale list in configuration (check zanata.xml)"); + log.warn("No locale list in configuration (check your server settings)"); return Collections.emptyList(); } @@ -46,7 +46,7 @@ List findLocales(String srcDocName) { // for all remaining po files we give a warning for (File transFile : transFilesOnDisk) { log.warn( - "Skipping file {}; no locale entry found in zanata.xml", + "Skipping file {}; no locale entry found from project config", transFile); } return localeListInConfig; diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushCommand.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushCommand.java index 6c378a9e..bda35eff 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushCommand.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushCommand.java @@ -339,7 +339,7 @@ private void pushCurrentModule() throws IOException, RuntimeException { if (pushTrans() && getOpts().getLocaleMapList() == null) { throw new ConfigException("pushType set to '" + getOpts().getPushType() - + "', but zanata.xml contains no "); + + "', but project has no locales configured"); } if (pushTrans()) { diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushOptionsImpl.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushOptionsImpl.java index bc97602c..f13c731f 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushOptionsImpl.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PushOptionsImpl.java @@ -94,7 +94,7 @@ public void setSourceLang(String sourceLang) { @Option(aliases = { "-l" }, name = "--locales", metaVar = "LOCALE1,LOCALE2,...", usage = "Locales to push to the server.\n" - + "By default all locales in zanata.xml will be pushed.") + + "By default all locales configured will be pushed.") public void setLocales(String locales) { this.locales = locales.split(","); } @@ -218,8 +218,8 @@ public boolean getExcludeLocaleFilenames() { @Option( name = "--exclude-locale-filenames", handler = BooleanValueHandler.class, - usage = "Exclude filenames which match locales in zanata.xml (other than the\n" - + "source locale). For instance, if zanata.xml includes de and fr,\n" + usage = "Exclude filenames which match project configured locales (other than the\n" + + "source locale). For instance, if project includes de and fr,\n" + "then the files messages_de.properties and messages_fr.properties\n" + "will not be treated as source files.\n" + "NB: This parameter will be ignored for some project types which use\n" diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java index 2a92cd48..814d2e0c 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java @@ -166,7 +166,7 @@ public void run() throws IOException { if (getOpts().getLocaleMapList() == null) throw new ConfigException("pushType set to '" + getOpts().getPushType() - + "', but zanata.xml contains no "); + + "', but project has no locales configured"); log.warn("pushType set to '" + getOpts().getPushType() + "': existing translations on server may be overwritten/deleted"); diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java index a4c1f919..d6f4de5f 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushStrategy.java @@ -57,7 +57,7 @@ public static interface TranslationFilesVisitor { public void visitTranslationFiles(String sourceDocument, TranslationFilesVisitor visitor) { if (getOpts().getLocaleMapList() == null) { - log.error("Locale mapping list not found, unable to push translations. Check mapping in zanata.xml"); + log.error("Locale mapping list not found, unable to push translations. Check your server settings."); return; } for (LocaleMapping localeMapping : getOpts().getLocaleMapList()) { diff --git a/zanata-client-commands/src/main/resources/prompts.properties b/zanata-client-commands/src/main/resources/prompts.properties index 7f09c78b..d9a93f5a 100644 --- a/zanata-client-commands/src/main/resources/prompts.properties +++ b/zanata-client-commands/src/main/resources/prompts.properties @@ -89,3 +89,5 @@ no.default.mapping=Can not find default translation file mapping rule for projec invalid.rule=Rule defined is not valid (must contain locale): %s unrecognized.variables=Valid variables are %s. Anything else in '%s' will be treated as literal value. confirm.rule=Continue with current mapping rule(s) (y/n)? + +locales.in.config.deprecated=Locale mappings are now handled using locale aliases on the server, so locale mappings in the project config file (zanata.xml) are now deprecated.\nPlease add a locale alias in the project language settings to replace each locale mapping in zanata.xml, then remove the section from zanata.xml. diff --git a/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushMojo.java b/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushMojo.java index 2915bc24..bf7c5ce8 100644 --- a/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushMojo.java +++ b/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushMojo.java @@ -123,10 +123,10 @@ public PushPullCommand initCommand() { private boolean caseSensitive = true; /** - * Exclude filenames which match locales in zanata.xml (other than the - * source locale). For instance, if zanata.xml includes de and fr, then the - * files messages_de.properties and messages_fr.properties will not be - * treated as source files. + * Exclude filenames which match locales configured for the project (other + * than the source locale). For instance, if project includes de and fr, + * then the files messages_de.properties and messages_fr.properties will not + * be treated as source files. *

* NB: This parameter will be ignored for some project types which use * different file naming conventions (eg podir, gettext). diff --git a/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushPullMojo.java b/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushPullMojo.java index 776aad9e..b1c4338d 100644 --- a/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushPullMojo.java +++ b/zanata-maven-plugin/src/main/java/org/zanata/maven/AbstractPushPullMojo.java @@ -131,8 +131,9 @@ private String toMavenModuleID(MavenProject module) { private String fromDoc; /** - * Locales to push to/pull from the server. By default all locales in - * zanata.xml will be pushed/pulled. Usage: + * Locales to push to/pull from the server. By default all locales + * configured for the project will be pushed/pulled. + * Usage: * -Dzanata.locales=locale1,locale2,locale3 * * @parameter expression="${zanata.locales}" diff --git a/zanata-maven-plugin/src/test/java/org/zanata/maven/PushMojoTest.java b/zanata-maven-plugin/src/test/java/org/zanata/maven/PushMojoTest.java index 8be6f944..3d20bba2 100644 --- a/zanata-maven-plugin/src/test/java/org/zanata/maven/PushMojoTest.java +++ b/zanata-maven-plugin/src/test/java/org/zanata/maven/PushMojoTest.java @@ -1,7 +1,5 @@ package org.zanata.maven; -import java.util.Arrays; - import com.google.common.collect.ImmutableList; import org.zanata.client.commands.push.PushCommand; import org.zanata.client.commands.PushPullType; diff --git a/zanata-maven-plugin/src/test/java/org/zanata/maven/ZanataMojoTest.java b/zanata-maven-plugin/src/test/java/org/zanata/maven/ZanataMojoTest.java index ce34301c..b4a4ef89 100644 --- a/zanata-maven-plugin/src/test/java/org/zanata/maven/ZanataMojoTest.java +++ b/zanata-maven-plugin/src/test/java/org/zanata/maven/ZanataMojoTest.java @@ -1,6 +1,7 @@ package org.zanata.maven; import java.io.File; +import java.net.URL; import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.testing.AbstractMojoTestCase; @@ -34,7 +35,9 @@ protected void tearDown() throws Exception { } protected void applyPomParams(String pomFile) throws Exception { - File testPom = getTestFile("src/test/resources/push-test/" + pomFile); + URL resource = Thread.currentThread().getContextClassLoader() + .getResource("push-test/" + pomFile); + File testPom = new File(resource.getFile()); // This will work with "mvn test", but not with Eclipse's JUnit runner: // PushSimpleMojo mojo = (PushSimpleMojo) lookupMojo("push", testPom); // assertNotNull(mojo); diff --git a/zanata-rest-client/src/main/java/org/zanata/rest/client/ProjectIterationLocalesClient.java b/zanata-rest-client/src/main/java/org/zanata/rest/client/ProjectIterationLocalesClient.java new file mode 100644 index 00000000..dba4d105 --- /dev/null +++ b/zanata-rest-client/src/main/java/org/zanata/rest/client/ProjectIterationLocalesClient.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014, Red Hat, Inc. and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This 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 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.zanata.rest.client; + +import java.util.List; + +import org.zanata.rest.dto.LocaleDetails; + +import com.sun.jersey.api.client.GenericType; + +/** + * REST client for project iteration locales. + * + * @author Patrick Huang pahuang@redhat.com + */ +public class ProjectIterationLocalesClient { + private final RestClientFactory restClientFactory; + private final String projectSlug; + private final String versionSlug; + + public ProjectIterationLocalesClient(RestClientFactory restClientFactory, + String projectSlug, String versionSlug) { + this.restClientFactory = restClientFactory; + this.projectSlug = projectSlug; + this.versionSlug = versionSlug; + } + + public List getLocales() { + return restClientFactory.getClient() + .resource(restClientFactory.getBaseUri()) + .path("projects").path("p").path(projectSlug) + .path("iterations").path("i").path(versionSlug) + .path("locales") + .get(new GenericType>() { + }); + } +} diff --git a/zanata-rest-client/src/main/java/org/zanata/rest/client/RestClientFactory.java b/zanata-rest-client/src/main/java/org/zanata/rest/client/RestClientFactory.java index f40abe57..faac5150 100644 --- a/zanata-rest-client/src/main/java/org/zanata/rest/client/RestClientFactory.java +++ b/zanata-rest-client/src/main/java/org/zanata/rest/client/RestClientFactory.java @@ -257,6 +257,11 @@ public TransDocResourceClient getTransDocResourceClient(String projectSlug, return new TransDocResourceClient(this, projectSlug, versionSlug); } + public ProjectIterationLocalesClient getProjectLocalesClient( + String projectSlug, String versionSlug) { + return new ProjectIterationLocalesClient(this, projectSlug, versionSlug); + } + private static class AcceptAllX509TrustManager implements X509TrustManager { public X509Certificate[] getAcceptedIssuers() { return null;