Java
do not provide any APIs, to get the alphabets in all the supported locales. I
am going to use icu4j API to get the alphabets information.
Test.java
<!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j
-->
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>62.1</version>
</dependency>
Below
function return list of locales that has upper and lower-case letters.
public static List<Locale> getLanguagesThatHasLowerAndUpper() { Locale[] locales = Locale.getAvailableLocales(); List<Locale> output = new ArrayList<>(); for (Locale locale : locales) { String lowerCase = getLowerAlphabets(locale); String upperCase = getUpperAlphabets(locale); if (!lowerCase.equals(upperCase)) { output.add(locale); } } return output; }
Find
the below working application.
AlphabetUtil.java
package com.smaple.app.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import com.ibm.icu.text.UnicodeSet; import com.ibm.icu.util.LocaleData; import com.ibm.icu.util.ULocale; public class AlphabetUtil { /** * Return lower case alphabets of given locale * * @param locale * @return */ public static String getLowerAlphabets(Locale locale) { if (locale == null) { throw new IllegalArgumentException("locale coudn't be null"); } ULocale ulocale = ULocale.forLocale(locale); UnicodeSet set = LocaleData.getExemplarSet(ulocale, LocaleData.ES_STANDARD); Iterator<String> iterator = set.iterator(); StringBuilder builder = new StringBuilder(); while (iterator.hasNext()) { builder.append(iterator.next()); } return builder.toString(); } /** * Return upper case alphabets of given locale. * * @param locale * @return */ public static String getUpperAlphabets(Locale locale) { return getLowerAlphabets(locale).toUpperCase(locale); } public static List<Locale> getLanguagesThatHasLowerAndUpper() { Locale[] locales = Locale.getAvailableLocales(); List<Locale> output = new ArrayList<>(); for (Locale locale : locales) { String lowerCase = getLowerAlphabets(locale); String upperCase = getUpperAlphabets(locale); if (!lowerCase.equals(upperCase)) { output.add(locale); } } return output; } }
Test.java
package com.sample.app; import java.util.List; import java.util.Locale; import com.smaple.app.util.AlphabetUtil; public class Test { public static void main(String args[]) { List<Locale> locales = AlphabetUtil.getLanguagesThatHasLowerAndUpper(); for (Locale locale : locales) { System.out.println(locale); } } }
Output
hr_HR fr_BE es_PA mt_MT es_VE bg it uk lv da_DK es_PR vi_VN en_US sr_ME sv_SE es_BO en_SG pt sk ga en_MT fi_FI et sv cs sr_BA_#Latn el uk_UA hu fr_CH in es_AR es_SV pt_BR be is_IS cs_CZ es pl_PL tr ca_ES sr_CS ms_MY hr lt es_ES es_CO bg_BG sq fr sr_BA is es_PY de es_EC es_US en ro_RO en_PH ca sr_ME_#Latn es_GT sl el_CY es_MX ru_RU es_HN no_NO_NY hu_HU es_CL fi ga_IE mk tr_TR et_EE sr__#Latn pt_PT fr_LU sq_AL es_DO es_CU ru en_NZ sr_RS de_CH es_UY ms el_GR en_ZA fr_FR de_AT nl no_NO en_AU vi nl_NL fr_CA lv_LV de_LU es_CR sr mt it_CH da de_DE sk_SK lt_LT it_IT en_IE ro en_CA nl_BE no pl de_GR sr_RS_#Latn en_IN es_NI mk_MK be_BY sl_SI es_PE in_ID en_GB
Below
program writes all the locales that has lower and upper case characters to a
file in html format.
AlphabetUtil.java
package com.smaple.app.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import com.ibm.icu.text.UnicodeSet; import com.ibm.icu.util.LocaleData; import com.ibm.icu.util.ULocale; public class AlphabetUtil { /** * Return lower case alphabets of given locale * * @param locale * @return */ public static String getLowerAlphabets(Locale locale) { if (locale == null) { throw new IllegalArgumentException("locale coudn't be null"); } ULocale ulocale = ULocale.forLocale(locale); UnicodeSet set = LocaleData.getExemplarSet(ulocale, LocaleData.ES_STANDARD); Iterator<String> iterator = set.iterator(); StringBuilder builder = new StringBuilder(); while (iterator.hasNext()) { builder.append(iterator.next()); } return builder.toString(); } /** * Return upper case alphabets of given locale. * * @param locale * @return */ public static String getUpperAlphabets(Locale locale) { return getLowerAlphabets(locale).toUpperCase(locale); } public static List<Locale> getLanguagesThatHasLowerAndUpper() { Locale[] locales = Locale.getAvailableLocales(); List<Locale> output = new ArrayList<>(); for (Locale locale : locales) { String lowerCase = getLowerAlphabets(locale); String upperCase = getUpperAlphabets(locale); if (!lowerCase.equals(upperCase)) { output.add(locale); } } return output; } /** * Write the languages that support both upper and lower case to a html file. * * @param filePath */ public static void writeLowerAndUpperCaseSupportedLang(String htmlFilePath) { if (htmlFilePath == null || htmlFilePath.isEmpty()) { htmlFilePath = "result.html"; } StringBuilder builder = new StringBuilder(); builder.append("<html>"); builder.append("<head>\r\n" + "<style>\r\n" + "table, th, td {\r\n" + " border: 1px solid black;\r\n" + "}\r\n" + "</style>\r\n" + "</head>"); builder.append("<body><table style=\"width:100%;text-align:left;background-color:gold;\""); builder.append("<tr><td>Locale</td>"); builder.append("<td>Lower Case Alphabets</td>"); builder.append("<td>Upper Case Alphabets</td></tr>"); Locale[] locales = Locale.getAvailableLocales(); for (Locale locale : locales) { String lowerCase = getLowerAlphabets(locale); String upperCase = getUpperAlphabets(locale); if (!lowerCase.equals(upperCase)) { builder.append("<tr><td>" + locale.toString() + "</td>"); builder.append("<td>" + lowerCase + "</td>"); builder.append("<td>" + upperCase + "</td></tr>"); } } writeDataToFile(htmlFilePath, builder.toString()); } private static void writeDataToFile(String htmlFilePath, String data) { File file = new File(htmlFilePath); String encoding = "UTF8"; try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), encoding)) { writer.write(data); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
No comments:
Post a Comment