Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Jan 21, 2025
1 parent 84fd2e6 commit 895a707
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 288 deletions.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 5 additions & 2 deletions examples/namespace/languine.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ export default defineConfig({
targets: ["es", "fr"],
},
files: {
json: {
include: ["locales/[locale]/*.json"],
// json: {
// include: ["locales/[locale]/*.json"],
// },
md: {
include: ["docs/[locale]/*.md"],
},
},
});
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"jiti": "^2.4.2",
"jsdom": "^26.0.0",
"jsonrepair": "^3.11.2",
"marked": "^15.0.6",
"node-html-parser": "^7.0.1",
"open": "^10.1.0",
"plist": "^3.1.0",
Expand All @@ -55,6 +54,7 @@
},
"devDependencies": {
"@types/gettext-parser": "^4.0.4",
"@types/mdast": "^4.0.4",
"@types/node": "^22.10.7",
"@types/plist": "^3.0.5",
"tsup": "^8.3.5",
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/commands/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ export async function translateCommand(args: string[] = []) {

// Read existing target file if it exists
let existingContent: Record<string, string> = {};
let originalFileContent: string | undefined;
try {
const existingFile = await readFile(targetPath, "utf-8");
originalFileContent = existingFile;
existingContent = await parser.parse(existingFile);
} catch (error) {
// File doesn't exist yet, use empty object
Expand All @@ -327,10 +329,11 @@ export async function translateCommand(args: string[] = []) {
...translatedContent,
};

// Pass the original file content as a string if it exists
const serialized = await parser.serialize(
targetLocale,
mergedContent,
existingContent,
originalFileContent,
);

// Run afterTranslate hook if configured
Expand Down
99 changes: 0 additions & 99 deletions packages/cli/src/parsers/__tests__/markdown.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/src/parsers/core/base-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export abstract class BaseParser implements Parser {
abstract serialize(
locale: string,
data: Record<string, string>,
originalData?: Record<string, string>,
originalData?: string | Record<string, unknown>,
): Promise<string>;
}
2 changes: 1 addition & 1 deletion packages/cli/src/parsers/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export interface Parser {
serialize(
locale: string,
data: Record<string, string>,
originalData?: Record<string, unknown>,
originalData?: string | Record<string, unknown>,
): Promise<string>;
}
6 changes: 1 addition & 5 deletions packages/cli/src/parsers/formats/android.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Builder, parseStringPromise } from "xml2js";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

interface StringItem {
$: { name: string };
Expand Down Expand Up @@ -172,7 +172,3 @@ export class AndroidParser extends BaseParser {
}
}
}

export function createAndroidParser(options: ParserOptions): AndroidParser {
return new AndroidParser(options);
}
6 changes: 1 addition & 5 deletions packages/cli/src/parsers/formats/arb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { merge, pickBy } from "rambda";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

export class ArbParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
Expand Down Expand Up @@ -28,7 +28,3 @@ export class ArbParser extends BaseParser {
}
}
}

export function createArbParser(options: ParserOptions): ArbParser {
return new ArbParser(options);
}
6 changes: 1 addition & 5 deletions packages/cli/src/parsers/formats/csv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from "csv-parse/sync";
import { stringify } from "csv-stringify/sync";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

interface CsvRow extends Record<string, string> {
id: string;
Expand Down Expand Up @@ -136,7 +136,3 @@ export class CsvParser extends BaseParser {
}
}
}

export function createCsvParser(options: ParserOptions): CsvParser {
return new CsvParser(options);
}
6 changes: 1 addition & 5 deletions packages/cli/src/parsers/formats/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDOM } from "jsdom";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

// Import Node and HTMLElement constants from jsdom
const { Node, HTMLElement } = new JSDOM().window;
Expand Down Expand Up @@ -186,7 +186,3 @@ export class HtmlParser extends BaseParser {
}
}
}

export function createHtmlParser(options: ParserOptions): HtmlParser {
return new HtmlParser(options);
}
9 changes: 1 addition & 8 deletions packages/cli/src/parsers/formats/javascript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { unflatten } from "../core/flatten.js";
import { BaseParser } from "../core/base-parser.js";

export class JavaScriptParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
Expand Down Expand Up @@ -318,9 +317,3 @@ export class JavaScriptParser extends BaseParser {
return result;
}
}

