1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: LocaleUtility.java,v 1.1.1.1 2004/02/26 11:40:13 vk112360 Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.Locale;
  21. /**
  22. * @author Igor Hersht, igorh@ca.ibm.com
  23. */
  24. public class LocaleUtility {
  25. /**
  26. * IETF RFC 1766 tag separator
  27. */
  28. public final static char IETF_SEPARATOR = '-';
  29. public final static String EMPTY_STRING = "";
  30. public static Locale langToLocale(String lang) {
  31. if((lang == null) || lang.equals(EMPTY_STRING)){ // not specified => getDefault
  32. return Locale.getDefault();
  33. }
  34. String language = EMPTY_STRING;
  35. String country = EMPTY_STRING;
  36. String variant = EMPTY_STRING;
  37. int i1 = lang.indexOf(IETF_SEPARATOR);
  38. if (i1 < 0) {
  39. language = lang;
  40. } else {
  41. language = lang.substring(0, i1);
  42. ++i1;
  43. int i2 = lang.indexOf(IETF_SEPARATOR, i1);
  44. if (i2 < 0) {
  45. country = lang.substring(i1);
  46. } else {
  47. country = lang.substring(i1, i2);
  48. variant = lang.substring(i2+1);
  49. }
  50. }
  51. if(language.length() == 2){
  52. language = language.toLowerCase();
  53. }else {
  54. language = EMPTY_STRING;
  55. }
  56. if(country.length() == 2){
  57. country = country.toUpperCase();
  58. }else {
  59. country = EMPTY_STRING;
  60. }
  61. if((variant.length() > 0) &&
  62. ((language.length() == 2) ||(country.length() == 2))){
  63. variant = variant.toUpperCase();
  64. }else{
  65. variant = EMPTY_STRING;
  66. }
  67. return new Locale(language, country, variant );
  68. }
  69. }