-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow splitting translations (#52)
* feat: add translation splitting I guess you could say this is a "prototype" * feat: add documentation * fix: update files * feat: add async functions * feat: better rewrite * fix: small changes * fix: fix for tests * fix: add missing properties * fix: add comma * fix: small changes * fix: small changes Changed `NestedTranslation` from `type` to `interface`. * fix: simplify sentence * test: add support for split translations * fix: rephrase example
- Loading branch information
Showing
17 changed files
with
181 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cart = { $first_name }, у вас { | ||
$apples -> | ||
[0] нет яблок | ||
[one] одно яблоко | ||
*[other] { $apples } яблок | ||
} в корзине. | ||
checkout = Спасибо за покупку! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
greeting = Привет { $first_name }! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language-set = Язык был изменен на Русский! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
multiline = | ||
Это пример | ||
многострочных | ||
сообщений | ||
чтобы увидеть, как они отформатированы! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ export { | |
type MiddlewareFn, | ||
} from "https://lib.deno.dev/x/[email protected]/mod.ts"; | ||
|
||
export { extname, resolve } from "https://deno.land/[email protected]/path/mod.ts"; | ||
export { extname, join, SEP } from "https://deno.land/[email protected]/path/mod.ts"; | ||
|
||
export { walk, walkSync } from "https://deno.land/[email protected]/fs/walk.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,81 @@ | ||
import { extname } from "./deps.ts"; | ||
|
||
export async function readLocalesDir(path: string): Promise<string[]> { | ||
const files = new Array<string>(); | ||
for await (const entry of Deno.readDir(path)) { | ||
if (!entry.isFile) continue; | ||
const extension = extname(entry.name); | ||
if (extension !== ".ftl") continue; | ||
files.push(entry.name); | ||
import { extname, join, SEP, walk, walkSync } from "./deps.ts"; | ||
import { NestedTranslation } from "./types.ts"; | ||
|
||
function throwReadFileError(path: string) { | ||
throw new Error( | ||
`Something went wrong while reading the "${path}" file, usually, this can be caused by the file being empty. \ | ||
If it is, please add at least one translation key to this file (or simply just delete it) to solve this error.`, | ||
); | ||
} | ||
|
||
export async function readLocalesDir( | ||
path: string, | ||
): Promise<NestedTranslation[]> { | ||
const files = new Array<NestedTranslation>(); | ||
const locales = new Set<string>(); | ||
|
||
for await (const entry of walk(path)) { | ||
if (entry.isFile && extname(entry.name) === ".ftl") { | ||
try { | ||
const decoder = new TextDecoder("utf-8"); | ||
const excludeRoot = entry.path.replace(path, ""); | ||
const contents = await Deno.readFile(join(path, excludeRoot)); | ||
|
||
const belongsTo = excludeRoot.split(SEP)[1].split(".")[0]; | ||
const translationSource = decoder.decode(contents); | ||
|
||
files.push({ | ||
belongsTo, | ||
translationSource, | ||
}); | ||
locales.add(belongsTo); | ||
} catch { | ||
throwReadFileError(entry.path); | ||
} | ||
} | ||
} | ||
return files; | ||
|
||
return Array.from(locales).map((locale) => { | ||
const sameLocale = files.filter((file) => file.belongsTo === locale); | ||
const sourceOnly = sameLocale.map((file) => file.translationSource); | ||
return { | ||
belongsTo: locale, | ||
translationSource: sourceOnly.join("\n"), | ||
}; | ||
}); | ||
} | ||
|
||
export function readLocalesDirSync(path: string): string[] { | ||
const files = new Array<string>(); | ||
for (const entry of Deno.readDirSync(path)) { | ||
if (!entry.isFile) continue; | ||
const extension = extname(entry.name); | ||
if (extension !== ".ftl") continue; | ||
files.push(entry.name); | ||
export function readLocalesDirSync(path: string): NestedTranslation[] { | ||
const files = new Array<NestedTranslation>(); | ||
const locales = new Set<string>(); | ||
|
||
for (const entry of walkSync(path)) { | ||
if (entry.isFile && extname(entry.name) === ".ftl") { | ||
try { | ||
const decoder = new TextDecoder("utf-8"); | ||
const excludeRoot = entry.path.replace(path, ""); | ||
const contents = Deno.readFileSync(join(path, excludeRoot)); | ||
|
||
const belongsTo = excludeRoot.split(SEP)[1].split(".")[0]; | ||
const translationSource = decoder.decode(contents); | ||
|
||
files.push({ | ||
belongsTo, | ||
translationSource, | ||
}); | ||
locales.add(belongsTo); | ||
} catch { | ||
throwReadFileError(entry.path); | ||
} | ||
} | ||
} | ||
return files; | ||
|
||
return Array.from(locales).map((locale) => { | ||
const sameLocale = files.filter((file) => file.belongsTo === locale); | ||
const sourceOnly = sameLocale.map((file) => file.translationSource); | ||
return { | ||
belongsTo: locale, | ||
translationSource: sourceOnly.join("\n"), | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters