diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java index e0c8c4dff4..5bed350a84 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java @@ -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 diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/Translations.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/Translations.java index 72afc2306f..c06a0fcec1 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/Translations.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/Translations.java @@ -45,14 +45,25 @@ public static Translations nullProvider(List languages) { * @param languages the set of 2-letter language codes to limit output translations to */ public static Translations defaultProvider(List 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; } @@ -75,7 +86,7 @@ public void addTranslations(Map output, Map 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()); } } } diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TranslationsTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TranslationsTest.java index 5f2156e0e8..14291c8d8c 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TranslationsTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TranslationsTest.java @@ -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("日本"));