@picgo/i18n
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("fs"),require("path"),require("chalk")):"function"==typeof define&&define.amd?define(["exports","fs","path","chalk"],t):t((e=e||self).i18n={},e.fs,e.path,e.chalk)}(this,(function(e,t,o,s){"use strict";s=s&&s.hasOwnProperty("default")?s.default:s;class a{}var r,l,c;!function(e){e.change="change"}(r||(r={})),function(e){e.dev="development"}(l||(l={})),function(e){e.node="node",e.web="web"}(c||(c={}));const n=new class{constructor(){this.isNode=this.getPlatform()===c.node}error(e){const t=this.formatOutput(e);console.error(this.isNode?s.red(t):t)}log(e){const t=this.formatOutput(e);console.log(this.isNode?s.gray(t):t)}warn(e){const t=this.formatOutput(e);console.warn(this.isNode?s.yellow(t):t)}getPlatform(){return void 0!==typeof process&&"[object process]"===Object.prototype.toString.call(process)?c.node:c.web}formatOutput(e){return`[i18n] ${e}`}};e.BaseAdapter=a,e.FileSyncAdapter=class extends a{constructor(e){super(),this.locales={},this.localeFileName={};const{localesBaseDir:t,localeFileName:o}=e;this.localesBaseDir=t,o?this.localeFileName=o:this.guessLocaleFileName(t),process.env.NODE_ENV===l.dev&&this.watch(this.localesBaseDir)}getLocale(e){return this.locales[e]||this.loadLocale(e),this.locales[e]}loadLocale(e){if(!this.localeFileName[e])return void n.error(`can't locate the locale file of language ${e}`);const s=o.join(this.localesBaseDir,this.localeFileName[e]),a=t.readFileSync(s,{encoding:"utf-8"});try{const t=JSON.parse(a);this.locales[e]=t}catch(e){n.error(`unable to parse locales from file (maybe ${s} is empty or invalid json?)`),n.error(`raw error info: ${e}`)}}guessLocaleFileName(e){const s=t.readdirSync(e),a={};s.forEach(e=>{const t=e.replace(o.extname(e),"");a[t]=e}),n.log(`guess locale file path from ${e}`),n.log(`localeFileName: ${JSON.stringify(a)}`),this.localeFileName=a}watch(e){t.watch(e,(e,t)=>{let o="";const{localeFileName:s}=this;for(const e in s)s.hasOwnProperty(e)&&s[e]===t&&(o=e);o&&e===r.change&&(this.loadLocale(o),n.log(`${t} has updated`))})}},e.I18n=class{constructor(e){const{adapter:t,defaultLanguage:o}=e;this.adapter=t,this.currentLanguage=o.trim().toLowerCase()}getLanguage(){return this.currentLanguage}setLanguage(e){this.currentLanguage=e.trim().toLowerCase()}translate(e,t){const o=this.adapter.getLocale(this.currentLanguage);if(!o)return void n.error("current locale is null");const s=e.split(".").reduce((t,o)=>{if(t&&t.hasOwnProperty(o))return t[o];n.warn(`current locale does't contain ${e}`)},o);return this.postProcess(s,t)}postProcess(e,t){if(e)return Object.keys(t).reduce((e,o)=>e.replace("${"+o+"}",t[o]),e)}},e.ObjectAdapter=class extends a{constructor(e){super(),this.locales={},this.locales=e}getLocale(e){return this.locales[e]}setLocales(e){this.locales=e}},e.logger=n,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("fs"),require("path"),require("chalk")):"function"==typeof define&&define.amd?define(["exports","fs","path","chalk"],t):t((e=e||self).PicGo_I18n={},e.fs,e.path,e.chalk)}(this,(function(e,t,o,s){"use strict";s=s&&s.hasOwnProperty("default")?s.default:s;class a{}var r,l,c;!function(e){e.change="change"}(r||(r={})),function(e){e.dev="development"}(l||(l={})),function(e){e.node="node",e.web="web"}(c||(c={}));const n=new class{constructor(){this.isNode=this.getPlatform()===c.node}error(e){const t=this.formatOutput(e);console.error(this.isNode?s.red(t):t)}log(e){const t=this.formatOutput(e);console.log(this.isNode?s.gray(t):t)}warn(e){const t=this.formatOutput(e);console.warn(this.isNode?s.yellow(t):t)}getPlatform(){return"undefined"!=typeof process&&"[object process]"===Object.prototype.toString.call(process)?c.node:c.web}formatOutput(e){return`[i18n] ${e}`}};e.BaseAdapter=a,e.FileSyncAdapter=class extends a{constructor(e){super(),this.locales={},this.localeFileName={};const{localesBaseDir:t,localeFileName:o}=e;this.localesBaseDir=t,o?this.localeFileName=o:this.guessLocaleFileName(t),process.env.NODE_ENV===l.dev&&this.watch(this.localesBaseDir)}getLocale(e){return this.locales[e]||this.loadLocale(e),this.locales[e]}loadLocale(e){if(!this.localeFileName[e])return void n.error(`can't locate the locale file of language ${e}`);const s=o.join(this.localesBaseDir,this.localeFileName[e]),a=t.readFileSync(s,{encoding:"utf-8"});try{const t=JSON.parse(a);this.locales[e]=t}catch(e){n.error(`unable to parse locales from file (maybe ${s} is empty or invalid json?)`),n.error(`raw error info: ${e}`)}}guessLocaleFileName(e){const s=t.readdirSync(e),a={};s.forEach(e=>{const t=e.replace(o.extname(e),"");a[t]=e}),n.log(`guess locale file path from ${e}`),n.log(`localeFileName: ${JSON.stringify(a)}`),this.localeFileName=a}watch(e){t.watch(e,(e,t)=>{let o="";const{localeFileName:s}=this;for(const e in s)s.hasOwnProperty(e)&&s[e]===t&&(o=e);o&&e===r.change&&(this.loadLocale(o),n.log(`${t} has updated`))})}},e.I18n=class{constructor(e){const{adapter:t,defaultLanguage:o}=e;this.adapter=t,this.currentLanguage=o.trim().toLowerCase()}getLanguage(){return this.currentLanguage}setLanguage(e){this.currentLanguage=e.trim().toLowerCase()}translate(e,t){const o=this.adapter.getLocale(this.currentLanguage);if(!o)return void n.error("current locale is null");const s=e.split(".").reduce((t,o)=>{if(t&&t.hasOwnProperty(o))return t[o];n.warn(`current locale does't contain ${e}`)},o);return this.postProcess(s,t)}postProcess(e,t){if(e)return Object.keys(t).reduce((e,o)=>e.replace("${"+o+"}",t[o]),e)}},e.ObjectAdapter=class extends a{constructor(e){super(),this.locales={},this.locales=e}getLocale(e){return this.locales[e]}setLocales(e){this.locales=e}},e.logger=n,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@picgo/i18n", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "i18n tool", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -69,3 +69,3 @@ ## i18n | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/@picgo/i18n@0.0.3/dist/i18n_umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/@picgo/i18n@latest/dist/i18n_umd.js"></script> | ||
``` | ||
@@ -72,0 +72,0 @@ |
import chalk from 'chalk'; | ||
import { EPlatform } from 'src/types'; | ||
import { EPlatform } from '../types'; | ||
class Logger { | ||
@@ -4,0 +4,0 @@ private isNode: boolean; |
32183
41
720
5