diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ef31a34..3c4859c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.12.1](https://github.com/lokalise/i18n-ally/compare/v2.12.0...v2.12.1) (2024-01-08) + ## [2.12.0](https://github.com/lokalise/i18n-ally/compare/v2.11.1...v2.12.0) (2023-09-23) ### ⚡ Features diff --git a/package.json b/package.json index b51218fe..a099df7a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "publisher": "lokalise", "name": "i18n-ally", - "version": "2.12.0", + "version": "2.12.1", "displayName": "i18n Ally", "description": "🌍 All in one i18n extension for VS Code", "keywords": [ @@ -21,10 +21,10 @@ "release:minor": "standard-version --release-as minor && git push --follow-tags", "release:patch": "standard-version --release-as patch && git push --follow-tags", "gh:release": "conventional-github-releaser -p angular", - "build": "rimraf dist && nr i18n:build && nr main:build && nr editor:build && esno scripts/post-build.ts", + "build": "export NODE_OPTIONS=--openssl-legacy-provider && rimraf dist && nr i18n:build && nr main:build && nr editor:build && esno scripts/post-build.ts", "dev": "run-p *:dev", - "main:build": "cross-env I18N_ALLY_ENV=production webpack --mode development", - "main:dev": "cross-env I18N_ALLY_ENV=development webpack --mode development --watch --info-verbosity verbose", + "main:build": "export NODE_OPTIONS=--openssl-legacy-provider && cross-env I18N_ALLY_ENV=production webpack --mode development", + "main:dev": "export NODE_OPTIONS=--openssl-legacy-provider && cross-env I18N_ALLY_ENV=development webpack --mode development --watch --info-verbosity verbose", "editor:build": "parcel build src/webview/src/index.html --out-dir dist/editor --no-minify", "editor:dev": "nodemon -w src/webview/src -e js,ts,vue,html --exec \"nr editor:build\"", "i18n:build": "esno scripts/build-i18n.ts", diff --git a/src/frameworks/custom.ts b/src/frameworks/custom.ts index 5fc8af19..b73a2fd9 100644 --- a/src/frameworks/custom.ts +++ b/src/frameworks/custom.ts @@ -15,6 +15,13 @@ interface CustomFrameworkConfig { refactorTemplates?: string[] monopoly?: boolean + /** + * tsFileTemplates: + * - 'en/index.ts': "import type { BaseTranslation } from '../i18n-types'\n\nconst en = $1 satisfies BaseTranslation\n\nexport default en\n" + * - 'default': "import type { Translation } from '../i18n-types'\n\nexport default $1 satisfies Translation\n" + */ + tsFileTemplates: Record[] + keyMatchReg?: string[] | string // deprecated. use "usageMatchRegex" instead } @@ -62,6 +69,18 @@ class CustomFramework extends Framework { return id } + get tsFileTemplates(): Record { + const templates = this.data?.tsFileTemplates ?? [] + + const result: Record = {} + for (const teamplate of templates) { + const [key, value] = Object.entries(teamplate)[0] + result[key] = value + } + + return result + } + get usageMatchRegex(): string[] { let id = this.data?.usageMatchRegex ?? this.data?.keyMatchReg ?? [] if (typeof id === 'string') diff --git a/src/parsers/ecmascript.ts b/src/parsers/ecmascript.ts index 701c0947..bcff72e9 100644 --- a/src/parsers/ecmascript.ts +++ b/src/parsers/ecmascript.ts @@ -1,9 +1,10 @@ import child_process from 'child_process' import path from 'path' import { Parser } from './base' -import i18n from '~/i18n' -import { Log } from '~/utils' import { Config, Global } from '~/core' +import { getFramework } from '~/frameworks' +import CustomFramework from '~/frameworks/custom' +import { File, Log } from '~/utils' const LanguageIds = { js: 'javascript', @@ -16,18 +17,48 @@ const LanguageExts = { } as const export class EcmascriptParser extends Parser { - readonly readonly = true + readonly readonly = false + + private framework: CustomFramework constructor(public readonly id: 'js'|'ts' = 'js') { super([LanguageIds[id]], LanguageExts[id]) + this.framework = getFramework('custom') as CustomFramework } async parse() { return {} } - async dump() { - return '' + async dump(object: object) { + return JSON.stringify(object, null, 2) + } + + async save(filepath: string, object: object): Promise { + const templates = this.framework.tsFileTemplates + + let selectedTemplate: string | undefined + for (const [key, template] of Object.entries(templates)) { + if (filepath.endsWith(key)) { + selectedTemplate = template + break + } + } + + if (!selectedTemplate) { + if (templates.default) + selectedTemplate = templates.default + } + + if (!selectedTemplate) { + Log.error(`[i18n-ally] no template found for ${filepath}`) + return + } + + const localization = await this.dump(object) + const text = selectedTemplate.replace('$1', localization) + + await File.write(filepath, text) } async load(filepath: string) { @@ -58,8 +89,4 @@ export class EcmascriptParser extends Parser { }) }) } - - async save() { - Log.error(i18n.t('prompt.writing_js')) - } }