LanguageData.js 3.6 KB

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