Skip to content

Commit

Permalink
Add support for localization
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Nov 8, 2024
1 parent f6f17ed commit 6b559c2
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 31 deletions.
6 changes: 1 addition & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
"name": "Launch Client",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}/packages/vscode"],
"outFiles": [
"${workspaceRoot}/packages/vscode/dist/**/*.js",
"${workspaceRoot}/packages/core/dist/**/*.cjs",
"${workspaceRoot}/packages/usfm/dist/**/*.cjs"
],
"outFiles": ["${workspaceRoot}/packages/*/dist/**/*.c?js"],
"autoAttachChildProcesses": true,
"preLaunchTask": "npm: dev",
"sourceMaps": true
Expand Down
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
}
},
"scripts": {
"build": "tsup src/index.ts --dts --format cjs,esm --clean --sourcemap",
"dev": "tsup src/index.ts --dts --format cjs,esm --watch --sourcemap",
"build": "tsup-node src/index.ts --dts --format cjs,esm --clean --sourcemap",
"dev": "tsup-node src/index.ts --dts --format cjs,esm --watch --sourcemap",
"check-types": "tsc --noEmit",
"lint": "eslint .",
"test": "vitest",
Expand All @@ -30,6 +30,7 @@
"author": "SIL Global",
"license": "MIT",
"dependencies": {
"i18next": "^23.16.5",
"rxjs": "^7.8.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/diagnostic/diagnostic-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface DiagnosticsChanged {
export interface DiagnosticProvider {
readonly id: string;
readonly diagnosticsChanged$: Observable<DiagnosticsChanged>;
init(): Promise<void>;
getDiagnostics(uri: string): Promise<Diagnostic[]>;
getDiagnosticFixes(uri: string, diagnostic: Diagnostic): Promise<DiagnosticFix[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export interface OnTypeFormattingProvider {

readonly onTypeTriggerCharacters: ReadonlySet<string>;

init(): Promise<void>;
getOnTypeEdits(uri: string, position: Position, ch: string): Promise<TextEdit[] | undefined>;
}
1 change: 1 addition & 0 deletions packages/core/src/workspace/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Localizer } from './localizer';
export type { WorkspaceConfig } from './workspace';
export { Workspace } from './workspace';
69 changes: 69 additions & 0 deletions packages/core/src/workspace/localizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import i18next, { i18n } from 'i18next';

export class Localizer {
private readonly i18n: i18n;
private readonly backend: LocalBackend;

constructor(language?: string) {
this.backend = new LocalBackend();
this.i18n = i18next
.createInstance({
lng: language,
fallbackLng: 'en',
partialBundledLanguages: true,
ns: [],
resources: {},
})
.use(this.backend);
}

get language(): string {
return this.i18n.language;
}

async init(): Promise<void> {
await this.i18n.init();
}

async addNamespace(namespace: string, loader: (language: string) => Promise<unknown>): Promise<void> {
if (this.i18n.hasLoadedNamespace(namespace)) {
return;
}
this.backend.addNamespace(namespace, loader);
await this.i18n.loadNamespaces(namespace);
}

async changeLanguage(language: string): Promise<void> {
await this.i18n.changeLanguage(language);
}

t(key: string, options?: Record<string, string>): string {
return this.i18n.t(key, options);
}
}

class LocalBackend {
private readonly namespaceLoaders = new Map<string, (language: string) => Promise<unknown>>();
readonly type = 'backend';

init(_services: unknown, _backendOptions: unknown, _i18nextOptions: unknown): void {
// do nothing
}

addNamespace(namespace: string, loader: (language: string) => Promise<unknown>): void {
this.namespaceLoaders.set(namespace, loader);
}

read(language: string, namespace: string, callback: (err: unknown, data?: unknown) => void): void {
const loader = this.namespaceLoaders.get(namespace);
if (loader == null) {
callback(new Error(`No loader for namespace ${namespace}`));
return;
}
loader(language)
.then((data) => {
callback(null, data);
})
.catch(callback);
}
}
14 changes: 14 additions & 0 deletions packages/core/src/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import { Diagnostic } from '../diagnostic/diagnostic';
import { DiagnosticFix } from '../diagnostic/diagnostic-fix';
import { DiagnosticProvider, DiagnosticsChanged } from '../diagnostic/diagnostic-provider';
import { OnTypeFormattingProvider } from '../formatting/on-type-formatting-provider';
import { Localizer } from './localizer';

export interface WorkspaceConfig {
localizer: Localizer;
diagnosticProviders?: DiagnosticProvider[];
onTypeFormattingProviders?: OnTypeFormattingProvider[];
}

export class Workspace {
private readonly localizer: Localizer;
private readonly diagnosticProviders: Map<string, DiagnosticProvider>;
private readonly onTypeFormattingProviders: Map<string, OnTypeFormattingProvider>;
private readonly lastDiagnosticChangedEvents = new Map<string, DiagnosticsChanged[]>();

public readonly diagnosticsChanged$: Observable<DiagnosticsChanged>;

constructor(config: WorkspaceConfig) {
this.localizer = config.localizer;
this.diagnosticProviders = new Map(config.diagnosticProviders?.map((provider) => [provider.id, provider]));
this.diagnosticsChanged$ = merge(
...Array.from(this.diagnosticProviders.values()).map((provider, i) =>
Expand All @@ -35,6 +39,16 @@ export class Workspace {
);
}

async init(): Promise<void> {
await this.localizer.init();
await Promise.all(Array.from(this.diagnosticProviders.values()).map((provider) => provider.init()));
await Promise.all(Array.from(this.onTypeFormattingProviders.values()).map((provider) => provider.init()));
}

changeLanguage(language: string): Promise<void> {
return this.localizer.changeLanguage(language);
}

async getDiagnostics(uri: string): Promise<Diagnostic[]> {
const diagnostics: Diagnostic[] = [];
for (const provider of this.diagnosticProviders.values()) {
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/library.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default tseslint.config(
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
},
},
{
Expand Down
12 changes: 12 additions & 0 deletions packages/examples/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import library from '@repo/eslint-config/library.mjs';

export default [
{
languageOptions: {
parserOptions: {
project: './tsconfig.json',
},
},
},
...library,
];
44 changes: 44 additions & 0 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@sillsdev/lynx-examples",
"version": "1.0.0",
"description": "",
"type": "module",
"types": "./dist/index.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"scripts": {
"build": "tsup-node src/index.ts --dts --format cjs,esm --clean --sourcemap",
"dev": "tsup-node src/index.ts --dts --format cjs,esm --watch --sourcemap",
"check-types": "tsc --noEmit",
"lint": "eslint .",
"test": "vitest",
"coverage": "vitest run --coverage"
},
"keywords": [],
"author": "SIL Global",
"license": "MIT",
"dependencies": {
"@sillsdev/lynx": "*"
},
"devDependencies": {
"@repo/eslint-config": "*",
"@repo/typescript-config": "*",
"@vitest/coverage-v8": "^2.1.4",
"eslint": "^9.9.1",
"tsup": "^8.3.0",
"typescript": "^5.5.4",
"vitest": "^2.1.4"
}
}
2 changes: 2 additions & 0 deletions packages/examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { SimpleQuoteFormattingProvider } from './simple-quote-formatting-provider';
export { VerseOrderDiagnosticProvider } from './verse-order-diagnostic-provider';
9 changes: 9 additions & 0 deletions packages/examples/src/locales/en/verse-order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"missingVerse": {
"description": "Verse {{verse}} is missing from chapter {{chapter}}. Insert the missing verse to fix.",
"fixTitle": "Insert missing verse"
},
"verseOutOfOrder": {
"description": "Verse {{verse}} occurs out of order in chapter {{chapter}}."
}
}
9 changes: 9 additions & 0 deletions packages/examples/src/locales/es/verse-order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"missingVerse": {
"description": "Falta el versículo {{verse}} del capítulo {{chapter}}. Inserta el versículo que falta para arreglarlo.",
"fixTitle": "Inserta el versículo que falta"
},
"verseOutOfOrder": {
"description": "El versículo {{verse}} aparece fuera de orden en el capítulo {{chapter}}."
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { DocumentManager, OnTypeFormattingProvider, Position, TextDocument, TextEdit } from '@sillsdev/lynx';

export class SmartQuoteFormattingProvider implements OnTypeFormattingProvider {
readonly id = 'smart-quote';
export class SimpleQuoteFormattingProvider implements OnTypeFormattingProvider {
readonly id = 'simple-quote';
readonly onTypeTriggerCharacters: ReadonlySet<string> = new Set(['"', '“', '”']);

constructor(private readonly documentManager: DocumentManager<TextDocument>) {}

init(): Promise<void> {
return Promise.resolve();
}

async getOnTypeEdits(uri: string, _position: Position, _ch: string): Promise<TextEdit[] | undefined> {
const doc = await this.documentManager.get(uri);
if (doc == null) {
Expand Down
Loading

0 comments on commit 6b559c2

Please sign in to comment.