|
20 | 20 | <%@ page import="org.wso2.carbon.identity.application.authentication.endpoint.util.EncodedControl" %> |
21 | 21 | <%@ page import="java.nio.charset.StandardCharsets" %> |
22 | 22 | <%@ page import="java.util.*" %> |
| 23 | +<%@ page import="java.io.FileReader" %> |
| 24 | +<%@ page import="java.io.BufferedReader" %> |
23 | 25 | <%@ page import="org.json.JSONObject" %> |
24 | 26 | <%@ page import="java.util.Calendar" %> |
25 | 27 | <%@ page import="org.apache.commons.lang.StringUtils" %> |
|
101 | 103 | Locale userLocale = browserLocale; |
102 | 104 | String localeFromCookie = null; |
103 | 105 | String BUNDLE = "org.wso2.carbon.identity.mgt.recovery.endpoint.i18n.Resources"; |
| 106 | + String SUPPORTED_LANGUAGES_ATTR = "supportedLanguages"; |
| 107 | + String LANGUAGE_SUPPORTED_COUNTRIES_ATTR = "languageSupportedCountries"; |
104 | 108 |
|
105 | 109 | // List of screen names for retrieving text branding customizations. |
106 | 110 | List<String> screenNames = new ArrayList<>(); |
|
109 | 113 | // Map to store default supported language codes. |
110 | 114 | // TODO: Use this map to generate the `language-switcher.jsp`. |
111 | 115 | Map<String, String> supportedLanguages = new HashMap<>(); |
112 | | - supportedLanguages.put("en", "US"); |
113 | | - supportedLanguages.put("fr", "FR"); |
114 | | - supportedLanguages.put("es", "ES"); |
115 | | - supportedLanguages.put("pt", "PT"); |
116 | | - supportedLanguages.put("de", "DE"); |
117 | | - supportedLanguages.put("zh", "CN"); |
118 | | - supportedLanguages.put("ja", "JP"); |
119 | | -
|
120 | 116 | List<String> languageSupportedCountries = new ArrayList<>(); |
121 | | - languageSupportedCountries.add("US"); |
122 | | - languageSupportedCountries.add("FR"); |
123 | | - languageSupportedCountries.add("ES"); |
124 | | - languageSupportedCountries.add("PT"); |
125 | | - languageSupportedCountries.add("DE"); |
126 | | - languageSupportedCountries.add("CN"); |
127 | | - languageSupportedCountries.add("JP"); |
128 | | - languageSupportedCountries.add("BR"); |
| 117 | +
|
| 118 | + // Dynamically load supported languages from languageOptions.properties |
| 119 | + // Use application scope to cache the parsed language options |
| 120 | + Map<String, String> cachedSupportedLanguages = (Map<String, String>) request.getAttribute(SUPPORTED_LANGUAGES_ATTR); |
| 121 | + List<String> cachedLanguageSupportedCountries = (List<String>) request.getAttribute(LANGUAGE_SUPPORTED_COUNTRIES_ATTR); |
| 122 | +
|
| 123 | + if (cachedSupportedLanguages != null && cachedLanguageSupportedCountries != null) { |
| 124 | + supportedLanguages = cachedSupportedLanguages; |
| 125 | + languageSupportedCountries = cachedLanguageSupportedCountries; |
| 126 | + } else { |
| 127 | + // Specify the file path |
| 128 | + String filePath = application.getRealPath("/") + "/WEB-INF/classes/LanguageOptions.properties"; |
| 129 | +
|
| 130 | + // Use a BufferedReader to read the file content |
| 131 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { |
| 132 | + String line; |
| 133 | + while ((line = bufferedReader.readLine()) != null) { |
| 134 | + // Ignore comments and empty lines |
| 135 | + if (!line.trim().startsWith("#") && !line.trim().isEmpty()) { |
| 136 | + // Split the line into key and value using '=' as the delimiter |
| 137 | + String[] keyValue = line.split("=", 2); |
| 138 | + if (keyValue.length < 2) { |
| 139 | + continue; // Skip malformed lines |
| 140 | + } |
| 141 | + // Split the key further using '.' as the delimiter |
| 142 | + String[] parts = keyValue[0].split("\\."); |
| 143 | + if (parts.length == 0) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + String languageCode = parts[parts.length - 1]; |
| 147 | + // Split the value further using ',' as the delimiter |
| 148 | + String[] values = keyValue[1].split(","); |
| 149 | + if (values.length < 2) { |
| 150 | + continue; // Skip lines without proper country,displayName format |
| 151 | + } |
| 152 | + String country = values[0]; |
| 153 | + // displayName is available in values[1] if needed in the future |
| 154 | + // Add the values to the list. |
| 155 | + supportedLanguages.put(languageCode, country); |
| 156 | + if (!languageSupportedCountries.contains(country)) { |
| 157 | + languageSupportedCountries.add(country); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +
|
| 162 | + request.setAttribute(SUPPORTED_LANGUAGES_ATTR, supportedLanguages); |
| 163 | + request.setAttribute(LANGUAGE_SUPPORTED_COUNTRIES_ATTR, languageSupportedCountries); |
| 164 | + } catch (Exception e) { |
| 165 | + throw e; |
| 166 | + } |
| 167 | + } |
129 | 168 |
|
130 | 169 | // Check cookie for the user selected language first |
131 | 170 | Cookie[] cookies = request.getCookies(); |
|
205 | 244 | userLocale = new Locale(browserLocale.getLanguage(), countryCode); |
206 | 245 | } else if (StringUtils.isNotBlank(fallbackCountryCode)){ |
207 | 246 | userLocale = new Locale(browserLocale.getLanguage(), fallbackCountryCode); |
| 247 | + } else if (StringUtils.isNotBlank(browserLocale.getLanguage())){ |
| 248 | + for (String key : supportedLanguages.keySet()) { |
| 249 | + if (key.startsWith(browserLocale.getLanguage() + "_")) { |
| 250 | + String[] parts = key.split("_"); |
| 251 | + userLocale = new Locale(parts[0], parts[1]); |
| 252 | + break; |
| 253 | + } |
| 254 | + } |
208 | 255 | } else { |
209 | 256 | userLocale = new Locale("en","US"); |
210 | 257 | } |
|
276 | 323 | userLocale = new Locale(browserLocale.getLanguage(), countryCode); |
277 | 324 | } else if (StringUtils.isNotBlank(fallbackCountryCode)){ |
278 | 325 | userLocale = new Locale(browserLocale.getLanguage(), fallbackCountryCode); |
| 326 | + } else if (StringUtils.isNotBlank(browserLocale.getLanguage())){ |
| 327 | + for (String key : supportedLanguages.keySet()) { |
| 328 | + if (key.startsWith(browserLocale.getLanguage() + "_")) { |
| 329 | + String[] parts = key.split("_"); |
| 330 | + userLocale = new Locale(parts[0], parts[1]); |
| 331 | + break; |
| 332 | + } |
| 333 | + } |
279 | 334 | } else { |
280 | 335 | userLocale = new Locale("en","US"); |
281 | 336 | } |
282 | | - } |
| 337 | + } |
283 | 338 | } |
284 | 339 |
|
285 | 340 | ResourceBundle recoveryResourceBundle = ResourceBundle.getBundle(BUNDLE, userLocale, new |
|
395 | 450 | } |
396 | 451 | %> |
397 | 452 |
|
398 | | -<%! |
| 453 | +<%! |
399 | 454 | private static final String LOCALE_CODE_KEY = "localeCode"; |
400 | 455 | private static final String FLAG_CODE_KEY = "flagCode"; |
401 | 456 | private static final String DISPLAY_NAME_KEY = "displayName"; |
|
415 | 470 | localeProperties.load(localeReader); |
416 | 471 | for (String key : localeProperties.stringPropertyNames()) { |
417 | 472 | String[] values = localeProperties.getProperty(key).split(","); |
418 | | - |
| 473 | +
|
419 | 474 | // Validate the number of values |
420 | 475 | if (values.length == 3) { |
421 | 476 | String flagCode = values[0].trim(); |
|
0 commit comments