@@ -130,7 +130,7 @@ public static String getSampleGrammarFile(@Nullable String grammar) {
130130 } else {
131131 // If they pasted in a whole file, it's fine.
132132 if (!grammar .contains ("<sfdcnames>" ) && !grammar .contains ("<sfdcadjectives>" )
133- && !grammar .contains ("<names>" ) && !grammar .contains ("<adjectives>" )) {
133+ && !grammar .contains ("<names>" ) && !grammar .contains ("<adjectives>" )) {
134134 StringBuilder sb = new StringBuilder (grammar .length () + 100 );
135135 sb .append ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" );
136136 sb .append ("<names>" );
@@ -139,7 +139,7 @@ public static String getSampleGrammarFile(@Nullable String grammar) {
139139 return sb .toString ();
140140 } else {
141141 // Remove the dtd which confuses the parser
142- String fixedGrammar = grammar .replace ("<!DOCTYPE sfdcnames SYSTEM \" sfdcnames.dtd\" >" , "" );
142+ String fixedGrammar = grammar .replace ("<!DOCTYPE sfdcnames SYSTEM \" sfdcnames.dtd\" >" , "" );
143143 fixedGrammar = fixedGrammar .replace ("<!DOCTYPE sfdcadjectives SYSTEM \" sfdcadjectives.dtd\" >" , "" );
144144 fixedGrammar = fixedGrammar .replace ("<!DOCTYPE names SYSTEM \" names.dtd\" >" , "" );
145145 fixedGrammar = fixedGrammar .replace ("<!DOCTYPE adjectives SYSTEM \" adjectives.dtd\" >" , "" );
@@ -180,46 +180,63 @@ public String nounify(String input, LanguageDictionary dictionary) {
180180 return new Nounifier (dictionary ).nounifyString (input );
181181 }
182182
183- /**
184- * Set of locale langauges strings where the results are JDK dependent
183+ /*
184+ * Map of locale langauges strings where the results are JDK dependent.
185+ * Java 17 fixed legacy language code and autmatically replace it in {@code java.util.Locale} (JDK-8267069). This is
186+ * for backward compatibility.
185187 */
186- static final Set <String > JDK_DEPENDENT_LANGUAGE = ImmutableSet .of (LanguageConstants .YIDDISH_ISO ,
187- LanguageConstants .HEBREW_ISO , LanguageConstants .INDONESIAN_ISO );
188-
188+ static final Map <String , String > JDK_DEPENDENT_LANGUAGE = Map .of ( //
189+ LanguageConstants .YIDDISH_ISO , LanguageConstants .YIDDISH , //
190+ LanguageConstants .HEBREW_ISO , LanguageConstants .HEBREW , //
191+ LanguageConstants .INDONESIAN_ISO , LanguageConstants .INDONESIAN );
189192
190- public static List <URL > getFileNames (HumanLanguage language ,URL rootDirectory , String basename ) {
193+ public static List <URL > getFileNames (HumanLanguage language , URL rootDirectory , String basename ) {
191194 List <URL > list = new ArrayList <>();
192195 try {
193- Locale locale = language .getLocale ();
194- list .add (new URL (rootDirectory , locale .getLanguage () + '/' + basename ));
196+ final Locale locale = language .getLocale ();
197+ final String override = language .getOverrideLanguage ();
198+ final boolean hasOverride = override != null && !locale .getLanguage ().equals (override );
199+
200+ list .add (getUrl (rootDirectory , locale .getLanguage (), basename ));
201+ // If the override language is set, add it
202+ if (hasOverride ) list .add (getUrl (rootDirectory , override , basename ));
195203 // If the iso code for the language changed between JDK versions, use the old isocode for the directory
196- if (JDK_DEPENDENT_LANGUAGE .contains (locale .getLanguage ())) {
197- list .add (new URL (rootDirectory , language .getOverrideLanguage () + '/' + basename ));
204+ if (JDK_DEPENDENT_LANGUAGE .containsKey (locale .getLanguage ())) {
205+ list .add (getUrl (rootDirectory , JDK_DEPENDENT_LANGUAGE .get (locale .getLanguage ()), basename ));
206+ }
207+
208+ if (!locale .getCountry ().isEmpty ()) {
209+ /*
210+ * Code required because there is no Chinese traditional locale in jdk Locale.java and taiwan and Hong
211+ * kong are 2 different countries
212+ * We will assume that Hong Kong (HK country code) derive the Traditional Chinese from Taiwan (TW)
213+ * In the future, to define Hong Kong label, a new directory will need to be created zh/HK.
214+ */
215+ if (language .getLocaleString ().equals (LanguageConstants .CHINESE_HK )) {
216+ // adding Taiwan as fallback. assuming no override, or variant are set for this
217+ list .add (getUrl (rootDirectory , locale .getLanguage (), Locale .TRADITIONAL_CHINESE .getCountry (), basename ));
218+ list .add (getUrl (rootDirectory , locale .getLanguage (), locale .getCountry (), basename ));
219+ } else {
220+ list .add (getUrl (rootDirectory , locale .getLanguage (), locale .getCountry (), basename ));
221+ if (hasOverride ) list .add (getUrl (rootDirectory , override , locale .getCountry (), basename ));
222+
223+ if (!locale .getVariant ().isEmpty ()) {
224+ list .add (getUrl (rootDirectory , locale .getLanguage (), locale .getCountry (), locale .getVariant (), basename ));
225+ if (hasOverride ) list .add (getUrl (rootDirectory , override , locale .getCountry (), locale .getVariant (), basename ));
226+ }
227+ }
198228 }
199- if (locale .getCountry ().length () > 0 ) {
200- /*
201- * Code required because there is no Chinese traditional locale in jdk Locale.java and taiwan and Hong kong are 2 different countries
202- * We will assume that Hong Kong (HK country code) derive the Traditional Chinese from Taiwan (TW)
203- * In the future, to define Hong Kong label, a new directory will need to be created zh/HK.
204- */
205- if (language .getLocaleString ().equals (LanguageConstants .CHINESE_HK )){
206- //adding Taiwan as fallback
207- list .add (new URL (rootDirectory , locale .getLanguage () + '/' + Locale .TRADITIONAL_CHINESE .getCountry () +'/' + basename ));
208- list .add (new URL (rootDirectory , locale .getLanguage () + '/' + locale .getCountry () + '/' + basename ));
209- }else {
210- list .add (new URL (rootDirectory , locale .getLanguage () + '/' + locale .getCountry () + '/' + basename ));
211- if (locale .getVariant ().length () > 0 ) {
212- list .add (new URL (rootDirectory , locale .getLanguage () + '/' + locale .getCountry () + '/' + locale .getVariant () + '/' + basename ));
213- }
214- }
215- }
216229 } catch (MalformedURLException e ) {
217230 throw new IllegalArgumentException (e );
218231 }
219- return list ;
220- }
232+ return list ;
233+ }
234+
235+ private static URL getUrl (URL root , String ... paths ) throws MalformedURLException {
236+ return new URL (root , String .join ("/" , paths ));
237+ }
221238
222- private static final Pattern nonalphaPattern = Pattern .compile ("\\ W" );
239+ private static final Pattern nonalphaPattern = Pattern .compile ("\\ W" );
223240
224241 // TODO: Switch this to splitter.
225242 static List <String > tokenize (String input ) {
@@ -244,7 +261,7 @@ static List<String> tokenize(String input) {
244261 * to determine if there should be some "tokenization" of the string.
245262 */
246263 public static class Nounifier {
247- private final static Set <String > EXCLUDED_NOUNS = ImmutableSet .of ("Role" , "Email" , "Address" , "{0}" );
264+ private static final Set <String > EXCLUDED_NOUNS = ImmutableSet .of ("Role" , "Email" , "Address" , "{0}" );
248265
249266 private final GenericTrieMatcher <String > nounMatcher ;
250267 private final GenericTrieMatcher <String > adjMatcher ;
0 commit comments