Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localization support for authentication and recovery endpoints #6406

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-onions-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wso2is/identity-apps-core": patch
---

Add localization Support
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@
<link href="css/language-selector.css" rel="stylesheet">

<%
// Specify the file path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayeshajay Could you please have a look?

String filePath = application.getRealPath("/") + "/WEB-INF/classes/LanguageOptions.properties";

// Create a List to store the parsed data
List<String[]> languageList = new ArrayList<>();

Expand All @@ -157,12 +154,20 @@
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
String languageCode = parts[parts.length - 1];
// Split the value further using ',' as the delimiter
// Split the code further using '_' as the delimiter.
String[] languageCodeParts = languageCode.split("_");
if (languageCodeParts.length != 2) {
continue;
}
// Split the value further using ',' as the delimiter.
String[] values = keyValue[1].split(",");
String country = values[0];
String displayName = values[1];
// Add the values to the list
languageList.add(new String[]{languageCode, country, displayName});

if (supportedLanguages.containsKey(languageCodeParts[0]) &&
AfraHussaindeen marked this conversation as resolved.
Show resolved Hide resolved
languageSupportedCountries.contains(languageCodeParts[1])) {
languageList.add(new String[]{languageCode, country, displayName});
}
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
String uiLocaleFromURL = request.getParameter("ui_locales");
String localeFromCookie = null;
String BUNDLE = "org.wso2.carbon.identity.application.authentication.endpoint.i18n.Resources";

// Map to store default supported language codes.
// TODO: Use this map to generate the `language-switcher.jsp`.
Map<String, String> supportedLanguages = new HashMap<>();
Expand All @@ -56,7 +56,58 @@
languageSupportedCountries.add("JP");
languageSupportedCountries.add("BR");

// Check cookie for the user selected language first
// Specify the file path.
String filePath = application.getRealPath("/") + "/WEB-INF/classes/LanguageOptions.properties";

// Use a BufferedReader to read the file content.
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// Trim the line and ignore comments and empty lines.
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue;
}

String[] keyValue = line.split("=");
if (keyValue.length != 2) {
continue;
}

// Split the key further using '.' as the delimiter.
String[] parts = keyValue[0].split("\\.");
if (parts.length == 0) {
continue;
}

// Split the code further using '_' as the delimiter.
String[] languageCode = parts[parts.length - 1].split("_");
if (languageCode.length != 2) {
continue;
}

// Find out whether we have resource bundle for the given locale.
Locale tempLocale = new Locale(languageCode[0], languageCode[1]);
try {
ResourceBundle foundBundle = ResourceBundle.getBundle(BUNDLE, tempLocale);

if (tempLocale.getLanguage().equals(foundBundle.getLocale().getLanguage()) &&
tempLocale.getCountry().equals(foundBundle.getLocale().getCountry())) {
// If the bundle is found, add the language to the supported list.
supportedLanguages.putIfAbsent(languageCode[0], languageCode[1]);
if (!languageSupportedCountries.contains(languageCode[1])) {
languageSupportedCountries.add(languageCode[1]);
}
}
} catch (Exception e) {
// Bundle not found, do not add this language.
}
}
} catch (Exception e) {
throw e;
}

// Check cookie for the user selected language first.
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
Expand Down Expand Up @@ -230,7 +281,7 @@
String COUNTRY_PLACEHOLDER = "{{country}}";
String LOCALE_PLACEHOLDER = "{{locale}}";

if (transformedLink.contains(LANGUAGE_PLACEHOLDER) || transformedLink.contains(COUNTRY_PLACEHOLDER) || transformedLink.contains(LOCALE_PLACEHOLDER)) {
if (transformedLink.contains(LANGUAGE_PLACEHOLDER) || transformedLink.contains(COUNTRY_PLACEHOLDER) || transformedLink.contains(LOCALE_PLACEHOLDER)) {
transformedLink = transformedLink
.replace("{{lang}}", langCode)
.replace("{{country}}", countryCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@
<link href="css/language-selector.css" rel="stylesheet">

<%
// Specify the file path
String filePath = application.getRealPath("/") + "/WEB-INF/classes/LanguageOptions.properties";

// Create a List to store the parsed data
List<String[]> languageList = new ArrayList<>();

Expand All @@ -146,12 +143,19 @@
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
String languageCode = parts[parts.length - 1];
// Split the value further using ',' as the delimiter
// Split the code further using '_' as the delimiter.
String[] languageCodeParts = languageCode.split("_");
if (languageCodeParts.length != 2) {
continue;
}
// Split the value further using ',' as the delimiter.
String[] values = keyValue[1].split(",");
String country = values[0];
String displayName = values[1];
// Add the values to the list
languageList.add(new String[]{languageCode, country, displayName});
if (supportedLanguages.containsKey(languageCodeParts[0]) &&
languageSupportedCountries.contains(languageCodeParts[1])) {
languageList.add(new String[]{languageCode, country, displayName});
}
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
supportedLanguages.put("de", "DE");
supportedLanguages.put("zh", "CN");
supportedLanguages.put("ja", "JP");

List<String> languageSupportedCountries = new ArrayList<>();
languageSupportedCountries.add("US");
languageSupportedCountries.add("FR");
Expand All @@ -56,7 +56,58 @@
languageSupportedCountries.add("JP");
languageSupportedCountries.add("BR");

// Check cookie for the user selected language first
// Specify the file path.
String filePath = application.getRealPath("/") + "/WEB-INF/classes/LanguageOptions.properties";

// Use a BufferedReader to read the file content.
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// Trim the line and ignore comments and empty lines.
line = line.trim();
if (line.isEmpty() || line.startsWith("#")) {
continue;
}

String[] keyValue = line.split("=");
if (keyValue.length != 2) {
continue;
}

// Split the key further using '.' as the delimiter.
String[] parts = keyValue[0].split("\\.");
if (parts.length == 0) {
continue;
}

// Split the code further using '_' as the delimiter.
String[] languageCode = parts[parts.length - 1].split("_");
if (languageCode.length != 2) {
continue;
}

// Find out whether we have resource bundle for the given locale.
Locale tempLocale = new Locale(languageCode[0], languageCode[1]);
try {
ResourceBundle foundBundle = ResourceBundle.getBundle(BUNDLE, tempLocale);

if (tempLocale.getLanguage().equals(foundBundle.getLocale().getLanguage()) &&
tempLocale.getCountry().equals(foundBundle.getLocale().getCountry())) {
// If the bundle is found, add the language to the supported list.
supportedLanguages.putIfAbsent(languageCode[0], languageCode[1]);
if (!languageSupportedCountries.contains(languageCode[1])) {
languageSupportedCountries.add(languageCode[1]);
}
}
} catch (Exception e) {
// Bundle not found, do not add this language.
}
}
} catch (Exception e) {
throw e;
}

// Check cookie for the user selected language first.
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
Expand Down Expand Up @@ -230,7 +281,7 @@
String COUNTRY_PLACEHOLDER = "{{country}}";
String LOCALE_PLACEHOLDER = "{{locale}}";

if (transformedLink.contains(LANGUAGE_PLACEHOLDER) || transformedLink.contains(COUNTRY_PLACEHOLDER) || transformedLink.contains(LOCALE_PLACEHOLDER)) {
if (transformedLink.contains(LANGUAGE_PLACEHOLDER) || transformedLink.contains(COUNTRY_PLACEHOLDER) || transformedLink.contains(LOCALE_PLACEHOLDER)) {
transformedLink = transformedLink
.replace("{{lang}}", langCode)
.replace("{{country}}", countryCode)
Expand Down
Loading