Initial commit.

This commit is contained in:
genxium
2022-09-20 23:50:01 +08:00
commit e90a335c56
432 changed files with 101884 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
const Polyglot = require('polyglot.min');
let polyInst = null;
if (!window.i18n) {
window.i18n = {
languages: {},
curLang:''
};
}
if (CC_EDITOR) {
Editor.Profile.load('profile://project/i18n.json', (err, profile) => {
window.i18n.curLang = profile.data['default_language'];
if (polyInst) {
let data = loadLanguageData(window.i18n.curLang) || {};
initPolyglot(data);
}
});
}
function loadLanguageData (language) {
return window.i18n.languages[language];
}
function initPolyglot (data) {
if (data) {
if (polyInst) {
polyInst.replace(data);
} else {
polyInst = new Polyglot({ phrases: data, allowMissing: true });
}
}
}
module.exports = {
/**
* This method allow you to switch language during runtime, language argument should be the same as your data file name
* such as when language is 'zh', it will load your 'zh.js' data source.
* @method init
* @param language - the language specific data file name, such as 'zh' to load 'zh.js'
*/
init (language) {
if (language === window.i18n.curLang) {
return;
}
let data = loadLanguageData(language) || {};
window.i18n.curLang = language;
initPolyglot(data);
this.inst = polyInst;
},
/**
* this method takes a text key as input, and return the localized string
* Please read https://github.com/airbnb/polyglot.js for details
* @method t
* @return {String} localized string
* @example
*
* var myText = i18n.t('MY_TEXT_KEY');
*
* // if your data source is defined as
* // {"hello_name": "Hello, %{name}"}
* // you can use the following to interpolate the text
* var greetingText = i18n.t('hello_name', {name: 'nantas'}); // Hello, nantas
*/
t (key, opt) {
if (polyInst) {
return polyInst.t(key, opt);
}
},
inst: polyInst,
updateSceneRenderers () { // very costly iterations
let rootNodes = cc.director.getScene().children;
// walk all nodes with localize label and update
let allLocalizedLabels = [];
for (let i = 0; i < rootNodes.length; ++i) {
let labels = rootNodes[i].getComponentsInChildren('LocalizedLabel');
Array.prototype.push.apply(allLocalizedLabels, labels);
}
for (let i = 0; i < allLocalizedLabels.length; ++i) {
let label = allLocalizedLabels[i];
if(!label.node.active)continue;
label.updateLabel();
}
// walk all nodes with localize sprite and update
let allLocalizedSprites = [];
for (let i = 0; i < rootNodes.length; ++i) {
let sprites = rootNodes[i].getComponentsInChildren('LocalizedSprite');
Array.prototype.push.apply(allLocalizedSprites, sprites);
}
for (let i = 0; i < allLocalizedSprites.length; ++i) {
let sprite = allLocalizedSprites[i];
if(!sprite.node.active)continue;
sprite.updateSprite(window.i18n.curLang);
}
}
};

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "61de0eb6-9f87-49ed-933d-fd776e9a8ce7",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,79 @@
const i18n = require('LanguageData');
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
cc.Class({
extends: cc.Component,
editor: {
executeInEditMode: true,
menu: 'i18n/LocalizedLabel'
},
properties: {
dataID: {
get () {
return this._dataID;
},
set (val) {
if (this._dataID !== val) {
this._dataID = val;
if (CC_EDITOR) {
this._debouncedUpdateLabel();
} else {
this.updateLabel();
}
}
}
},
_dataID: ''
},
onLoad () {
if(CC_EDITOR) {
this._debouncedUpdateLabel = debounce(this.updateLabel, 200);
}
if (!i18n.inst) {
i18n.init();
}
// cc.log('dataID: ' + this.dataID + ' value: ' + i18n.t(this.dataID));
this.fetchRender();
},
fetchRender () {
let label = this.getComponent(cc.Label);
if (label) {
this.label = label;
this.updateLabel();
return;
}
},
updateLabel () {
if (!this.label) {
cc.error('Failed to update localized label, label component is invalid!');
return;
}
let localizedString = i18n.t(this.dataID);
if (localizedString) {
this.label.string = i18n.t(this.dataID);
}
}
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "744dcb38-0c27-4da6-b361-1b4c70aba14a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,54 @@
const SpriteFrameSet = require('SpriteFrameSet');
cc.Class({
extends: cc.Component,
editor: {
executeInEditMode: true,
inspector: 'packages://i18n/inspector/localized-sprite.js',
menu: 'i18n/LocalizedSprite'
},
properties: {
spriteFrameSet: {
default: [],
type: SpriteFrameSet
}
},
onLoad () {
this.fetchRender();
},
fetchRender () {
let sprite = this.getComponent(cc.Sprite);
if (sprite) {
this.sprite = sprite;
this.updateSprite(window.i18n.curLang);
return;
}
},
getSpriteFrameByLang (lang) {
for (let i = 0; i < this.spriteFrameSet.length; ++i) {
if (this.spriteFrameSet[i].language === lang) {
return this.spriteFrameSet[i].spriteFrame;
}
}
},
updateSprite (language) {
if (!this.sprite) {
cc.error('Failed to update localized sprite, sprite component is invalid!');
return;
}
let spriteFrame = this.getSpriteFrameByLang(language);
if (!spriteFrame && this.spriteFrameSet[0]) {
spriteFrame = this.spriteFrameSet[0].spriteFrame;
}
this.sprite.spriteFrame = spriteFrame;
}
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "f34acd86-1a25-4e05-b1ba-5e57ef8183f8",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,9 @@
const SpriteFrameSet = cc.Class({
name: 'SpriteFrameSet',
properties: {
language: '',
spriteFrame: cc.SpriteFrame
}
});
module.exports = SpriteFrameSet;

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "9701943c-d23a-44d9-87f3-e336ee09906a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,17 @@
// (c) 2012 Airbnb, Inc.
//
// polyglot.js may be freely distributed under the terms of the BSD
// license. For all licensing information, details, and documention:
// http://airbnb.github.com/polyglot.js
//
//
// Polyglot.js is an I18n helper library written in JavaScript, made to
// work both in the browser and in Node. It provides a simple solution for
// interpolation and pluralization, based off of Airbnb's
// experience adding I18n functionality to its Backbone.js and Node apps.
//
// Polylglot is agnostic to your translation backend. It doesn't perform any
// translation; it simply gives you a way to manage translated phrases from
// your client- or server-side JavaScript application.
//
(function(e,t){typeof define=="function"&&define.amd?define([],function(){return t(e)}):typeof exports=="object"?module.exports=t(e):e.Polyglot=t(e)})(this,function(e){"use strict";function t(e){e=e||{},this.phrases={},this.extend(e.phrases||{}),this.currentLocale=e.locale||"en",this.allowMissing=!!e.allowMissing,this.warn=e.warn||c}function s(e){var t,n,r,i={};for(t in e)if(e.hasOwnProperty(t)){n=e[t];for(r in n)i[n[r]]=t}return i}function o(e){var t=/^\s+|\s+$/g;return e.replace(t,"")}function u(e,t,r){var i,s,u;return r!=null&&e?(s=e.split(n),u=s[f(t,r)]||s[0],i=o(u)):i=e,i}function a(e){var t=s(i);return t[e]||t.en}function f(e,t){return r[a(e)](t)}function l(e,t){for(var n in t)n!=="_"&&t.hasOwnProperty(n)&&(e=e.replace(new RegExp("%\\{"+n+"\\}","g"),t[n]));return e}function c(t){e.console&&e.console.warn&&e.console.warn("WARNING: "+t)}function h(e){var t={};for(var n in e)t[n]=e[n];return t}t.VERSION="0.4.3",t.prototype.locale=function(e){return e&&(this.currentLocale=e),this.currentLocale},t.prototype.extend=function(e,t){var n;for(var r in e)e.hasOwnProperty(r)&&(n=e[r],t&&(r=t+"."+r),typeof n=="object"?this.extend(n,r):this.phrases[r]=n)},t.prototype.clear=function(){this.phrases={}},t.prototype.replace=function(e){this.clear(),this.extend(e)},t.prototype.t=function(e,t){var n,r;return t=t==null?{}:t,typeof t=="number"&&(t={smart_count:t}),typeof this.phrases[e]=="string"?n=this.phrases[e]:typeof t._=="string"?n=t._:this.allowMissing?n=e:(this.warn('Missing translation for key: "'+e+'"'),r=e),typeof n=="string"&&(t=h(t),r=u(n,this.currentLocale,t.smart_count),r=l(r,t)),r},t.prototype.has=function(e){return e in this.phrases};var n="||||",r={chinese:function(e){return 0},german:function(e){return e!==1?1:0},french:function(e){return e>1?1:0},russian:function(e){return e%10===1&&e%100!==11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2},czech:function(e){return e===1?0:e>=2&&e<=4?1:2},polish:function(e){return e===1?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2},icelandic:function(e){return e%10!==1||e%100===11?1:0}},i={chinese:["fa","id","ja","ko","lo","ms","th","tr","zh"],german:["da","de","en","es","fi","el","he","hu","it","nl","no","pt","sv"],french:["fr","tl","pt-br"],russian:["hr","ru"],czech:["cs"],polish:["pl"],icelandic:["is"]};return t});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "e26fdf72-cbae-40e2-adff-264a559c5620",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}