From 494b08960c342aa24174e426330d19cb420c3480 Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Sun, 6 Oct 2024 23:34:56 +0200 Subject: [PATCH] Support for display names. --- .../meeuw/i18n/languages/DisplayNames_en.java | 15 +++++++++++++++ .../org/meeuw/i18n/languages/ISO_639_Code.java | 7 +++++++ .../org/meeuw/i18n/languages/LanguageCode.java | 2 ++ .../i18n/languages/DisplayNames_nl.properties | 7 +++++++ .../i18n/languages/test/LanguageCodeTest.java | 16 ++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 src/main/java/org/meeuw/i18n/languages/DisplayNames_en.java create mode 100644 src/main/resources/org/meeuw/i18n/languages/DisplayNames_nl.properties diff --git a/src/main/java/org/meeuw/i18n/languages/DisplayNames_en.java b/src/main/java/org/meeuw/i18n/languages/DisplayNames_en.java new file mode 100644 index 0000000..797628a --- /dev/null +++ b/src/main/java/org/meeuw/i18n/languages/DisplayNames_en.java @@ -0,0 +1,15 @@ +package org.meeuw.i18n.languages; + +import java.util.stream.Stream; + +public class DisplayNames_en extends java.util.ListResourceBundle { + @Override + protected Object[][] getContents() { + return Stream.concat( + Stream.of(ISO_639_1_Code.values()), + ISO_639_3_Code.stream()) + .map(i -> new Object[] {i.code(), i.refName()}) + .toArray(i -> new Object[i][2]); + + } +} diff --git a/src/main/java/org/meeuw/i18n/languages/ISO_639_Code.java b/src/main/java/org/meeuw/i18n/languages/ISO_639_Code.java index a7a0f88..bf93f4c 100644 --- a/src/main/java/org/meeuw/i18n/languages/ISO_639_Code.java +++ b/src/main/java/org/meeuw/i18n/languages/ISO_639_Code.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.Locale; +import java.util.ResourceBundle; import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @@ -56,4 +57,10 @@ static ISO_639_Code fromCode(String code) { } + default String getDisplayName(Locale locale) { + ResourceBundle bundle = ResourceBundle.getBundle("org.meeuw.i18n.languages.DisplayNames", locale); + return bundle.getString(code()); + + } + } diff --git a/src/main/java/org/meeuw/i18n/languages/LanguageCode.java b/src/main/java/org/meeuw/i18n/languages/LanguageCode.java index 06b6aef..4d9376f 100644 --- a/src/main/java/org/meeuw/i18n/languages/LanguageCode.java +++ b/src/main/java/org/meeuw/i18n/languages/LanguageCode.java @@ -378,6 +378,8 @@ default NameRecord nameRecord(Locale locale) { } } + + /** * The macro language(s) of which this language is a part. * @return a list of macro languages, or an empty list if this language is not known to be a part of a macro language. diff --git a/src/main/resources/org/meeuw/i18n/languages/DisplayNames_nl.properties b/src/main/resources/org/meeuw/i18n/languages/DisplayNames_nl.properties new file mode 100644 index 0000000..e1ab8bd --- /dev/null +++ b/src/main/resources/org/meeuw/i18n/languages/DisplayNames_nl.properties @@ -0,0 +1,7 @@ +dse=Nederlandse Gebarentaal +vgt=Vlaamse Gebarentaal +ase=Amerikaanse Gebarentaal +bfi=Britse Gebarentaal +csl=Chinese Gebarentaal +gsg=Duitse Gebarentaal +tsm=Turkse Gebarentaal diff --git a/src/test/java/org/meeuw/i18n/languages/test/LanguageCodeTest.java b/src/test/java/org/meeuw/i18n/languages/test/LanguageCodeTest.java index a13c702..c977c4f 100644 --- a/src/test/java/org/meeuw/i18n/languages/test/LanguageCodeTest.java +++ b/src/test/java/org/meeuw/i18n/languages/test/LanguageCodeTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.meeuw.i18n.languages.*; import static org.assertj.core.api.Assertions.assertThat; @@ -210,6 +212,20 @@ public void deprecated() { assertThat(LanguageCode.getByPart3("nld")).contains((LanguageCode) ISO_639.get("nl").get()); } + @ParameterizedTest + @ValueSource(strings = {"dse"}) + public void signLanguage(String code) { + LanguageCode l = LanguageCode.get(code).orElseThrow(); + + assertThat(l.refName()).isEqualTo("Dutch Sign Language"); + assertThat(l.nameRecord().print()).isEqualTo("dse (Dutch Sign Language)"); + assertThat(new Locale(l.code()).getDisplayName(Locale.US)).isEqualTo("dse"); + + assertThat(l.getDisplayName(Locale.US)).isEqualTo("Dutch Sign Language"); + + assertThat(l.getDisplayName(new Locale("nl"))).isEqualTo("Nederlandse Gebarentaal"); + } + // TODO @Disabled