public static Locale localeFromString(final String localeAsString){
  if (StringUtils.isBlank(localeAsString)) {
    final List<ApiParameterError> dataValidationErrors=new ArrayList<>();
    final ApiParameterError error=ApiParameterError.parameterError("validation.msg.invalid.locale.format","The parameter locale is invalid. It cannot be blank.","locale");
    dataValidationErrors.add(error);
    throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist","Validation errors exist.",dataValidationErrors);
  }
  String languageCode="";
  String countryCode="";
  String variantCode="";
  final String[] localeParts=localeAsString.split("_");
  if (localeParts != null && localeParts.length == 1) {
    languageCode=localeParts[0];
  }
  if (localeParts != null && localeParts.length == 2) {
    languageCode=localeParts[0];
    countryCode=localeParts[1];
  }
  if (localeParts != null && localeParts.length == 3) {
    languageCode=localeParts[0];
    countryCode=localeParts[1];
    variantCode=localeParts[2];
  }
  return localeFrom(languageCode,countryCode,variantCode);
}

public static Locale localeFromString ( final String localeAsString ) { if ( StringUtils . isBlank ( localeAsString ) ) { final List < ApiParameterError > dataValidationErrors = new ArrayList < > ( ) ; final ApiParameterError error = ApiParameterError . parameterError ( "validation.msg.invalid.locale.format" , "The parameter locale is invalid. It cannot be blank." , "locale" ) ; dataValidationErrors . add ( error ) ; throw new PlatformApiDataValidationException ( "validation.msg.validation.errors.exist" , "Validation errors exist." , dataValidationErrors ) ; } String languageCode = "" ; String countryCode = "" ; String variantCode = "" ; final String [ ] localeParts = localeAsString . split ( "_" ) ; if ( localeParts != null && localeParts . length == 1 ) { languageCode = localeParts [ 0 ] ; } if ( localeParts != null && localeParts . length == 2 ) { languageCode = localeParts [ 0 ] ; countryCode = localeParts [ 1 ] ; } if ( localeParts != null && localeParts . length == 3 ) { languageCode = localeParts [ 0 ] ; countryCode = localeParts [ 1 ] ; variantCode = localeParts [ 2 ] ; } return localeFrom ( languageCode , countryCode , variantCode ) ; }
todo : vishwas move all locale related code to a separate utils class
