61de0eb6-9f87-49ed-933d-fd776e9a8ce7.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. cc._RF.push(module, '61de062n4dJ7ZM9/Xdumozn', 'LanguageData');
  3. // LanguageData.js
  4. "use strict";
  5. var Polyglot = require('polyglot.min');
  6. var polyInst = null;
  7. if (!window.i18n) {
  8. window.i18n = {
  9. languages: {},
  10. curLang: ''
  11. };
  12. }
  13. if (CC_EDITOR) {
  14. Editor.Profile.load('profile://project/i18n.json', function (err, profile) {
  15. window.i18n.curLang = profile.data['default_language'];
  16. if (polyInst) {
  17. var data = loadLanguageData(window.i18n.curLang) || {};
  18. initPolyglot(data);
  19. }
  20. });
  21. }
  22. function loadLanguageData(language) {
  23. return window.i18n.languages[language];
  24. }
  25. function initPolyglot(data) {
  26. if (data) {
  27. if (polyInst) {
  28. polyInst.replace(data);
  29. } else {
  30. polyInst = new Polyglot({
  31. phrases: data,
  32. allowMissing: true
  33. });
  34. }
  35. }
  36. }
  37. module.exports = {
  38. /**
  39. * This method allow you to switch language during runtime, language argument should be the same as your data file name
  40. * such as when language is 'zh', it will load your 'zh.js' data source.
  41. * @method init
  42. * @param language - the language specific data file name, such as 'zh' to load 'zh.js'
  43. */
  44. init: function init(language) {
  45. if (language === window.i18n.curLang) {
  46. return;
  47. }
  48. var data = loadLanguageData(language) || {};
  49. window.i18n.curLang = language;
  50. initPolyglot(data);
  51. this.inst = polyInst;
  52. },
  53. /**
  54. * this method takes a text key as input, and return the localized string
  55. * Please read https://github.com/airbnb/polyglot.js for details
  56. * @method t
  57. * @return {String} localized string
  58. * @example
  59. *
  60. * var myText = i18n.t('MY_TEXT_KEY');
  61. *
  62. * // if your data source is defined as
  63. * // {"hello_name": "Hello, %{name}"}
  64. * // you can use the following to interpolate the text
  65. * var greetingText = i18n.t('hello_name', {name: 'nantas'}); // Hello, nantas
  66. */
  67. t: function t(key, opt) {
  68. if (polyInst) {
  69. return polyInst.t(key, opt);
  70. }
  71. },
  72. inst: polyInst,
  73. updateSceneRenderers: function updateSceneRenderers() {
  74. // very costly iterations
  75. var rootNodes = cc.director.getScene().children; // walk all nodes with localize label and update
  76. var allLocalizedLabels = [];
  77. for (var i = 0; i < rootNodes.length; ++i) {
  78. var labels = rootNodes[i].getComponentsInChildren('LocalizedLabel');
  79. Array.prototype.push.apply(allLocalizedLabels, labels);
  80. }
  81. for (var _i = 0; _i < allLocalizedLabels.length; ++_i) {
  82. var label = allLocalizedLabels[_i];
  83. if (!label.node.active) continue;
  84. label.updateLabel();
  85. } // walk all nodes with localize sprite and update
  86. var allLocalizedSprites = [];
  87. for (var _i2 = 0; _i2 < rootNodes.length; ++_i2) {
  88. var sprites = rootNodes[_i2].getComponentsInChildren('LocalizedSprite');
  89. Array.prototype.push.apply(allLocalizedSprites, sprites);
  90. }
  91. for (var _i3 = 0; _i3 < allLocalizedSprites.length; ++_i3) {
  92. var sprite = allLocalizedSprites[_i3];
  93. if (!sprite.node.active) continue;
  94. sprite.updateSprite(window.i18n.curLang);
  95. }
  96. }
  97. };
  98. cc._RF.pop();