Skip to content

Instantly share code, notes, and snippets.

@TheFinestArtist
Created December 27, 2014 13:40
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TheFinestArtist/2fd1b4aa1d4824fcbaef to your computer and use it in GitHub Desktop.
Save TheFinestArtist/2fd1b4aa1d4824fcbaef to your computer and use it in GitHub Desktop.
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