export function createJavaScriptParser(
options: ParserOptions,
): JavaScriptParser {
return new JavaScriptParser(options);
}
6 changes: 1 addition & 5 deletions packages/cli/src/parsers/formats/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { jsonrepair } from "jsonrepair";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";
import { flatten, unflatten } from "../core/flatten.js";

export class JsonParser extends BaseParser {
Expand All @@ -25,7 +25,3 @@ export class JsonParser extends BaseParser {
return `${JSON.stringify(unflatten(data), null, 2)}\n`;
}
}

export function createJsonParser(options: ParserOptions): JsonParser {
return new JsonParser(options);
}
63 changes: 5 additions & 58 deletions packages/cli/src/parsers/formats/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,16 @@
import { marked } from "marked";
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.ts";
import type { ParserOptions } from "../core/types.ts";

export class MarkdownParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
try {
const tokens = marked.lexer(input);
const result: Record<string, string> = {};

// Extract text content from markdown tokens
for (const token of tokens) {
if (token.type === "heading") {
const key = `heading.${token.depth}`;
result[key] = token.text;
} else if (token.type === "paragraph") {
result[`paragraph.${Object.keys(result).length}`] = token.text;
} else if (token.type === "list") {
token.items.forEach((item: { text: string }, index: number) => {
result[`list.${index}`] = item.text;
});
}
}

return result;
} catch (error) {
throw new Error(
`Failed to parse Markdown translations: ${error instanceof Error ? error.message : String(error)}`,
);
}
return {};
}

async serialize(
_locale: string,
data: Record<string, string>,
_originalData?: Record<string, string>,
originalData?: string | Record<string, unknown>,
): Promise<string> {
try {
// Validate input data
for (const [key, value] of Object.entries(data)) {
if (value === undefined) {
throw new Error(`Value for key "${key}" is undefined`);
}
}

const markdownParts: string[] = [];

for (const [key, value] of Object.entries(data)) {
if (key.startsWith("heading.")) {
const depth = Number.parseInt(key.split(".")[1], 10);
markdownParts.push(`${"#".repeat(depth)} ${value}\n`);
} else if (key.startsWith("paragraph.")) {
markdownParts.push(`${value}\n\n`);
} else if (key.startsWith("list.")) {
markdownParts.push(`- ${value}\n`);
}
}

return markdownParts.join("");
} catch (error) {
throw new Error(
`Failed to serialize Markdown translations: ${error instanceof Error ? error.message : String(error)}`,
);
}
return "";
}
}

export function createMarkdownParser(options: ParserOptions): MarkdownParser {
return new MarkdownParser(options);
}
8 changes: 2 additions & 6 deletions packages/cli/src/parsers/formats/po.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

class PoParser extends BaseParser {
export class PoParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
try {
const result: Record<string, string> = {};
Expand Down Expand Up @@ -63,10 +63,6 @@ class PoParser extends BaseParser {
}
}

export function createPoParser(options: ParserOptions): PoParser {
return new PoParser(options);
}

function isSkippableLine(line: string): boolean {
return !line || line.startsWith("#");
}
Expand Down
8 changes: 1 addition & 7 deletions packages/cli/src/parsers/formats/properties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

export class PropertiesParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
Expand Down Expand Up @@ -45,9 +45,3 @@ export class PropertiesParser extends BaseParser {
}
}
}

export function createPropertiesParser(
options: ParserOptions,
): PropertiesParser {
return new PropertiesParser(options);
}
8 changes: 1 addition & 7 deletions packages/cli/src/parsers/formats/xcode-strings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseParser, type ParserOptions } from "../core/base-parser.js";
import { BaseParser } from "../core/base-parser.js";

export class XcodeStringsParser extends BaseParser {
async parse(input: string): Promise<Record<string, string>> {
Expand Down Expand Up @@ -46,12 +46,6 @@ export class XcodeStringsParser extends BaseParser {
}
}

export function createXcodeStringsParser(
options: ParserOptions,
): XcodeStringsParser {
return new XcodeStringsParser(options);
}

function unescapeXcodeString(str: string): string {
return str.replace(/\\"/g, '"').replace(/\\n/g, "\n").replace(/\\\\/g, "\\");
}
Expand Down
Loading

0 comments on commit 895a707

Please sign in to comment.