Skip to content

Commit

Permalink
Refactor code and add validation for existence of corresponding resou…
Browse files Browse the repository at this point in the history
…rce bundle.
  • Loading branch information
AfraHussaindeen committed Jun 6, 2024
1 parent f587094 commit ef82898
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,19 @@
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
String languageCode = parts[parts.length - 1];
// 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 @@ -63,29 +63,44 @@
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// Ignore comments and empty lines
if (!line.trim().startsWith("#") && !line.trim().isEmpty()) {
// Split the line into key and value using '=' as the delimiter
String[] keyValue = line.split("=");
if (keyValue.length == 2) {
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
// Ensure the key has at least one part
if (parts.length > 0) {
// Split the code further using '_' as the delimiter
String[] languageCode = parts[parts.length - 1].split("_");
// Ensure the languageCode has at least two parts (language and country)
if (languageCode.length == 2) {
// Add the values
if (!supportedLanguages.containsKey(languageCode[0])) {
supportedLanguages.put(languageCode[0], languageCode[1]);
}
if (!languageSupportedCountries.contains(languageCode[1])) {
languageSupportedCountries.add(languageCode[1]);
}
}
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,19 @@
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
String languageCode = parts[parts.length - 1];
// 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 @@ -63,29 +63,44 @@
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// Ignore comments and empty lines
if (!line.trim().startsWith("#") && !line.trim().isEmpty()) {
// Split the line into key and value using '=' as the delimiter
String[] keyValue = line.split("=");
if (keyValue.length == 2) {
// Split the key further using '.' as the delimiter
String[] parts = keyValue[0].split("\\.");
// Ensure the key has at least one part
if (parts.length > 0) {
// Split the code further using '_' as the delimiter
String[] languageCode = parts[parts.length - 1].split("_");
// Ensure the languageCode has at least two parts (language and country)
if (languageCode.length == 2) {
// Add the values
if (!supportedLanguages.containsKey(languageCode[0])) {
supportedLanguages.put(languageCode[0], languageCode[1]);
}
if (!languageSupportedCountries.contains(languageCode[1])) {
languageSupportedCountries.add(languageCode[1]);
}
}
// 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) {
Expand Down

0 comments on commit ef82898

Please sign in to comment.