Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@picgo/i18n

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@picgo/i18n - npm Package Compare versions

Comparing version 0.0.4 to 1.0.0

CHANGELOG.md

9

.eslintrc.json

@@ -6,9 +6,12 @@ {

"ecmaVersion": 6,
"sourceType": "module"
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"extends": ["prettier/@typescript-eslint"],
"extends": ["standard-with-typescript"],
"rules": {
"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/semi": "error",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"no-prototype-builtins": "off",
"no-throw-literal": "error",

@@ -15,0 +18,0 @@ "no-unused-expressions": "error",

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAdapter = void 0;
class BaseAdapter {
}
exports.BaseAdapter = BaseAdapter;
import { BaseAdapter } from './base';
import { IFileSyncAdapterConstructorOptions } from '../types';
import { IFileSyncAdapterConstructorOptions, ILocale } from '../types';
export declare class FileSyncAdapter extends BaseAdapter {
private locales;
private localeFileName;
private localesBaseDir;
private readonly localesBaseDir;
constructor(options: IFileSyncAdapterConstructorOptions);
getLocale(language: string): import("../types").ILocale;
getLocale(language: string): ILocale;
private loadLocale;

@@ -10,0 +10,0 @@ private guessLocaleFileName;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSyncAdapter = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const fs = (0, tslib_1.__importStar)(require("fs"));
const path = (0, tslib_1.__importStar)(require("path"));
const base_1 = require("./base");

@@ -16,3 +17,3 @@ const utils_1 = require("../utils");

this.localesBaseDir = localesBaseDir;
if (localeFileName) {
if (localeFileName != null) {
this.localeFileName = localeFileName;

@@ -36,3 +37,3 @@ }

if (!this.localeFileName[language]) {
utils_1.logger.error(`can\'t locate the locale file of language ${language}`);
utils_1.logger.error(`can 't locate the locale file of language ${language}`);
return;

@@ -42,3 +43,3 @@ }

const fileContent = fs.readFileSync(filePath, {
encoding: 'utf-8',
encoding: 'utf-8'
});

@@ -45,0 +46,0 @@ try {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./base"), exports);
tslib_1.__exportStar(require("./file-sync"), exports);
tslib_1.__exportStar(require("./object"), exports);
(0, tslib_1.__exportStar)(require("./base"), exports);
(0, tslib_1.__exportStar)(require("./file-sync"), exports);
(0, tslib_1.__exportStar)(require("./object"), exports);

@@ -8,2 +8,3 @@ import { BaseAdapter } from './base';

setLocales(locales: ILocaleMap): void;
setLocale(language: string, locales: ILocale): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectAdapter = void 0;
const base_1 = require("./base");

@@ -17,3 +18,7 @@ class ObjectAdapter extends base_1.BaseAdapter {

}
// change the locale dynamic
setLocale(language, locales) {
this.locales[language] = locales;
}
}
exports.ObjectAdapter = ObjectAdapter;

@@ -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).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})}));
!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="undefined"!=typeof globalThis?globalThis:e||self).PicGo_I18n={},e.fs,e.path,e.chalk)}(this,(function(e,t,a,r){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function s(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(a){if("default"!==a){var r=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(t,a,r.get?r:{enumerable:!0,get:function(){return e[a]}})}})),t.default=e,Object.freeze(t)}var l,n,c,i=s(t),u=s(a),h=o(r);class f{}!function(e){e.change="change"}(l||(l={})),function(e){e.dev="development"}(n||(n={})),function(e){e.node="node",e.web="web"}(c||(c={}));const d=new class{constructor(){this.isNode=this.getPlatform()===c.node}error(e){const t=this.formatOutput(e);console.error(this.isNode?h.default.red(t):t)}log(e){const t=this.formatOutput(e);console.log(this.isNode?h.default.gray(t):t)}warn(e){const t=this.formatOutput(e);console.warn(this.isNode?h.default.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=f,e.FileSyncAdapter=class extends f{constructor(e){super(),this.locales={},this.localeFileName={};const{localesBaseDir:t,localeFileName:a}=e;this.localesBaseDir=t,null!=a?this.localeFileName=a:this.guessLocaleFileName(t),process.env.NODE_ENV===n.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 d.error(`can 't locate the locale file of language ${e}`);const t=u.join(this.localesBaseDir,this.localeFileName[e]),a=i.readFileSync(t,{encoding:"utf-8"});try{const t=JSON.parse(a);this.locales[e]=t}catch(e){d.error(`unable to parse locales from file (maybe ${t} is empty or invalid json?)`),d.error(`raw error info: ${e}`)}}guessLocaleFileName(e){const t=i.readdirSync(e),a={};t.forEach((e=>{const t=e.replace(u.extname(e),"");a[t]=e})),d.log(`guess locale file path from ${e}`),d.log(`localeFileName: ${JSON.stringify(a)}`),this.localeFileName=a}watch(e){i.watch(e,((e,t)=>{let a="";const{localeFileName:r}=this;for(const e in r)r.hasOwnProperty(e)&&r[e]===t&&(a=e);a&&e===l.change&&(this.loadLocale(a),d.log(`${t} has updated`))}))}},e.I18n=class{constructor(e){const{adapter:t,defaultLanguage:a}=e;this.adapter=t,this.currentLanguage=a.trim(),this.defaultLanguage=this.currentLanguage}getAdapter(){return this.adapter}getLanguage(){return this.currentLanguage}setLanguage(e){this.currentLanguage=e.trim()}setDefaultLanguage(e){this.defaultLanguage=e.trim()}getLocale(){let e=this.adapter.getLocale(this.currentLanguage);if(!e){if(e=this.adapter.getLocale(this.defaultLanguage),!e)return d.error(`current locale ${this.currentLanguage} is null`),null;d.error(`current locale ${this.currentLanguage} is null, change to default locale ${this.defaultLanguage}`)}return e}translate(e,t){const a=this.getLocale();if(!a)return;const r=e.split(".").reduce(((t,a)=>{if(t&&t.hasOwnProperty(a))return t[a];d.warn(`current locale doesn't contain ${e}`)}),a);return this.postProcess(r,t)}postProcess(e,t){if(e)return t?Object.keys(t).reduce(((e,a)=>e.replace("${"+a+"}",t[a])),e):e}},e.ObjectAdapter=class extends f{constructor(e){super(),this.locales={},this.locales=e}getLocale(e){return this.locales[e]}setLocales(e){this.locales=e}setLocale(e,t){this.locales[e]=t}},e.logger=d,Object.defineProperty(e,"__esModule",{value:!0})}));

@@ -0,10 +1,15 @@

import { BaseAdapter } from './adapters';
import { II18nConstructorOptions } from './types';
export declare class I18n {
private adapter;
private readonly adapter;
private currentLanguage;
private defaultLanguage;
constructor(options: II18nConstructorOptions);
getAdapter(): BaseAdapter;
getLanguage(): string;
setLanguage(language: string): void;
translate(phrase: string, args: any): string | undefined;
setDefaultLanguage(language: string): void;
private getLocale;
translate(phrase: string, args?: any): string | undefined;
private postProcess;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.I18n = void 0;
const utils_1 = require("./utils");

@@ -9,4 +10,8 @@ const DOTNOTATION = '.';

this.adapter = adapter;
this.currentLanguage = defaultLanguage.trim().toLowerCase();
this.currentLanguage = defaultLanguage.trim();
this.defaultLanguage = this.currentLanguage;
}
getAdapter() {
return this.adapter;
}
getLanguage() {

@@ -16,8 +21,22 @@ return this.currentLanguage;

setLanguage(language) {
this.currentLanguage = language.trim().toLowerCase();
this.currentLanguage = language.trim();
}
setDefaultLanguage(language) {
this.defaultLanguage = language.trim();
}
getLocale() {
let currentLocale = this.adapter.getLocale(this.currentLanguage);
if (!currentLocale) {
currentLocale = this.adapter.getLocale(this.defaultLanguage);
if (!currentLocale) {
utils_1.logger.error(`current locale ${this.currentLanguage} is null`);
return null;
}
utils_1.logger.error(`current locale ${this.currentLanguage} is null, change to default locale ${this.defaultLanguage}`);
}
return currentLocale;
}
translate(phrase, args) {
const currentLocale = this.adapter.getLocale(this.currentLanguage);
const currentLocale = this.getLocale();
if (!currentLocale) {
utils_1.logger.error('current locale is null');
return;

@@ -27,4 +46,4 @@ }

if (!object || !object.hasOwnProperty(key)) {
utils_1.logger.warn(`current locale does\'t contain ${phrase}`);
return;
utils_1.logger.warn(`current locale doesn't contain ${phrase}`);
return undefined;
}

@@ -39,2 +58,5 @@ return object[key];

}
if (!args) {
return template;
}
// see benchmark

@@ -41,0 +63,0 @@ return Object.keys(args).reduce((res, key) => {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./adapters"), exports);
tslib_1.__exportStar(require("./utils"), exports);
tslib_1.__exportStar(require("./i18n"), exports);
(0, tslib_1.__exportStar)(require("./adapters"), exports);
(0, tslib_1.__exportStar)(require("./utils"), exports);
(0, tslib_1.__exportStar)(require("./i18n"), exports);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EPlatform = exports.ERUN_ENV = exports.EFileChangeType = void 0;
var EFileChangeType;

@@ -4,0 +5,0 @@ (function (EFileChangeType) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./logger"), exports);
(0, tslib_1.__exportStar)(require("./logger"), exports);
declare class Logger {
private isNode;
private readonly isNode;
constructor();

@@ -4,0 +4,0 @@ error(info: string): void;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
const types_1 = require("../types");

@@ -6,0 +7,0 @@ class Logger {

{
"name": "@picgo/i18n",
"version": "0.0.4",
"version": "1.0.0",
"description": "i18n tool",

@@ -11,6 +11,5 @@ "main": "dist/index.js",

"lint": "eslint ./src{/,/**/}*.ts",
"codestyle:check": "prettier --check ./src{/,/**/}*.ts **/**.json",
"codestyle:fix": "prettier --write ./src{/,/**/}*.ts **/**.json",
"lint:fix": "eslint --fix ./src{/,/**/}*.ts",
"test": "mocha ./test/index.js",
"cz": "npm run codestyle:check && npm run lint && git-cz",
"cz": "npm run lint && git-cz",
"release": "bump-version"

@@ -47,30 +46,27 @@ },

"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@picgo/bump-version": "^1.0.3",
"@types/chalk": "^2.2.0",
"@types/node": "^13.1.7",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"commitizen": "^4.0.3",
"conventional-changelog": "^3.1.18",
"cz-customizable": "^6.2.0",
"eslint": "6",
"eslint-config-prettier": "^6.11.0",
"eslint-config-standard-with-typescript": "^11.0.1",
"eslint-plugin-import": "2",
"eslint-plugin-node": "9",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-promise": "4",
"eslint-plugin-standard": "4",
"husky": "^4.0.10",
"mocha": "^7.1.2",
"prettier": "^2.0.5",
"rollup": "^1.29.0",
"rollup-plugin-terser": "^5.2.0",
"rollup-plugin-typescript2": "^0.25.3",
"typescript": "^3.7.4"
"@commitlint/cli": "^16.0.1",
"@picgo/bump-version": "^1.1.2",
"@types/node": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"commitizen": "^4.2.4",
"conventional-changelog": "^3.1.25",
"cz-customizable": "^6.3.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard-with-typescript": "^21.0.1",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"husky": "^7.0.4",
"mocha": "^9.1.3",
"rollup": "^2.63.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.1",
"typescript": "^4.5.4"
},
"dependencies": {
"chalk": "^4.0.0"
"chalk": "4.1.2",
"tslib": "^2.3.1"
}
}

@@ -69,3 +69,3 @@ ## i18n

```html
<script src="https://cdn.jsdelivr.net/npm/@picgo/i18n@latest/dist/i18n_umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@picgo/i18n/dist/i18n_umd.js"></script>
```

@@ -72,0 +72,0 @@

@@ -77,4 +77,18 @@ const assert = require('assert');

});
it('getLocale null & translate to undefined', () => {
i18n.setLanguage('TEST')
assert.equal(i18n.translate('test'), undefined)
})
it('getLocale null but change to default', () => {
i18n.setDefaultLanguage('en')
assert.equal(i18n.translate('user.name'), 'PicGo')
})
it('language can has upper-case string', () => {
objectAdapter.setLocale('zh-CN', {
test: '测试PicGo'
})
i18n.setLanguage('zh-CN')
assert.equal(i18n.translate('test'), '测试PicGo')
})
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc