Lang.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/BaseTypes.js
  7. * @requires OpenLayers/Console.js
  8. */
  9. /**
  10. * Namespace: OpenLayers.Lang
  11. * Internationalization namespace. Contains dictionaries in various languages
  12. * and methods to set and get the current language.
  13. */
  14. OpenLayers.Lang = {
  15. /**
  16. * Property: code
  17. * {String} Current language code to use in OpenLayers. Use the
  18. * <setCode> method to set this value and the <getCode> method to
  19. * retrieve it.
  20. */
  21. code: null,
  22. /**
  23. * APIProperty: defaultCode
  24. * {String} Default language to use when a specific language can't be
  25. * found. Default is "en".
  26. */
  27. defaultCode: "en",
  28. /**
  29. * APIFunction: getCode
  30. * Get the current language code.
  31. *
  32. * Returns:
  33. * The current language code.
  34. */
  35. getCode: function() {
  36. if(!OpenLayers.Lang.code) {
  37. OpenLayers.Lang.setCode();
  38. }
  39. return OpenLayers.Lang.code;
  40. },
  41. /**
  42. * APIFunction: setCode
  43. * Set the language code for string translation. This code is used by
  44. * the <OpenLayers.Lang.translate> method.
  45. *
  46. * Parameters-
  47. * code - {String} These codes follow the IETF recommendations at
  48. * http://www.ietf.org/rfc/rfc3066.txt. If no value is set, the
  49. * browser's language setting will be tested. If no <OpenLayers.Lang>
  50. * dictionary exists for the code, the <OpenLayers.String.defaultLang>
  51. * will be used.
  52. */
  53. setCode: function(code) {
  54. var lang;
  55. if(!code) {
  56. code = (OpenLayers.BROWSER_NAME == "msie") ?
  57. navigator.userLanguage : navigator.language;
  58. }
  59. var parts = code.split('-');
  60. parts[0] = parts[0].toLowerCase();
  61. if(typeof OpenLayers.Lang[parts[0]] == "object") {
  62. lang = parts[0];
  63. }
  64. // check for regional extensions
  65. if(parts[1]) {
  66. var testLang = parts[0] + '-' + parts[1].toUpperCase();
  67. if(typeof OpenLayers.Lang[testLang] == "object") {
  68. lang = testLang;
  69. }
  70. }
  71. if(!lang) {
  72. OpenLayers.Console.warn(
  73. 'Failed to find OpenLayers.Lang.' + parts.join("-") +
  74. ' dictionary, falling back to default language'
  75. );
  76. lang = OpenLayers.Lang.defaultCode;
  77. }
  78. OpenLayers.Lang.code = lang;
  79. },
  80. /**
  81. * APIMethod: translate
  82. * Looks up a key from a dictionary based on the current language string.
  83. * The value of <getCode> will be used to determine the appropriate
  84. * dictionary. Dictionaries are stored in <OpenLayers.Lang>.
  85. *
  86. * Parameters:
  87. * key - {String} The key for an i18n string value in the dictionary.
  88. * context - {Object} Optional context to be used with
  89. * <OpenLayers.String.format>.
  90. *
  91. * Returns:
  92. * {String} A internationalized string.
  93. */
  94. translate: function(key, context) {
  95. var dictionary = OpenLayers.Lang[OpenLayers.Lang.getCode()];
  96. var message = dictionary && dictionary[key];
  97. if(!message) {
  98. // Message not found, fall back to message key
  99. message = key;
  100. }
  101. if(context) {
  102. message = OpenLayers.String.format(message, context);
  103. }
  104. return message;
  105. }
  106. };
  107. /**
  108. * APIMethod: OpenLayers.i18n
  109. * Alias for <OpenLayers.Lang.translate>. Looks up a key from a dictionary
  110. * based on the current language string. The value of
  111. * <OpenLayers.Lang.getCode> will be used to determine the appropriate
  112. * dictionary. Dictionaries are stored in <OpenLayers.Lang>.
  113. *
  114. * Parameters:
  115. * key - {String} The key for an i18n string value in the dictionary.
  116. * context - {Object} Optional context to be used with
  117. * <OpenLayers.String.format>.
  118. *
  119. * Returns:
  120. * {String} A internationalized string.
  121. */
  122. OpenLayers.i18n = OpenLayers.Lang.translate;