protected static String getLang ( ) { Locale locale = Locale . getDefault ( ) ; if ( null == locale ) return DEFAULT_LANGUAGE_CODE ; String language = locale . getLanguage ( ) ; String country = locale . getCountry ( ) ; String localeString = String . format ( "%s_%s" , language != null ? language . toLowerCase ( Locale . ENGLISH ) : "" , country != null ? country . toUpperCase ( Locale . ENGLISH ) : "" ) ; // 1. if no language is specified, return the default language if ( null == language ) return DEFAULT_LANGUAGE_CODE ; // 2. try to match the language or the entire locale string among the // list of available language codes String matchedLanguageCode = null ; for ( String languageCode : VALID_LANGUAGE_CODES ) { if ( languageCode . equals ( localeString ) ) { // return here as this is the most precise match we can get return localeString ; } if ( languageCode . equals ( language ) ) { // set the matched language code, and continue iterating as we // may match the localeString in a later iteration. matchedLanguageCode = language ; } } if ( null != matchedLanguageCode ) return matchedLanguageCode ; return DEFAULT_LANGUAGE_CODE ; }
Gets the current language guessed from the system .