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

Commit

Permalink
Browse files Browse the repository at this point in the history
rhbz736898 - Implement -Dzanata.locales option on maven client.
The new option allows users to push documents and/or translations for specific locales.
  • Loading branch information
Carlos Munoz committed Apr 30, 2012
1 parent 9a4be04 commit 8c9762f
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 40 deletions.
Expand Up @@ -61,12 +61,12 @@ public File getProjectConfig()
return projectConfig;
}

public LocaleList getLocales()
public LocaleList getLocaleMapList()
{
return locales;
}

public void setLocales(LocaleList locales)
public void setLocaleMapList(LocaleList locales)
{
this.locales = locales;
}
Expand Down
Expand Up @@ -36,8 +36,8 @@ public interface ConfigurableProjectOptions extends ConfigurableOptions

public File getProjectConfig();

public LocaleList getLocales();
public LocaleList getLocaleMapList();

public void setLocales(LocaleList locales);
public void setLocaleMapList(LocaleList locales);

}
Expand Up @@ -102,13 +102,13 @@ public File getProjectConfig()
}

@Override
public LocaleList getLocales()
public LocaleList getLocaleMapList()
{
return locales;
}

@Override
public void setLocales(LocaleList locales)
public void setLocaleMapList(LocaleList locales)
{
this.locales = locales;
}
Expand Down
Expand Up @@ -93,7 +93,7 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts, ZanataCo
opts.setProjectType(config.getProjectType());
}
LocaleList locales = config.getLocales();
opts.setLocales(locales);
opts.setLocaleMapList(locales);
}

