Tuesday 17 July 2018

Java: Get alphabets of all the supported languages

In this post, I am going to explain, how to get the alphabets in all the java supported languages.

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.

                  <!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j -->
                  <dependency>
                           <groupId>com.ibm.icu</groupId>
                           <artifactId>icu4j</artifactId>
                           <version>62.1</version>
                  </dependency>

Below method prints all the lower case alphabets in given language.
	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();
	}

Find the below working example.

AlphabetUtil.java
package com.smaple.app.util;

import java.util.Iterator;
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);
	}

}


Test.java
package com.sample.app;

import java.util.Locale;

import com.smaple.app.util.AlphabetUtil;

public class Test {

	public static void main(String args[]) {
		System.out.println(AlphabetUtil.getLowerAlphabets(Locale.ENGLISH));
		System.out.println(AlphabetUtil.getUpperAlphabets(Locale.ENGLISH));
	}
}

Output
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ


Print all the language alphabets to a html file
Below function writes all the alphabets of java supported locales to a file in html table format.
	public static void printToFile(String htmlFilePath) {
		if (htmlFilePath == null || htmlFilePath.isEmpty()) {
			htmlFilePath = "out.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) {
			builder.append("<tr><td>" + locale.toString() + "</td>");
			builder.append("<td>" + getLowerAlphabets(locale) + "</td>");
			builder.append("<td>" + getUpperAlphabets(locale) + "</td></tr>");
		}

		writeDataToFile(htmlFilePath, builder.toString());
	}


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.Iterator;
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);
	}

	/**
	 * Write all alphabets of java supported locales to a file in html format.
	 * 
	 * @param htmlFilePath
	 */
	public static void printToFile(String htmlFilePath) {
		if (htmlFilePath == null || htmlFilePath.isEmpty()) {
			htmlFilePath = "out.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) {
			builder.append("<tr><td>" + locale.toString() + "</td>");
			builder.append("<td>" + getLowerAlphabets(locale) + "</td>");
			builder.append("<td>" + getUpperAlphabets(locale) + "</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();
		}

	}

}

Test.java

package com.sample.app;

import com.smaple.app.util.AlphabetUtil;

public class Test {

	public static void main(String args[]) {
		AlphabetUtil.printToFile("localeAlphabets.html");
	}
}

Run Test.java, it copies all the alphabets of java supported locales to ‘localeAlphabtes.html’ file.

Open ‘localeAlphabtes.html’ file, you can able to see below kind of output.



You may like


No comments:

Post a Comment