You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2022/05/03 09:46:29 UTC
[isis] branch master updated: ISIS-2987: Block (po reader): support for root context
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 5a2985e0f7 ISIS-2987: Block (po reader): support for root context
5a2985e0f7 is described below
commit 5a2985e0f7d951c2c6bea3eb756a70a74a78d0de
Author: andi-huber <ah...@apache.org>
AuthorDate: Tue May 3 11:46:23 2022 +0200
ISIS-2987: Block (po reader): support for root context
- that is, allow for translation of words that have no context
---
.../isis/core/runtimeservices/i18n/po/Block.java | 26 +-
.../core/runtimeservices/i18n/po/PoReaderTest.java | 333 ++++++++++-----------
2 files changed, 188 insertions(+), 171 deletions(-)
diff --git a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/i18n/po/Block.java b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/i18n/po/Block.java
index 2ca120ec8d..dcc8b4923b 100644
--- a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/i18n/po/Block.java
+++ b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/i18n/po/Block.java
@@ -28,7 +28,16 @@ import org.apache.isis.commons.internal.collections._Lists;
class Block {
private enum State {
- CONTEXT("^#: (?<value>.+)$"),
+ /**
+ * XXX[ISIS-2987] in support of (not providing any context) ...
+ * <pre>
+ * #:
+ * msgid "Anonymous"
+ * msgstr "Anonim felhasználó"
+ * </pre>
+ */
+ ROOT_CONTEXT("^#:\\s*$"),
+ SPECIFIC_CONTEXT("^#: (?<value>.+)$"),
MSGID("^msgid \"(?<value>.+)\"$"),
MSGID_PLURAL("^msgid_plural \"(?<value>.+)\"$"),
MSGSTR("^msgstr \"(?<value>.+)\"$"),
@@ -42,7 +51,7 @@ class Block {
}
}
- State state = State.CONTEXT;
+ State state = State.SPECIFIC_CONTEXT;
List<String> contextList = _Lists.newArrayList();
String msgid = null;
@@ -51,12 +60,23 @@ class Block {
String msgstr_plural = null; // from msgstr[1]
Block parseLine(final String line, final Map<ContextAndMsgId, String> translationsByKey) {
- if (state == State.CONTEXT) {
+
+ if (state == State.SPECIFIC_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.ROOT_CONTEXT;
+ }
+ }
+
+ if (state == State.ROOT_CONTEXT) {
+ final Matcher contextMatcher = state.pattern.matcher(line);
+ if (contextMatcher.matches()) {
+ contextList.add("");
+ return this;
} else {
state = State.MSGID;
// fallthrough (there may not have been any more context)
diff --git a/core/runtimeservices/src/test/java/org/apache/isis/core/runtimeservices/i18n/po/PoReaderTest.java b/core/runtimeservices/src/test/java/org/apache/isis/core/runtimeservices/i18n/po/PoReaderTest.java
index 22834589d2..421f47953f 100644
--- a/core/runtimeservices/src/test/java/org/apache/isis/core/runtimeservices/i18n/po/PoReaderTest.java
+++ b/core/runtimeservices/src/test/java/org/apache/isis/core/runtimeservices/i18n/po/PoReaderTest.java
@@ -76,176 +76,173 @@ public class PoReaderTest {
Assert.assertNotNull(mockLanguageProvider.getPreferredLanguage());
}
- public static class Translate extends PoReaderTest {
-
- @Test
- public void singleContext() throws Exception {
-
- // given
- final TranslationContext context = TranslationContext.ofName(
- "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#object()");
- final String msgId = "Work of art";
- final String msgStr = "Objet d'art";
-
- poReader = new PoReader(mockTranslationServicePo) {
- @Override
- protected List<String> readPo(final Locale locale) {
- final List<String> lines = _Lists.newArrayList();
- lines.add(String.format("#: %s", context.getName()));
- lines.add(String.format("msgid \"%s\"", msgId));
- lines.add(String.format("msgstr \"%s\"", msgStr));
- return lines;
- }
- };
-
- // when
- final String translated = poReader.translate(context, msgId);
-
- // then
- assertThat(translated, is(equalTo(msgStr)));
- }
-
- @Test
- public void multipleContext() throws Exception {
-
- // given
- final TranslationContext context1 = TranslationContext.ofName(
- "fixture.simple.SimpleObjectsFixturesService#runFixtureScript(org.apache.isis.applib.fixturescripts.FixtureScript,java.lang.String)");
- final TranslationContext context2 = TranslationContext.ofName(
- "org.apache.isis.applib.fixturescripts.FixtureScripts#runFixtureScript(org.apache.isis.applib.fixturescripts.FixtureScript,java.lang.String)");
- final String msgId = "Parameters";
- final String msgStr = "Paramètres";
-
- poReader = new PoReader(mockTranslationServicePo) {
- @Override
- protected List<String> readPo(final Locale locale) {
- final List<String> lines = _Lists.newArrayList();
- lines.add(String.format("#: %s", context1.getName()));
- lines.add(String.format("#: %s", context2.getName()));
- lines.add(String.format("msgid \"%s\"", msgId));
- lines.add(String.format("msgstr \"%s\"", msgStr));
- return lines;
- }
- };
- // when
- final String translated = poReader.translate(context1, msgId);
-
- // then
- assertThat(translated, is(equalTo(msgStr)));
-
- // when
- final String translated2 = poReader.translate(context2, msgId);
-
- // then
- assertThat(translated2, is(equalTo(msgStr)));
- }
-
- @Test
- public void multipleBlocks() throws Exception {
-
- // given
- final TranslationContext context1 = TranslationContext.ofName(
- "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#object()");
- final String msgid1 = "Work of art";
- final String msgstr1 = "Objet d'art";
-
- final TranslationContext context2 = TranslationContext.ofName(
- "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#lookup()");
- final String msgid2 = "Lookup";
- final String msgstr2 = "Look up";
-
- poReader = new PoReader(mockTranslationServicePo) {
- @Override
- protected List<String> readPo(final Locale locale) {
- final List<String> lines = _Lists.newArrayList();
- lines.add(String.format("#: %s", context1.getName()));
- lines.add(String.format("msgid \"%s\"", msgid1));
- lines.add(String.format("msgstr \"%s\"", msgstr1));
-
- lines.add(String.format(""));
- lines.add(String.format("# "));
-
- lines.add(String.format("#: %s", context2.getName()));
- lines.add(String.format("msgid \"%s\"", msgid2));
- lines.add(String.format("msgstr \"%s\"", msgstr2));
-
- lines.add(String.format(""));
- return lines;
- }
- };
-
- // when
- final String translated1 = poReader.translate(context1, msgid1);
-
- // then
- assertThat(translated1, is(equalTo(msgstr1)));
-
- // when
- final String translated2 = poReader.translate(context2, msgid2);
-
- // then
- assertThat(translated2, is(equalTo(msgstr2)));
- }
-
- @Test
- public void withPlurals() throws Exception {
-
- // given
- final TranslationContext context = TranslationContext.ofName(
- "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(mockTranslationServicePo) {
- @Override
- protected List<String> readPo(final Locale locale) {
- final List<String> lines = _Lists.newArrayList();
- lines.add(String.format("#: %s", context.getName()));
- 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);
-
- // then
- assertThat(translated1, is(equalTo(msgstr$0)));
-
- // when
- final String translated2 = poReader.translate(context, msgid_plural);
-
- // then
- assertThat(translated2, is(equalTo(msgstr$1)));
- }
-
-
-
- @Test
- public void noTranslation() throws Exception {
+ @Test
+ public void singleContext() throws Exception {
+
+ // given
+ final TranslationContext context = TranslationContext.ofName(
+ "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#object()");
+ final String msgId = "Work of art";
+ final String msgStr = "Objet d'art";
+
+ poReader = new PoReader(mockTranslationServicePo) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ final List<String> lines = _Lists.newArrayList();
+ lines.add(String.format("#: %s", context.getName()));
+ lines.add(String.format("msgid \"%s\"", msgId));
+ lines.add(String.format("msgstr \"%s\"", msgStr));
+ return lines;
+ }
+ };
+
+ // when
+ final String translated = poReader.translate(context, msgId);
+
+ // then
+ assertThat(translated, is(equalTo(msgStr)));
+ }
+
+ @Test
+ public void multipleContext() throws Exception {
+
+ // given
+ final TranslationContext context1 = TranslationContext.ofName(
+ "fixture.simple.SimpleObjectsFixturesService#runFixtureScript(org.apache.isis.applib.fixturescripts.FixtureScript,java.lang.String)");
+ final TranslationContext context2 = TranslationContext.ofName(
+ "org.apache.isis.applib.fixturescripts.FixtureScripts#runFixtureScript(org.apache.isis.applib.fixturescripts.FixtureScript,java.lang.String)");
+ final String msgId = "Parameters";
+ final String msgStr = "Paramètres";
+
+ poReader = new PoReader(mockTranslationServicePo) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ final List<String> lines = _Lists.newArrayList();
+ lines.add(String.format("#: %s", context1.getName()));
+ lines.add(String.format("#: %s", context2.getName()));
+ lines.add(String.format("msgid \"%s\"", msgId));
+ lines.add(String.format("msgstr \"%s\"", msgStr));
+ return lines;
+ }
+ };
+ // when
+ final String translated = poReader.translate(context1, msgId);
+
+ // then
+ assertThat(translated, is(equalTo(msgStr)));
+
+ // when
+ final String translated2 = poReader.translate(context2, msgId);
+
+ // then
+ assertThat(translated2, is(equalTo(msgStr)));
+ }
+
+ @Test
+ public void multipleBlocks() throws Exception {
+
+ // given
+ final TranslationContext context1 = TranslationContext.ofName(
+ "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#object()");
+ final String msgid1 = "Work of art";
+ final String msgstr1 = "Objet d'art";
+
+ final TranslationContext context2 = TranslationContext.ofName(
+ "org.apache.isis.applib.services.bookmark.BookmarkHolderAssociationContributions#lookup()");
+ final String msgid2 = "Lookup";
+ final String msgstr2 = "Look up";
+
+ poReader = new PoReader(mockTranslationServicePo) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ final List<String> lines = _Lists.newArrayList();
+ lines.add(String.format("#: %s", context1.getName()));
+ lines.add(String.format("msgid \"%s\"", msgid1));
+ lines.add(String.format("msgstr \"%s\"", msgstr1));
+
+ lines.add(String.format(""));
+ lines.add(String.format("# "));
+
+ lines.add(String.format("#: %s", context2.getName()));
+ lines.add(String.format("msgid \"%s\"", msgid2));
+ lines.add(String.format("msgstr \"%s\"", msgstr2));
+
+ lines.add(String.format(""));
+ return lines;
+ }
+ };
+
+ // when
+ final String translated1 = poReader.translate(context1, msgid1);
+
+ // then
+ assertThat(translated1, is(equalTo(msgstr1)));
+
+ // when
+ final String translated2 = poReader.translate(context2, msgid2);
+
+ // then
+ assertThat(translated2, is(equalTo(msgstr2)));
+ }
+
+ @Test
+ public void withPlurals() throws Exception {
+
+ // given
+ final TranslationContext context = TranslationContext.ofName(
+ "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(mockTranslationServicePo) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ final List<String> lines = _Lists.newArrayList();
+ lines.add(String.format("#: %s", context.getName()));
+ 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);
+
+ // then
+ assertThat(translated1, is(equalTo(msgstr$0)));
+
+ // when
+ final String translated2 = poReader.translate(context, msgid_plural);
+
+ // then
+ assertThat(translated2, is(equalTo(msgstr$1)));
+ }
+
+
+
+ @Test
+ public void noTranslation() throws Exception {
+
+ // given
+
+ poReader = new PoReader(mockTranslationServicePo) {
+ @Override
+ protected List<String> readPo(final Locale locale) {
+ return _Lists.newArrayList();
+ }
+ };
+
+ TranslationContext context = TranslationContext.ofName("someContext");
- // given
-
- poReader = new PoReader(mockTranslationServicePo) {
- @Override
- protected List<String> readPo(final Locale locale) {
- return _Lists.newArrayList();
- }
- };
-
- TranslationContext context = TranslationContext.ofName("someContext");
+ // when
+ final String translated = poReader.translate(context, "Something to translate");
- // when
- final String translated = poReader.translate(context, "Something to translate");
-
- // then
- assertThat(translated, is(equalTo("Something to translate")));
- }
+ // then
+ assertThat(translated, is(equalTo("Something to translate")));
}
}
\ No newline at end of file