Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow write to typescript files (typesafe-i18n support) #1071

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions src/frameworks/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>[]

keyMatchReg?: string[] | string // deprecated. use "usageMatchRegex" instead
}

Expand Down Expand Up @@ -62,6 +69,18 @@ class CustomFramework extends Framework {
return id
}

get tsFileTemplates(): Record<string, string> {
const templates = this.data?.tsFileTemplates ?? []

const result: Record<string, string> = {}
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')
Expand Down
45 changes: 36 additions & 9 deletions src/parsers/ecmascript.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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<void> {
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) {
Expand Down Expand Up @@ -58,8 +89,4 @@ export class EcmascriptParser extends Parser {
})
})
}

async save() {
Log.error(i18n.t('prompt.writing_js'))
}
}