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.
Merge pull request #334 from zanata/gwt-i18n
enable localization of gwt resources
- Loading branch information
Showing
16 changed files
with
299 additions
and
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.zanata-cache |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import com.google.common.io.Files | ||
|
||
// This script will used combine pulled translation files from Zanata server and GWT generated skeleton file. | ||
// This will enable GWT to compile locale permutation for not 100% translated locales. | ||
// It's used in zanata.xml as command hook. | ||
|
||
File gwtGenDir = new File(project.build.directory + "/gwt-extra/webtrans") | ||
assert gwtGenDir.isDirectory() :"target/gwt-extra/webtrans does not exist. You need to have source available. Either pull with source or re-generate source again." | ||
|
||
File basedir = pom.basedir | ||
// zanata pull target dir is here | ||
File pulledTargetDir = new File(basedir.absolutePath + "/src/main/resources/zanata-editor") | ||
|
||
// find properties file with underscore (translation properties) | ||
def targetNames = new FileNameByRegexFinder(). | ||
getFileNames(pulledTargetDir.absolutePath, /.+_.+\.properties$/) | ||
|
||
def targetProps = targetNames.collect { | ||
new File(it) | ||
} | ||
|
||
def ln = System.getProperty("line.separator") | ||
|
||
Map<String, Properties> translationMap = targetProps.collectEntries { | ||
Properties source = new Properties() | ||
it.withReader("UTF-8") { | ||
source.load(it) | ||
} | ||
[it.name, source] | ||
} | ||
|
||
log.info("translation map: {} \n\n\n", translationMap.keySet()) | ||
|
||
// get all GWT generated translation skeletons | ||
def genPropNames = new FileNameByRegexFinder(). | ||
getFileNames(gwtGenDir.absolutePath, /\w+\.properties$/, /_default.properties$/) | ||
|
||
genPropNames.each { | ||
// we move GWT generated skeletons to project build class path but not in source tree | ||
def relativePath = it - gwtGenDir | ||
def skeleton = new File(it) | ||
def finalOutput = new File("$project.build.outputDirectory/$relativePath") | ||
finalOutput.getParentFile().mkdirs() | ||
log.debug("finalOutput: {}", finalOutput.absolutePath) | ||
Files.copy(skeleton, finalOutput) | ||
|
||
def translation = translationMap.get(skeleton.name) | ||
if (!translation) { | ||
log.info("no translation found for {}", skeleton.name) | ||
return | ||
} | ||
def lines = finalOutput.readLines("UTF-8") | ||
lines.eachWithIndex { line, index -> | ||
def pair = line.split(/=/) | ||
if (pair.size() != 2) { | ||
return | ||
} | ||
String key = pair[0] | ||
def textFlowTarget = translation.get(key) | ||
if (textFlowTarget) { | ||
log.debug("fill in {} with translation: {}", key, textFlowTarget) | ||
lines.set(index, "$key=$textFlowTarget") | ||
} | ||
} | ||
|
||
finalOutput.withPrintWriter("UTF-8") { writer -> | ||
lines.each { | ||
writer.append("$it$ln") | ||
} | ||
} | ||
log.info("processed {}", skeleton.name) | ||
} | ||
|
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,96 @@ | ||
import com.google.common.io.Files | ||
|
||
// This script will rename GWT generated properties for Zanata to use. | ||
log.info "===== Preparing GWT generated properties files =====" | ||
|
||
def gwtGenDir = new File(project.build.directory + "/gwt-extra/webtrans") | ||
|
||
assert gwtGenDir.isDirectory() | ||
|
||
def nameFilter = { dir, name -> | ||
name.endsWith(".properties") | ||
} as FilenameFilter | ||
|
||
def properties = gwtGenDir.listFiles(nameFilter) | ||
|
||
|
||
if (!properties) { | ||
log.info "no properties found. quit." | ||
return | ||
} | ||
// scrip off the file name part to get package name | ||
def packageName = properties[0].name.replaceAll(/\.\w+\.properties/, "") | ||
def packagePath = packageName.replaceAll(/\./, "/") | ||
|
||
def resourceDir = new File("$gwtGenDir.absolutePath/$packagePath") | ||
resourceDir.mkdirs() | ||
|
||
int sourceCount = 0 | ||
int targetCount = 0 | ||
|
||
properties.each { | ||
if (it.name.endsWith("_default.properties")) { | ||
log.debug " * found source: $it.name" | ||
// change the name of the file | ||
def fileName = (it.name - "$packageName.").replace("_default", "") | ||
def destFile = new File(resourceDir, fileName) | ||
log.info " moved to: $destFile" | ||
Files.move(it, destFile) | ||
sourceCount++ | ||
} else { | ||
log.debug " * found target: $it.name" | ||
def fileName = (it.name - "$packageName.") | ||
def destFile = new File(resourceDir, fileName) | ||
// we ALWAYS copy generated target skeleton to make sure target is in sync if source has changed. | ||
// Also to make sure enabled locale (in Application.gwt.xml) has something to display if translation is not yet available. | ||
// Later we will base on pulled translation to repopulate the file. | ||
// see FillInTranslationGap.groovy | ||
log.info " moved to :$destFile" | ||
Files.move(it, destFile); | ||
targetCount++ | ||
} | ||
} | ||
|
||
log.info "Generated $sourceCount source(s) and $targetCount target(s) in $resourceDir" | ||
log.info "===== Prepared GWT generated properties files =====" | ||
|
||
// below procedure is to fix GWT's bizarre behavior. | ||
// if we have properties files on classpath (i.e. compile with extra already), | ||
// the second time GWT compiler produces properties file will output plural forms in properties but some of them are empty. | ||
// It will be empty if: | ||
// 1. the required plural form for that language is not defined in java interface. i.e. in Ukranian you ought to have "one", "few", "other" defined in @AlternateMessage. | ||
// see com.google.gwt.i18n.client.impl.plurals.DefaultRule_x1_x234_n | ||
// 2. in java interface it uses complex plural combination. i.e. having multiple @PluralCount in parameters. | ||
// see org.zanata.webtrans.client.resources.WebTransMessages.showingResultsForProjectWideSearch | ||
// First one won't cause any trouble. We can ignore it. | ||
// Second one may not be fixable. The GWT doc says the plural form is still a work in progress. I don't know how it works either. | ||
// We can either add extra plural count in the [] i.e. turn [one] to [one|one]. But Zanata doesn't support mismatch source and target. | ||
// So I removing those extra plural entries which allows GWT to compile again (with a warning). | ||
// see https://github.com/zanata/zanata-server/wiki/Localize-Zanata for more detail. | ||
|
||
def filter = { | ||
it.name.endsWith(".properties") | ||
} as FileFilter | ||
properties = resourceDir.listFiles(filter) | ||
|
||
def ln = System.getProperty("line.separator") | ||
|
||
properties.each { | ||
def lines = it.readLines("UTF-8") | ||
boolean touched = false | ||
lines.eachWithIndex { line, index -> | ||
if (line.matches(/.+=$/)) { | ||
log.info("found and removed empty plural entry: {}", line) | ||
lines.set(index, "") | ||
touched = true | ||
} | ||
} | ||
if (touched) { | ||
log.info("processed {}", it.name) | ||
it.withPrintWriter("UTF-8") { writer -> | ||
lines.each { | ||
writer.append("$it$ln") | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
13 changes: 13 additions & 0 deletions
13
zanata-war/src/main/java/org/zanata/webtrans/ApplicationI18n.gwt.xml
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,13 @@ | ||
<!-- | ||
set-property-fallback is missing from the DTD: | ||
<!DOCTYPE module PUBLIC "//gwt-module/" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.3/distro-source/core/src/gwt-module.dtd"> | ||
--> | ||
<module rename-to="webtrans"> | ||
<inherits name="org.zanata.webtrans.Application" /> | ||
<set-property name="user.agent" value="safari" /> | ||
<extend-property name="log_level" values="DEBUG" /> | ||
<set-property name="log_level" value="DEBUG" /> | ||
|
||
<!-- add locales to generate different properties template--> | ||
<set-property name="locale" value="default,en,ja,zh_Hant_TW,uk" /> | ||
</module> |
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
Oops, something went wrong.