-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathTranslatable.njs
More file actions
38 lines (33 loc) · 968 Bytes
/
Translatable.njs
File metadata and controls
38 lines (33 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { existsSync, readFileSync } from "fs";
import Nullstack from "nullstack";
import YAML from "yaml";
class Translatable extends Nullstack {
static async geti18nByLocale({ locale }) {
const [name] = this.name.split("_");
const translationPath = `i18n/${locale}/components/${name}.yml`;
if (existsSync(translationPath)) {
const file = readFileSync(translationPath, "utf-8");
return YAML.parse(file);
}
return {};
}
async initiate({ page }) {
this.locale = page.locale;
this.i18n = await this.geti18nByLocale({ locale: page.locale });
if (this.i18n.title) {
page.description = this.i18n.description;
page.locale = page.locale || "en-US";
}
}
launch({ project, page }) {
if (this.i18n.title) {
page.title = `${this.i18n.title} - ${project.name}`;
}
}
update({ page }) {
if (this.locale !== page.locale) {
this.initiate();
}
}
}
export default Translatable;