xshell 0.0.32 → 0.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/i18n/index.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare class I18N {
28
28
  [key: string]: any;
29
29
  }) => string;
30
30
  /** render: 翻译配置字段 */
31
- r: (field: Item | undefined | null) => string;
31
+ r: (field: Item | undefined | null, language?: Language) => string;
32
32
  i18next: I18Next;
33
33
  /** react-i18next <Trans/> 组件 */
34
34
  Trans: typeof Trans;
package/i18n/index.js CHANGED
@@ -47,8 +47,8 @@ class I18N {
47
47
  const language = options.language || this.language;
48
48
  return this.i18next.t(text, { ...options, lng: language, defaultValue: text });
49
49
  };
50
- this.r = (field) => field ?
51
- field[this.language] || field.en || field.zh || field || ''
50
+ this.r = (field, language = this.language) => field ?
51
+ field[language] || field.en || field.zh || field || ''
52
52
  :
53
53
  field || '';
54
54
  // --- init i18next
package/i18n/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;AAAA,oDAAmB;AACnB,kEAA+B;AAC/B,8DAGgB;AAGhB,uCAIkB;AAKL,QAAA,SAAS,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAU,CAAA;AAS1D;;;EAGE;AACF,MAAa,IAAI;IAyBb;;;;;;;;MAQE;IACF,YAAa,KAAY,EAAE,QAAmB;QAb9C,gCAAgC;QAChC,UAAK,GAAiB,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,QAAe,CAAA;QAanD,MAAM,UAAU,GAAG,OAAO,QAAQ,KAAK,WAAW,IAAI,OAAO,MAAM,KAAK,WAAW,CAAA;QAEnF,MAAM,IAAI,GAAG,IAAI,cAAI,CAAC,KAAK,CAAC,CAAA;QAE5B,+CAA+C;QAC/C,IAAI,UAAU,EAAE;YACZ,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE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AA0B,EAAE,EAAE;aACjC;SACJ,CAAC,CAAA;QAGF,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC;YAC7D,MAAc,CAAC,IAAI,GAAG,IAAI,CAAA;IACnC,CAAC;IAGD;;;;;MAKE;IACF,IAAI,CAAE,IAAW;QACb,MAAM,SAAS,GAAG,IAAI,cAAI,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,CAAA;QAC/C,KAAK,MAAM,QAAQ,IAAI,SAAS;YAC5B,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,EAAE,aAAa,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,WAAW,CAAC,CAAA;IAC3F,CAAC;IAED,MAAM;QACF,OAAO;YACH,QAAQ,EAAE,IAAI,CAAC,QAAQ;SAC1B,CAAA;IACL,CAAC;;AA7IL,oBA8IC;AA7IU,oBAAe,GAAG,oBAAoB,CAAA;AAgKjD,kBAAe,IAAI,CAAA","sourcesContent":["import qs from 'qs'\nimport Cookies from 'js-cookie'\nimport {\n default as i18next,\n type i18n as I18Next\n} from 'i18next'\nimport type { Trans } from 'react-i18next'\n\nimport {\n Dict,\n type _Dict,\n type Item,\n} from './dict.js'\n\n\nexport type Language = 'zh' | 'en' | 'ja' | 'ko'\n\nexport const LANGUAGES = ['zh', 'en', 'ja', 'ko'] as const\n\ndeclare global {\n interface Window {\n language: Language\n }\n}\n\n\n/**\n 提供翻译文本功能,在浏览器环境下自动解析当前语言、国际/国内用户\n @see https://github.com/ShenHongFei/xshell/tree/master/i18n\n*/\nexport class I18N {\n static LANGUAGE_REGEXP = /^(zh|en|ja|jp|ko)$/\n \n \n /** (ISO 639-1 标准语言代码) 可能取 zh, en, ja, ko */\n language: Language\n \n /** hostname shortcuts */\n hosts: Hosts\n \n /** url prefix shortcuts */\n roots: Roots\n \n /** 标记静态文本,以便扫描词条,并在运行时根据当前语言获取翻译 */\n t : (text: string, options?: { language?: Language, context?: string, count?: number, [key: string]: any }) => string\n \n /** render: 翻译配置字段 */\n r : (field: Item | undefined | null) => string\n \n i18next: I18Next\n \n /** react-i18next <Trans/> 组件 */\n Trans: typeof Trans = ({ children }) => children as any\n \n \n /** ```ts\n import dict from './dict.json' // { \"添加\": { \"en\": \"Add\", \"ja\": \"追加\", \"ko\": \"추가\" } }\n \n const i18n = new I18N(dict, 'zh') // 创建实例,传入词典 dict 并指定语言(NodeJS 环境),\n const i18n = new I18N(dict) // 创建实例,传入词典 dict 并自动判断当前语言(浏览器环境),\n const i18n = new I18N({ }) // 创建实例,传入空词典\n ```\n @see https://github.com/ShenHongFei/xshell/tree/master/i18n\n */\n constructor (_dict: _Dict, language?: Language) {\n const is_browser = typeof document !== 'undefined' && typeof window !== 'undefined'\n \n const dict = new Dict(_dict)\n \n // --- if in bowser then detect language & intl\n if (is_browser) {\n const { search = '' } = document.location || { }\n const queries = qs.parse(search, { ignoreQueryPrefix: true })\n \n if (!language) {\n const lquery = queries.language as string // 暂时不考虑是数组的情况\n const lwindow = window.language\n const lbrowser = typeof navigator !== 'undefined' && navigator.language.slice(0, 2)\n \n language = (lquery || lwindow || Cookies.get('language') || lbrowser || 'en') as Language\n }\n \n if (!I18N.LANGUAGE_REGEXP.test(language))\n language = 'en'\n \n // console.log(`language = ${language}`)\n }\n \n language ||= 'en'\n \n this.language = language\n \n this.t = (text, options) => {\n options = options || { }\n \n const language = options.language || this.language\n \n return this.i18next.t(text, { ...options, lng: language, defaultValue: text })\n }\n \n this.r = (field) => \n field ?\n field[this.language] || field.en || field.zh || field as any || ''\n :\n field || ''\n \n // --- init i18next\n this.i18next = i18next.createInstance()\n \n if (is_browser)\n try {\n // 在无 React 的浏览器环境下避免 react-i18next 中执行 React.createContext() 报错\n // const React = require('react') as typeof import('react')\n const { initReactI18next, Trans: I18NextTrans } = require('react-i18next') as typeof import('react-i18next')\n this.i18next.use(initReactI18next)\n const _i18next = this.i18next\n // 绑定 Trans 组件的 i18n 到 this.i18next, 解决多个 i18next 冲突的问题\n // react-i18next/context.js 中 i18n 实例只在模块级别维护,多次 this.i18next.use(initReactI18next) 会覆盖前面的 i18n,导致 Trans 无法翻译\n // https://github.com/i18next/react-i18next/issues/726\n this.Trans = function Trans ({ i18n = _i18next, ...others }) {\n // 简单转发,性能更好\n return I18NextTrans({ i18n, ...others })\n // return React.createElement(I18NextTrans, { i18n, ...others } as any, children)\n // return <I18NextTrans {...{ i18n, ...others } }>{children}</I18NextTrans>\n }\n } catch { }\n \n this.i18next.init({\n lng: this.language,\n // LOCAL\n // debug: true,\n debug: false,\n fallbackLng: {\n en: ['zh'],\n ja: ['en', 'zh'],\n ko: ['en', 'zh'],\n },\n // 禁用 : 和 . 作为 seperator\n keySeparator: false,\n nsSeparator: false,\n resources: dict.to_resources(),\n interpolation: {\n escapeValue: false\n },\n react: {\n transKeepBasicHtmlNodesFor: []\n },\n })\n \n \n if (typeof window !== 'undefined' && window && !('i18n' in window))\n (window as any).i18n = this\n }\n \n \n /** 加载词典文件 (需要将这两行单独放一个文件里,以保证在 import 其他文件之前执行) \n \n @example\n import dict from './dict.json' // { \"添加\": { \"en\": \"Add\", \"ja\": \"追加\", \"ko\": \"추가\" } }\n i18n.init(dict)\n */\n init (dict: _Dict) {\n const resources = new Dict(dict).to_resources()\n for (const language in resources)\n this.i18next.addResources(language, 'translation', resources[language].translation)\n }\n \n toJSON () {\n return {\n language: this.language,\n }\n }\n}\n\n\nexport interface Hosts {\n \n}\n\n\nexport interface Roots {\n \n}\n\nexport type { _Dict, Item }\n\nexport interface I18NBasic {\n intl: boolean\n language: Language\n}\n\nexport default I18N\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;AAAA,oDAAmB;AACnB,kEAA+B;AAC/B,8DAGgB;AAGhB,uCAIkB;AAKL,QAAA,SAAS,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAU,CAAA;AAS1D;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AAC;YACd,GAAG,EAAE,IAAI,CAAC,QAAQ;YAClB,QAAQ;YACR,eAAe;YACf,KAAK,EAAE,KAAK;YACZ,WAAW,EAAE;gBACT,EAAE,EAAE,CAAC,IAAI,CAAC;gBACV,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;gBAChB,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;aACnB;YACD,wBAAwB;YACxB,YAAY,EAAE,KAAK;YACnB,WAAW,EAAE,KAAK;YAClB,SAAS,EAAE,IAAI,CAAC,YAAY,EAAE;YAC9B,aAAa,EAAE;gBACX,WAAW,EAAE,KAAK;aACrB;YACD,KAAK,EAAE;gBACH,0BAA0B,EAAE,EAAE;aACjC;SACJ,CAAC,CAAA;QAGF,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC;YAC7D,MAAc,CAAC,IAAI,GAAG,IAAI,CAAA;IACnC,CAAC;IAGD;;;;;MAKE;IACF,IAAI,CAAE,IAAW;QACb,MAAM,SAAS,GAAG,IAAI,cAAI,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,CAAA;QAC/C,KAAK,MAAM,QAAQ,IAAI,SAAS;YAC5B,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,EAAE,aAAa,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,WAAW,CAAC,CAAA;IAC3F,CAAC;IAED,MAAM;QACF,OAAO;YACH,QAAQ,EAAE,IAAI,CAAC,QAAQ;SAC1B,CAAA;IACL,CAAC;;AA7IL,oBA8IC;AA7IU,oBAAe,GAAG,oBAAoB,CAAA;AAgKjD,kBAAe,IAAI,CAAA","sourcesContent":["import qs from 'qs'\nimport Cookies from 'js-cookie'\nimport {\n default as i18next,\n type i18n as I18Next\n} from 'i18next'\nimport type { Trans } from 'react-i18next'\n\nimport {\n Dict,\n type _Dict,\n type Item,\n} from './dict.js'\n\n\nexport type Language = 'zh' | 'en' | 'ja' | 'ko'\n\nexport const LANGUAGES = ['zh', 'en', 'ja', 'ko'] as const\n\ndeclare global {\n interface Window {\n language: Language\n }\n}\n\n\n/**\n 提供翻译文本功能,在浏览器环境下自动解析当前语言、国际/国内用户\n @see https://github.com/ShenHongFei/xshell/tree/master/i18n\n*/\nexport class I18N {\n static LANGUAGE_REGEXP = /^(zh|en|ja|jp|ko)$/\n \n \n /** (ISO 639-1 标准语言代码) 可能取 zh, en, ja, ko */\n language: Language\n \n /** hostname shortcuts */\n hosts: Hosts\n \n /** url prefix shortcuts */\n roots: Roots\n \n /** 标记静态文本,以便扫描词条,并在运行时根据当前语言获取翻译 */\n t : (text: string, options?: { language?: Language, context?: string, count?: number, [key: string]: any }) => string\n \n /** render: 翻译配置字段 */\n r : (field: Item | undefined | null, language?: Language) => string\n \n i18next: I18Next\n \n /** react-i18next <Trans/> 组件 */\n Trans: typeof Trans = ({ children }) => children as any\n \n \n /** ```ts\n import dict from './dict.json' // { \"添加\": { \"en\": \"Add\", \"ja\": \"追加\", \"ko\": \"추가\" } }\n \n const i18n = new I18N(dict, 'zh') // 创建实例,传入词典 dict 并指定语言(NodeJS 环境),\n const i18n = new I18N(dict) // 创建实例,传入词典 dict 并自动判断当前语言(浏览器环境),\n const i18n = new I18N({ }) // 创建实例,传入空词典\n ```\n @see https://github.com/ShenHongFei/xshell/tree/master/i18n\n */\n constructor (_dict: _Dict, language?: Language) {\n const is_browser = typeof document !== 'undefined' && typeof window !== 'undefined'\n \n const dict = new Dict(_dict)\n \n // --- if in bowser then detect language & intl\n if (is_browser) {\n const { search = '' } = document.location || { }\n const queries = qs.parse(search, { ignoreQueryPrefix: true })\n \n if (!language) {\n const lquery = queries.language as string // 暂时不考虑是数组的情况\n const lwindow = window.language\n const lbrowser = typeof navigator !== 'undefined' && navigator.language.slice(0, 2)\n \n language = (lquery || lwindow || Cookies.get('language') || lbrowser || 'en') as Language\n }\n \n if (!I18N.LANGUAGE_REGEXP.test(language))\n language = 'en'\n \n // console.log(`language = ${language}`)\n }\n \n language ||= 'en'\n \n this.language = language\n \n this.t = (text, options) => {\n options = options || { }\n \n const language = options.language || this.language\n \n return this.i18next.t(text, { ...options, lng: language, defaultValue: text })\n }\n \n this.r = (field, language = this.language) => \n field ?\n field[language] || field.en || field.zh || field as any || ''\n :\n field || ''\n \n // --- init i18next\n this.i18next = i18next.createInstance()\n \n if (is_browser)\n try {\n // 在无 React 的浏览器环境下避免 react-i18next 中执行 React.createContext() 报错\n // const React = require('react') as typeof import('react')\n const { initReactI18next, Trans: I18NextTrans } = require('react-i18next') as typeof import('react-i18next')\n this.i18next.use(initReactI18next)\n const _i18next = this.i18next\n // 绑定 Trans 组件的 i18n 到 this.i18next, 解决多个 i18next 冲突的问题\n // react-i18next/context.js 中 i18n 实例只在模块级别维护,多次 this.i18next.use(initReactI18next) 会覆盖前面的 i18n,导致 Trans 无法翻译\n // https://github.com/i18next/react-i18next/issues/726\n this.Trans = function Trans ({ i18n = _i18next, ...others }) {\n // 简单转发,性能更好\n return I18NextTrans({ i18n, ...others })\n // return React.createElement(I18NextTrans, { i18n, ...others } as any, children)\n // return <I18NextTrans {...{ i18n, ...others } }>{children}</I18NextTrans>\n }\n } catch { }\n \n this.i18next.init({\n lng: this.language,\n // LOCAL\n // debug: true,\n debug: false,\n fallbackLng: {\n en: ['zh'],\n ja: ['en', 'zh'],\n ko: ['en', 'zh'],\n },\n // 禁用 : 和 . 作为 seperator\n keySeparator: false,\n nsSeparator: false,\n resources: dict.to_resources(),\n interpolation: {\n escapeValue: false\n },\n react: {\n transKeepBasicHtmlNodesFor: []\n },\n })\n \n \n if (typeof window !== 'undefined' && window && !('i18n' in window))\n (window as any).i18n = this\n }\n \n \n /** 加载词典文件 (需要将这两行单独放一个文件里,以保证在 import 其他文件之前执行) \n \n @example\n import dict from './dict.json' // { \"添加\": { \"en\": \"Add\", \"ja\": \"追加\", \"ko\": \"추가\" } }\n i18n.init(dict)\n */\n init (dict: _Dict) {\n const resources = new Dict(dict).to_resources()\n for (const language in resources)\n this.i18next.addResources(language, 'translation', resources[language].translation)\n }\n \n toJSON () {\n return {\n language: this.language,\n }\n }\n}\n\n\nexport interface Hosts {\n \n}\n\n\nexport interface Roots {\n \n}\n\nexport type { _Dict, Item }\n\nexport interface I18NBasic {\n intl: boolean\n language: Language\n}\n\nexport default I18N\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell",
3
- "version": "0.0.32",
3
+ "version": "0.0.33",
4
4
  "main": "./index.js",
5
5
  "bin": {
6
6
  "xshell": "./xshell.js",
package/server.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference path="server.d.ts" />
2
1
  /// <reference types="node" />
3
2
  import { type Server as HttpServer } from 'http';
4
3
  import { default as nodefs } from 'fs';