Skip to content

Commit 0d118dc

Browse files
committed
Throw IllegalArgumentException when unknown language tag is used in set_language
1 parent 5f94853 commit 0d118dc

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

linguistics/src/main/java/com/yahoo/language/LocaleFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
22
package com.yahoo.language;
33

4+
import java.util.IllformedLocaleException;
45
import java.util.Locale;
56
import java.util.Objects;
67

@@ -45,7 +46,11 @@ public static Locale fromLanguageTag(String tag) {
4546
}
4647
}
4748
if (language.isEmpty()) return UNKNOWN;
48-
return new Locale.Builder().setLanguage(language).setScript(script).setRegion(region).build();
49+
try {
50+
return new Locale.Builder().setLanguage(language).setScript(script).setRegion(region).build();
51+
} catch (IllformedLocaleException e) {
52+
throw new IllegalArgumentException("Unknown language tag '" + tag + "', it must be a language tag corresponding to RFC5646");
53+
}
4954
}
5055

5156
}

linguistics/src/test/java/com/yahoo/language/LocaleFactoryTestCase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Locale;
77

88
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertThrows;
910
import static org.junit.Assert.assertTrue;
1011
import static org.junit.Assert.fail;
1112

@@ -41,6 +42,11 @@ public void requireThatLocaleCanBeCreatedFromLanguageTag() {
4142
assertLocale("", "", "", "");
4243
assertLocale("z-foo", "", "", "");
4344
assertLocale("ojeroierhoiherohjdadsfodsfoifiopeoipefwoipfwe", "", "", "");
45+
46+
var exception = assertThrows(IllegalArgumentException.class,
47+
() -> LocaleFactory.fromLanguageTag("foo-13"));
48+
assertEquals("Unknown language tag 'foo-13', it must be a language tag corresponding to RFC5646",
49+
exception.getMessage());
4450
}
4551

4652
private static void assertLocale(String tag, String language, String variant, String country) {

0 commit comments

Comments
 (0)