Skip to content

Commit

Permalink
Switch ordering of translation providers (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Dec 1, 2022
1 parent 2dc4bc6 commit 926bc92
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public void run() throws Exception {
Wikidata.fetch(osmInputFile(), wikidataNamesFile, config(), profile(), stats());
}
if (useWikidata) {
translations().addTranslationProvider(Wikidata.load(wikidataNamesFile));
translations().addFallbackTranslationProvider(Wikidata.load(wikidataNamesFile));
}
if (onlyDownloadSources || onlyFetchWikidata) {
return; // exit only if just fetching wikidata or downloading sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ public static Translations nullProvider(List<String> languages) {
* @param languages the set of 2-letter language codes to limit output translations to
*/
public static Translations defaultProvider(List<String> languages) {
return nullProvider(languages).addTranslationProvider(new OsmTranslationProvider());
return nullProvider(languages).addFallbackTranslationProvider(new OsmTranslationProvider());
}

/**
* Mutates this translation instance to add {@code provider} which will be used before all other providers.
*
* @deprecated
*/
@Deprecated(forRemoval = true)
public Translations addTranslationProvider(TranslationProvider provider) {
providers.add(0, provider);
return this;
}

/**
* Mutates this translation instance to add {@code provider} which will be used only if all existing providers fail to
* produce a translation for a given language.
*/
public Translations addTranslationProvider(TranslationProvider provider) {
public Translations addFallbackTranslationProvider(TranslationProvider provider) {
providers.add(provider);
return this;
}
Expand All @@ -75,7 +86,7 @@ public void addTranslations(Map<String, Object> output, Map<String, Object> inpu
for (var entry : translations.entrySet()) {
String key = entry.getKey();
if (languageSet.contains(key)) {
output.put(key.startsWith("name:") ? key : "name:" + key, entry.getValue());
output.putIfAbsent(key.startsWith("name:") ? key : "name:" + key, entry.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ void testDefaultProvider() {
}

@Test
void testTwoProviders() {
void testTwoProvidersPrefersFirst() {
var translations = Translations.defaultProvider(List.of("en", "es", "de"))
.addTranslationProvider(elem -> Map.of("name:de", "de2", "name:en", "en2"));
assertEquals(Map.of("name:en", "en2", "name:es", "es1", "name:de", "de2"),
.addFallbackTranslationProvider(elem -> Map.of("name:de", "de2", "name:en", "en2"));
assertEquals(Map.of("name:en", "en1", "name:es", "es1", "name:de", "de2"),
translations.getTranslations(Map.of("name:en", "en1", "name:es", "es1")));
}


@Test
void testTransliterate() {
assertEquals("rì běn", Translations.transliterate("日本"));
Expand Down

0 comments on commit 926bc92

Please sign in to comment.