Created
December 27, 2014 13:40
-
-
Save TheFinestArtist/2fd1b4aa1d4824fcbaef to your computer and use it in GitHub Desktop.
Detect whether current language is Korean, Japanese or Others
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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