Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect whether current language is Korean, Japanese or Others
/**
* Created by TheFinestArtist on 2014. 9. 2..
*/
public class LanguageDetector {
public enum Language {Korean, Japanese, English}
public static boolean isEnglish(CharSequence charSequence) {
boolean isEnglish = true;
for (char c : charSequence.toString().toCharArray()) {
if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
isEnglish = false;
break;
}
}
return isEnglish;
}
public static boolean hasKorean(CharSequence charSequence) {
boolean hasKorean = false;
for (char c : charSequence.toString().toCharArray()) {
if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HANGUL_JAMO
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HANGUL_SYLLABLES) {
hasKorean = true;
break;
}
}
return hasKorean;
}
public static boolean hasJapanese(CharSequence charSequence) {
boolean hasJapanese = false;
for (char c : charSequence.toString().toCharArray()) {
if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HIRAGANA
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.KATAKANA
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
hasJapanese = true;
break;
}
}
return hasJapanese;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.