Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #48 from zanata/locale-aliases
Browse files Browse the repository at this point in the history
rhbz1156236 - deprecate locales in config file and fetch from server
  • Loading branch information
Patrick Huang committed Mar 4, 2015
2 parents 1e4413b + 9a6adde commit 38e5fe8
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 27 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -183,6 +184,7 @@ Mockito.<SubCommand> 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);
Expand Down
Expand Up @@ -52,8 +52,9 @@ public abstract class AbstractPushPullOptionsImpl<O extends PushPullOptions>
* 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() {
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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<LocaleMapping> localeMappings =
Lists.transform(projectIterationLocalesClient.getLocales(),
new Function<LocaleDetails, LocaleMapping>() {
@Override
public LocaleMapping apply(LocaleDetails input) {
return input == null ? null : new LocaleMapping(
input.getLocaleId().getId(),
input.getAlias());
}
});
localeList.addAll(localeMappings);
return localeList;
}

/**
Expand All @@ -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());
Expand Down
Expand Up @@ -170,13 +170,15 @@ protected List<ResourceMeta> 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) {
Expand All @@ -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;
Expand Down
Expand Up @@ -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(",");
}
Expand Down
Expand Up @@ -45,7 +45,7 @@ List<LocaleMapping> 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;
}

Expand All @@ -62,7 +62,7 @@ List<LocaleMapping> 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());
}

Expand Down
Expand Up @@ -30,7 +30,7 @@ List<LocaleMapping> 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();
}

Expand All @@ -46,7 +46,7 @@ List<LocaleMapping> 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;
Expand Down
Expand Up @@ -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 <locales>");
+ "', but project has no locales configured");
}

if (pushTrans()) {
Expand Down
Expand Up @@ -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(",");
}
Expand Down Expand Up @@ -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"
Expand Down
Expand Up @@ -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 <locales>");
+ "', but project has no locales configured");
log.warn("pushType set to '"
+ getOpts().getPushType()
+ "': existing translations on server may be overwritten/deleted");
Expand Down
Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions zanata-client-commands/src/main/resources/prompts.properties
Expand Up @@ -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 <locales> section from zanata.xml.
Expand Up @@ -123,10 +123,10 @@ public PushPullCommand<PushOptions> 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.
* <p>
* NB: This parameter will be ignored for some project types which use
* different file naming conventions (eg podir, gettext).
Expand Down
Expand Up @@ -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}"
Expand Down
@@ -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;
Expand Down
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
@@ -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 <a
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
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<LocaleDetails> getLocales() {
return restClientFactory.getClient()
.resource(restClientFactory.getBaseUri())
.path("projects").path("p").path(projectSlug)
.path("iterations").path("i").path(versionSlug)
.path("locales")
.get(new GenericType<List<LocaleDetails>>() {
});
}
}
Expand Up @@ -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;
Expand Down

0 comments on commit 38e5fe8

Please sign in to comment.