This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rhbz1139950 - read config and add trans file resolver
- Loading branch information
Patrick Huang
committed
Sep 15, 2014
1 parent
21938bf
commit 023bea9
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
zanata-client-commands/src/main/java/org/zanata/client/commands/TransFileResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.client.commands; | ||
|
||
import com.google.common.base.Optional; | ||
import org.zanata.client.config.FileMappingRule; | ||
import org.zanata.client.config.LocaleMapping; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Patrick Huang | ||
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
public class TransFileResolver { | ||
private final ConfigurableProjectOptions opts; | ||
|
||
public TransFileResolver(ConfigurableProjectOptions opts) { | ||
this.opts = opts; | ||
} | ||
|
||
public Optional<File> getTransFile(String sourceDocWithExtension, | ||
LocaleMapping localeMapping) { | ||
List<FileMappingRule> fileMappingRules = opts.getFileMappingRules(); | ||
for (FileMappingRule rule : fileMappingRules) { | ||
FileMappingRuleParser parser = new FileMappingRuleParser(rule); | ||
if (parser.isApplicable(sourceDocWithExtension)) { | ||
String relativePath = parser | ||
.getRelativePathFromRule(sourceDocWithExtension, | ||
localeMapping); | ||
return Optional.of(new File(opts.getTransDir(), relativePath)); | ||
} | ||
} | ||
return Optional.absent(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
zanata-client-commands/src/test/java/org/zanata/client/commands/TransFileResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.zanata.client.commands; | ||
|
||
|
||
import com.google.common.base.Optional; | ||
import com.google.common.collect.Lists; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.zanata.client.commands.init.InitOptionsImpl; | ||
import org.zanata.client.config.FileMappingRule; | ||
import org.zanata.client.config.LocaleMapping; | ||
|
||
import java.io.File; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class TransFileResolverTest { | ||
|
||
private TransFileResolver resolver; | ||
private ConfigurableProjectOptions opts; | ||
|
||
@Before | ||
public void setUp() { | ||
// we choose init option as the implementation just because it's simple | ||
opts = new InitOptionsImpl(); | ||
resolver = new TransFileResolver(opts); | ||
} | ||
|
||
@Test | ||
public void canGetTransFileUsingRule() { | ||
opts.setTransDir(new File(".")); | ||
opts.setFileMappingRules(Lists.newArrayList( | ||
new FileMappingRule("**/*.pot", | ||
"{path}/{locale_with_underscore}.po"), | ||
new FileMappingRule("**/*.properties", | ||
"{path}/{filename}_{locale_with_underscore}.{extension}"))); | ||
Optional<File> gettext = | ||
resolver.getTransFile("gcc/po/gcc.pot", new LocaleMapping("de-DE")); | ||
|
||
assertThat(gettext.get().getPath(), equalTo("./gcc/po/de_DE.po")); | ||
|
||
Optional<File> prop = resolver | ||
.getTransFile("src/main/resources/messages.properties", | ||
new LocaleMapping("zh")); | ||
assertThat(prop.get().getPath(), equalTo( | ||
"./src/main/resources/messages_zh.properties")); | ||
|
||
Optional<File> noMatching = resolver | ||
.getTransFile("doc/marketting.odt", new LocaleMapping("ja")); | ||
assertThat(noMatching.isPresent(), equalTo(false)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters