Skip to content

Commit cc986a8

Browse files
duonglaiquangrbri
authored andcommitted
Intl: add supportedLocalesOf()
1 parent 7e74f43 commit cc986a8

5 files changed

Lines changed: 106 additions & 1 deletion

File tree

src/main/java/org/htmlunit/javascript/host/intl/DateTimeFormat.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.htmlunit.javascript.configuration.JsxClass;
3838
import org.htmlunit.javascript.configuration.JsxConstructor;
3939
import org.htmlunit.javascript.configuration.JsxFunction;
40+
import org.htmlunit.javascript.configuration.JsxStaticFunction;
4041
import org.htmlunit.javascript.host.Window;
4142
import org.htmlunit.util.StringUtils;
4243

@@ -305,6 +306,18 @@ public Scriptable resolvedOptions() {
305306
return options;
306307
}
307308

309+
/**
310+
* Returns an array containing those of the provided locales that are supported
311+
* without having to fall back to the default locale.
312+
* @param localesArgument A string with a BCP 47 language tag, or an array of such strings
313+
* @param options unused
314+
* @return an array containing supported locales
315+
*/
316+
@JsxStaticFunction
317+
public static Scriptable supportedLocalesOf(final Scriptable localesArgument, final Scriptable options) {
318+
return Intl.supportedLocalesOf(localesArgument);
319+
}
320+
308321
/**
309322
* Helper.
310323
*/

src/main/java/org/htmlunit/javascript/host/intl/Intl.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626

27+
import org.apache.commons.lang3.LocaleUtils;
2728
import org.htmlunit.BrowserVersion;
2829
import org.htmlunit.corejs.javascript.Context;
2930
import org.htmlunit.corejs.javascript.Function;
@@ -60,7 +61,7 @@ public static void init(final Scriptable scope, final ScriptableObject globalThi
6061
intl.setParentScope(scope);
6162
intl.defineProperties(scope, browserVersion);
6263

63-
// Configure static functions
64+
// Configure static functions (getCanonicalLocales)
6465
final ClassConfiguration intlConfig = AbstractJavaScriptConfiguration.getClassConfiguration(Intl.class, browserVersion);
6566
if (intlConfig != null) {
6667
defineStaticFunctions(intlConfig, intl, intl);
@@ -173,4 +174,36 @@ else if (elem instanceof ScriptableObject) {
173174

174175
return cx.newArray(TopLevel.getTopLevelScope(thisObj), canonicalLocales.toArray());
175176
}
177+
178+
/**
179+
* Shared utility for {@code supportedLocalesOf} implementations.
180+
* @param localesArgument the locales argument
181+
* @return a Scriptable array of supported locale strings
182+
*/
183+
static Scriptable supportedLocalesOf(final Scriptable localesArgument) {
184+
final String[] locales;
185+
if (localesArgument instanceof NativeArray array) {
186+
locales = new String[(int) array.getLength()];
187+
for (int i = 0; i < locales.length; i++) {
188+
locales[i] = JavaScriptEngine.toString(array.get(i));
189+
}
190+
}
191+
else {
192+
locales = new String[] {JavaScriptEngine.toString(localesArgument)};
193+
}
194+
195+
final List<String> supportedLocales = new ArrayList<>();
196+
for (final String locale : locales) {
197+
if (locale.isEmpty()) {
198+
throw JavaScriptEngine.rangeError("Invalid language tag: " + locale);
199+
}
200+
final java.util.Locale l = java.util.Locale.forLanguageTag(locale);
201+
if (LocaleUtils.isAvailableLocale(l)) {
202+
supportedLocales.add(locale);
203+
}
204+
}
205+
206+
return Context.getCurrentContext().newArray(
207+
TopLevel.getTopLevelScope(localesArgument), supportedLocales.toArray());
208+
}
176209
}

src/main/java/org/htmlunit/javascript/host/intl/NumberFormat.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.htmlunit.javascript.configuration.JsxClass;
3333
import org.htmlunit.javascript.configuration.JsxConstructor;
3434
import org.htmlunit.javascript.configuration.JsxFunction;
35+
import org.htmlunit.javascript.configuration.JsxStaticFunction;
3536
import org.htmlunit.javascript.host.Window;
3637
import org.htmlunit.util.StringUtils;
3738

@@ -209,6 +210,18 @@ public Scriptable resolvedOptions() {
209210
return JavaScriptEngine.newObject(getParentScope());
210211
}
211212

213+
/**
214+
* Returns an array containing those of the provided locales that are supported
215+
* without having to fall back to the default locale.
216+
* @param localesArgument A string with a BCP 47 language tag, or an array of such strings
217+
* @param options unused
218+
* @return an array containing supported locales
219+
*/
220+
@JsxStaticFunction
221+
public static Scriptable supportedLocalesOf(final Scriptable localesArgument, final Scriptable options) {
222+
return Intl.supportedLocalesOf(localesArgument);
223+
}
224+
212225
/**
213226
* Helper.
214227
*/

src/test/java/org/htmlunit/javascript/host/intl/DateTimeFormatTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,27 @@ private void locale(final String language) throws Exception {
434434
shutDownAll();
435435
}
436436
}
437+
438+
/**
439+
* @throws Exception if the test fails
440+
*/
441+
@Test
442+
@Alerts({"en", "en,ja", ""})
443+
public void supportedLocalesOf() throws Exception {
444+
final String html = DOCTYPE_HTML
445+
+ "<html><head>\n"
446+
+ "<script>\n"
447+
+ LOG_TITLE_FUNCTION
448+
+ " function test() {\n"
449+
+ " log(Intl.DateTimeFormat.supportedLocalesOf('en'));\n"
450+
+ " log(Intl.DateTimeFormat.supportedLocalesOf(['en', 'xx-YY', 'ja']));\n"
451+
+ " log(Intl.DateTimeFormat.supportedLocalesOf([]));\n"
452+
+ " }\n"
453+
+ "</script>\n"
454+
+ "</head>\n"
455+
+ "<body onload='test()'>\n"
456+
+ "</body></html>";
457+
458+
loadPageVerifyTitle2(html);
459+
}
437460
}

src/test/java/org/htmlunit/javascript/host/intl/NumberFormatTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,27 @@ public void numberFormat() throws Exception {
111111

112112
loadPageVerifyTitle2(html);
113113
}
114+
115+
/**
116+
* @throws Exception if the test fails
117+
*/
118+
@Test
119+
@Alerts({"en", "en,ja", ""})
120+
public void supportedLocalesOf() throws Exception {
121+
final String html = DOCTYPE_HTML
122+
+ "<html><head>\n"
123+
+ "<script>\n"
124+
+ LOG_TITLE_FUNCTION
125+
+ " function test() {\n"
126+
+ " log(Intl.NumberFormat.supportedLocalesOf('en'));\n"
127+
+ " log(Intl.NumberFormat.supportedLocalesOf(['en', 'xx-YY', 'ja']));\n"
128+
+ " log(Intl.NumberFormat.supportedLocalesOf([]));\n"
129+
+ " }\n"
130+
+ "</script>\n"
131+
+ "</head>\n"
132+
+ "<body onload='test()'>\n"
133+
+ "</body></html>";
134+
135+
loadPageVerifyTitle2(html);
136+
}
114137
}

0 commit comments

Comments
 (0)