You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2015/02/17 15:06:39 UTC
[11/13] isis git commit: ISIS-903: working towards parsing plural
form (not there yet)
ISIS-903: working towards parsing plural form (not there yet)
Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/e315c504
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/e315c504
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/e315c504
Branch: refs/heads/ISIS-903
Commit: e315c504b770939018884550e9362145867cb14e
Parents: 02c5363
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Tue Feb 17 11:57:33 2015 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Tue Feb 17 14:04:26 2015 +0000
----------------------------------------------------------------------
.../core/metamodel/services/i18n/po/Block.java | 83 +++++++++++++++-----
.../services/i18n/po/PoReaderTest.java | 39 +++++++++
2 files changed, 103 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/isis/blob/e315c504/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/i18n/po/Block.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/i18n/po/Block.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/i18n/po/Block.java
index d4543ad..45af493 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/i18n/po/Block.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/i18n/po/Block.java
@@ -9,43 +9,88 @@ import com.google.common.collect.Lists;
class Block {
private enum State {
- CONTEXT,
- MSGID,
- MSGSTR
- }
+ CONTEXT("^#: (?<value>.+)$"),
+ MSGID("^msgid \"(?<value>.+)\"$"),
+ MSGID_PLURAL("^msgid_plural \"(?<value>.+)\"$"),
+ MSGSTR("^msgstr \"(?<value>.+)\"$"),
+ MSGSTR0("^msgstr\\[0\\] \"(?<value>.+)\"$"),
+ MSGSTR1("^msgstr\\[1\\] \"(?<value>.+)\"$");
- private static final Pattern contextPattern = Pattern.compile("^#: (?<context>.+)$");
- private static final Pattern msgidPattern = Pattern.compile("^msgid \"(?<msgid>.+)\"$");
- private static final Pattern msgstrPattern = Pattern.compile("^msgstr \"(?<msgstr>.+)\"$");
+ private final Pattern pattern;
+
+ private State(final String regex) {
+ pattern = Pattern.compile(regex);
+ }
+ }
State state = State.CONTEXT;
List<String> contextList = Lists.newArrayList();
String msgid = null;
- String msgstr = null;
+ String msgid_plural = null;
+ String msgstr = null; // either from msgstr or msgstr[0] if there is a plural
+ String msgstr_plural = null; // from msgstr[1]
Block parseLine(final String line, final Map<MsgIdAndContext, String> translationsByKey) {
if (state == State.CONTEXT) {
- final Matcher matcher = contextPattern.matcher(line);
- if (matcher.matches()) {
- final String context = matcher.group("context");
+ final Matcher contextMatcher = state.pattern.matcher(line);
+ if (contextMatcher.matches()) {
+ final String context = contextMatcher.group("value");
contextList.add(context);
+ return this;
} else {
state = State.MSGID;
+ // fallthrough (there may not have been any more context)
}
}
+
if (state == State.MSGID) {
- final Matcher matcher = msgidPattern.matcher(line);
- if (matcher.matches()) {
- msgid = matcher.group("msgid");
+ final Matcher msgidMatcher = state.pattern.matcher(line);
+ if (msgidMatcher.matches()) {
+ msgid = msgidMatcher.group("value");
+ state = State.MSGID_PLURAL; // found, next time look for plurals
} else {
- state = State.MSGSTR;
+ return new Block();
}
+ return this;
}
+
+ if (state == State.MSGID_PLURAL) {
+ final Matcher msgIdPluralMatcher = state.pattern.matcher(line);
+ if (msgIdPluralMatcher.matches()) {
+ msgid_plural = msgIdPluralMatcher.group("value");
+ state = State.MSGSTR0; // next time look for msgstr[0]
+ return this;
+ } else {
+ state = State.MSGSTR; // fall through (there may not have been any plural form)
+ }
+ }
+
if (state == State.MSGSTR) {
- final Matcher matcher = msgstrPattern.matcher(line);
- if (matcher.matches()) {
- msgstr = matcher.group("msgstr");
+ final Matcher msgStrMatcher = state.pattern.matcher(line);
+ if (msgStrMatcher.matches()) {
+ msgstr = msgStrMatcher.group("value");
+ }
+ append(translationsByKey);
+ return new Block();
+ }
+
+ if (state == State.MSGSTR0) {
+ final Matcher msgStr0Matcher = state.pattern.matcher(line);
+ if (msgStr0Matcher.matches()) {
+ msgstr = msgStr0Matcher.group("value");
+ state = State.MSGSTR1; // next time, look for plural
+ } else {
+ append(translationsByKey);
+ return new Block();
+ }
+ return this;
+ }
+
+ if (state == State.MSGSTR1) {
+ final Matcher msgStr1Matcher = state.pattern.matcher(line);
+ if (msgStr1Matcher.matches()) {
+ msgstr_plural = msgStr1Matcher.group("value");
}
append(translationsByKey);
return new Block();
@@ -53,7 +98,7 @@ class Block {
return this;
}
- private void append(final Map<MsgIdAndContext, String> translationsByKey) {
+ void append(final Map<MsgIdAndContext, String> translationsByKey) {
if(msgid != null && msgstr != null) {
for (String context : contextList) {
final MsgIdAndContext mc = new MsgIdAndContext(msgid, context);
http://git-wip-us.apache.org/repos/asf/isis/blob/e315c504/core/metamodel/src/test/java/org/apache/isis/core/metamodel/services/i18n/po/PoReaderTest.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/test/java/org/apache/isis/core/metamodel/services/i18n/po/PoReaderTest.java b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/services/i18n/po/PoReaderTest.java
index 6b516f2..a0a00c5 100644
--- a/core/metamodel/src/test/java/org/apache/isis/core/metamodel/services/i18n/po/PoReaderTest.java
+++ b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/services/i18n/po/PoReaderTest.java
@@ -125,6 +125,45 @@ public class PoReaderTest {
}
@Test
+ public void withPlurals() throws Exception {
+
+ // given
+ final String context =
+ "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#object()";
+ final String msgid = "Work of art";
+ final String msgid_plural = "Works of art";
+ final String msgstr$0 = "Œuvre d'art";
+ final String msgstr$1 = "Les œuvres d'art";
+
+ poReader = new PoReader(null) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ final List<String> lines = Lists.newArrayList();
+ lines.add(String.format("#: %s", context));
+ lines.add(String.format("msgid \"%s\"", msgid));
+ lines.add(String.format("msgid_plural \"%s\"", msgid_plural));
+ lines.add(String.format("msgstr[0] \"%s\"", msgstr$0));
+ lines.add(String.format("msgstr[1] \"%s\"", msgstr$1));
+ return lines;
+ }
+ };
+
+ // when
+ final String translated1 = poReader.translate(context, msgid, Locale.FRENCH);
+
+ // then
+ assertThat(translated1, is(equalTo(msgstr$0)));
+
+ // when
+ final String translated2 = poReader.translate(context, msgid_plural, Locale.FRENCH);
+
+ // then
+ assertThat(translated2, is(equalTo(msgstr$1)));
+ }
+
+
+
+ @Test
public void noTranslation() throws Exception {
// given