/**
Expand Down
Expand Up @@ -83,7 +83,7 @@ public void run() throws Exception
}
log.info("PO base directory (translations): {}", getOpts().getDstDir());

LocaleList locales = getOpts().getLocales();
LocaleList locales = getOpts().getLocaleMapList();
if (locales == null)
throw new ConfigException("no locales specified");
PoWriter2 poWriter = new PoWriter2();
Expand Down
Expand Up @@ -172,9 +172,9 @@ else if (getOpts().isInteractiveMode())
List<LocaleMapping> locales = null;
if (getOpts().getImportPo())
{
if (getOpts().getLocales() != null)
if (getOpts().getLocaleMapList() != null)
{
locales = PublicanUtil.findLocales(getOpts().getSrcDir(), getOpts().getLocales());
locales = PublicanUtil.findLocales(getOpts().getSrcDir(), getOpts().getLocaleMapList());
if (locales.size() == 0)
{
log.warn("option 'importPo' is set, but none of the configured locale directories was found (check zanata.xml)");
Expand Down
Expand Up @@ -42,9 +42,9 @@ public abstract class AbstractGlossaryPushReader

protected LocaleId getLocaleFromMap(String localLocale)
{
if (!getOpts().getLocales().isEmpty())
if (!getOpts().getLocaleMapList().isEmpty())
{
for (LocaleMapping loc : getOpts().getLocales())
for (LocaleMapping loc : getOpts().getLocaleMapList())
{
if (loc.getLocalLocale().equals(localLocale))
{
Expand Down
Expand Up @@ -85,7 +85,7 @@ private void logOptions()
}
}
}
log.info("Locales to pull: {}", getOpts().getLocales());
log.info("Locales to pull: {}", getOpts().getLocaleMapList());
if (getOpts().getPullSrc())
{
log.info("Pulling source and target (translation) documents");
Expand All @@ -109,7 +109,7 @@ public void run() throws Exception
{
logOptions();

LocaleList locales = getOpts().getLocales();
LocaleList locales = getOpts().getLocaleMapList();
if (locales == null)
throw new ConfigException("no locales specified");
PullStrategy strat = getStrategy(getOpts().getProjectType());
Expand Down
Expand Up @@ -83,7 +83,7 @@ public String[] getSrcFiles(File srcDir, List<String> includes, List<String> exc

private void excludeLocaleFileName(List<String> excludes)
{
for (LocaleMapping locMap : opts.getLocales())
for (LocaleMapping locMap : opts.getLocaleMapList())
{
String loc = locMap.getJavaLocale().toLowerCase();
excludes.add("**/*_" + loc + fileExtension);
Expand Down
Expand Up @@ -93,9 +93,9 @@ private List<LocaleMapping> findLocales()
return locales;
if (getOpts().getPushType() == PushType.Both || getOpts().getPushType() == PushType.Trans)
{
if (getOpts().getLocales() != null)
if (getOpts().getLocaleMapList() != null)
{
locales = PublicanUtil.findLocales(getOpts().getTransDir(), getOpts().getLocales());
locales = PublicanUtil.findLocales(getOpts().getTransDir(), getOpts().getLocaleMapList());
if (locales.size() == 0)
{
log.warn("'pushType' is set to '" + getOpts().getPushType() + "', but none of the configured locale " +
Expand Down
Expand Up @@ -113,7 +113,7 @@ private TranslationsResource loadTranslationsResource(Resource srcDoc, File tran
@Override
public void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor callback) throws IOException
{
for (LocaleMapping locale : getOpts().getLocales())
for (LocaleMapping locale : getOpts().getLocaleMapList())
{
String filename = docNameToFilename(docName, locale);
File transFile = new File(getOpts().getTransDir(), filename);
Expand Down
Expand Up @@ -18,6 +18,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.PushPullCommand;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.LocaleMapping;
import org.zanata.client.exceptions.ConfigException;
import org.zanata.common.LocaleId;
Expand Down Expand Up @@ -112,7 +113,7 @@ private void logOptions()
if (getOpts().getPushType() == PushType.Trans)
{
log.info("Pushing target documents only");
log.info("Locales to push: {}", getOpts().getLocales());
log.info("Locales to push: {}", getOpts().getLocaleMapList());
}
else if(getOpts().getPushType() == PushType.Source)
{
Expand All @@ -121,7 +122,7 @@ else if(getOpts().getPushType() == PushType.Source)
else
{
log.info("Pushing source and target documents");
log.info("Locales to push: {}", getOpts().getLocales());
log.info("Locales to push: {}", getOpts().getLocaleMapList());
}
log.info("Source directory (originals): {}", getOpts().getSrcDir());
if (getOpts().getPushType() == PushType.Both || getOpts().getPushType() == PushType.Trans)
Expand Down Expand Up @@ -246,7 +247,7 @@ private void pushCurrentModule() throws IOException

if (getOpts().getPushType() == PushType.Trans || getOpts().getPushType() == PushType.Both )
{
if (getOpts().getLocales() == null)
if (getOpts().getLocaleMapList() == null)
throw new ConfigException("pushType set to '" + getOpts().getPushType() + "', but zanata.xml contains no <locales>");
log.warn("pushType set to '" + getOpts().getPushType() + "': existing translations on server may be overwritten/deleted");

Expand Down
Expand Up @@ -79,7 +79,7 @@ public Resource loadSrcDoc(File sourceDir, String docName) throws IOException
@Override
public void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor visitor) throws FileNotFoundException
{
for (LocaleMapping locale : getOpts().getLocales())
for (LocaleMapping locale : getOpts().getLocaleMapList())
{
String filename = docNameToFilename(docName, locale);
File transFile = new File(getOpts().getTransDir(), filename);
Expand Down
Expand Up @@ -97,7 +97,7 @@ public void visitTranslationResources(String docName, Resource srcDoc, Translati
{
try
{
for (LocaleMapping locale : getOpts().getLocales())
for (LocaleMapping locale : getOpts().getLocaleMapList())
{
String filename = docNameToFilename(docName, locale);
File transFile = new File(getOpts().getTransDir(), filename);
Expand Down
Expand Up @@ -78,7 +78,7 @@ private void publicanPull(boolean exportPot, boolean mapLocale) throws Exception
{
LocaleList locales = new LocaleList();
locales.add(new LocaleMapping("ja", "ja-JP"));
opts.setLocales(locales);
opts.setLocaleMapList(locales);
}

List<ResourceMeta> resourceMetaList = new ArrayList<ResourceMeta>();
Expand Down
Expand Up @@ -83,7 +83,7 @@ private void publicanPush(boolean importPo, boolean mapLocale) throws Exception
{
LocaleList locales = new LocaleList();
locales.add(new LocaleMapping("ja", "ja-JP"));
opts.setLocales(locales);
opts.setLocaleMapList(locales);
}

List<ResourceMeta> resourceMetaList = new ArrayList<ResourceMeta>();
Expand Down
Expand Up @@ -31,8 +31,6 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.zanata.client.commands.glossary.push.GlossaryCSVReader;
import org.zanata.client.commands.glossary.push.GlossaryPushOptions;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.LocaleMapping;
import org.zanata.rest.dto.Glossary;
Expand Down Expand Up @@ -82,7 +80,7 @@ public void extractGlossaryTest1() throws IOException
mockPushOption = createMock("mockPushGlossaryOption", GlossaryPushOptions.class);
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getTransLang()).andReturn("hi").anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getTreatSourceCommentsAsTarget()).andReturn(false).anyTimes();
EasyMock.expect(mockPushOption.getCommentCols()).andReturn(commentHeaders).anyTimes();

Expand Down Expand Up @@ -113,7 +111,7 @@ public void extractGlossaryTest2() throws IOException
mockPushOption = createMock("mockPushGlossaryOption", GlossaryPushOptions.class);
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getTransLang()).andReturn("hi").anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getTreatSourceCommentsAsTarget()).andReturn(false).anyTimes();
EasyMock.expect(mockPushOption.getCommentCols()).andReturn(commentHeaders).anyTimes();

Expand Down
Expand Up @@ -74,7 +74,7 @@ public void extractGlossaryTest() throws IOException
mockPushOption = createMock("mockPushGlossaryOption", GlossaryPushOptions.class);
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getTransLang()).andReturn("hi").anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getTreatSourceCommentsAsTarget()).andReturn(false).anyTimes();

reader.setOpts(mockPushOption);
Expand Down
Expand Up @@ -97,7 +97,7 @@ private void push(boolean pushTrans, boolean mapLocale) throws Exception
{
locales.add(new LocaleMapping("ja-JP"));
}
opts.setLocales(locales);
opts.setLocaleMapList(locales);

List<ResourceMeta> resourceMetaList = new ArrayList<ResourceMeta>();
resourceMetaList.add(new ResourceMeta("obsolete"));
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void findDocNamesTest() throws IOException
include.add("**/**StringResource_en_US*");

// EasyMock.expect(mockPushOption.getIncludes()).andReturn(include);
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getDefaultExcludes()).andReturn(true).anyTimes();

Expand All @@ -87,7 +87,7 @@ public void loadSrcDocTest() throws IOException

// EasyMock.expect(mockPushOption.getIncludes()).andReturn(include);
EasyMock.expect(mockPushOption.getTransDir()).andReturn(sourceDir).anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getDefaultExcludes()).andReturn(true).anyTimes();

Expand Down Expand Up @@ -134,7 +134,7 @@ public void loadSrcDocTestWithExcludeFileOption() throws IOException

// EasyMock.expect(mockPushOption.getIncludes()).andReturn(include);
EasyMock.expect(mockPushOption.getTransDir()).andReturn(sourceDir).anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getDefaultExcludes()).andReturn(true).anyTimes();

Expand Down Expand Up @@ -185,7 +185,7 @@ public void loadSrcDocTestWithExcludeOption() throws IOException

// EasyMock.expect(mockPushOption.getIncludes()).andReturn(include);
EasyMock.expect(mockPushOption.getTransDir()).andReturn(sourceDir).anyTimes();
EasyMock.expect(mockPushOption.getLocales()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getLocaleMapList()).andReturn(locales).anyTimes();
EasyMock.expect(mockPushOption.getSourceLang()).andReturn("en-US").anyTimes();
EasyMock.expect(mockPushOption.getDefaultExcludes()).andReturn(true).anyTimes();

Expand Down
Expand Up @@ -54,7 +54,7 @@ public abstract class ConfigurableProjectMojo<O extends ConfigurableOptions> ext
*/
private String projectType;

private LocaleList locales;
private LocaleList localeMapList;

public ConfigurableProjectMojo()
{
Expand Down Expand Up @@ -110,15 +110,15 @@ public void setProjectType(String projectType)
}

@Override
public LocaleList getLocales()
public LocaleList getLocaleMapList()
{
return locales;
return localeMapList;
}

@Override
public void setLocales(LocaleList locales)
public void setLocaleMapList(LocaleList localeMapList)
{
this.locales = locales;
this.localeMapList = localeMapList;
}

}
60 changes: 60 additions & 0 deletions zanata-maven-plugin/src/main/java/org/zanata/maven/PushMojo.java
Expand Up @@ -8,6 +8,9 @@
import org.zanata.client.commands.push.PushCommand;
import org.zanata.client.commands.push.PushOptions;
import org.zanata.client.commands.push.PushType;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.LocaleMapping;
import org.zanata.client.exceptions.ConfigException;

/**
* Pushes source text to a Zanata project version so that it can be translated, and optionally push translated text as well.
Expand Down Expand Up @@ -111,6 +114,18 @@ public PushCommand initCommand()
*/
private boolean deleteObsoleteModules;

/**
* Locales to push to the server.
* By default all locales in zanata.xml will be pushed.
* Usage: -Dzanata.locales=locale1,locale2,locale3
*
* @parameter expression="${zanata.locales}"
*/
private String[] locales;

// Cached copy of the effective locales to avoid calculating it more than once
private LocaleList effectiveLocales;


@Override
public String getSourceLang()
Expand Down Expand Up @@ -180,4 +195,49 @@ public boolean getDefaultExcludes()
return defaultExcludes;
}

/**
* Override the default {@link org.zanata.maven.ConfigurableProjectMojo#getLocaleMapList()} method as the push
* command 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.
*/
@Override
public LocaleList getLocaleMapList()
{
if( effectiveLocales == null )
{
if(locales != null && locales.length > 0)
{
// filter the locales that are specified in both the global config and the parameter list
effectiveLocales = new LocaleList();

for( String locale : locales )
{
boolean foundLocale = false;
for(LocaleMapping lm : super.getLocaleMapList())
{
if( lm.getLocale().equals(locale) ||
(lm.getMapFrom() != null && lm.getMapFrom().equals( locale )) )
{
effectiveLocales.add(lm);
foundLocale = true;
break;
}
}

if(!foundLocale)
{
throw new ConfigException("Specified locale '" + locale + "' was not found in zanata.xml!" );
}
}
}
else
{
effectiveLocales = super.getLocaleMapList();
}
}

return effectiveLocales;
}
}

0 comments on commit 8c9762f

Please sign in to comment.