Skip to content

Commit

Permalink
Modify the !a.String to no need to be filtered in template.
Browse files Browse the repository at this point in the history
Localized the document fields when loading fields for a document.
Uses the document's locale to get the correct localized value when `toString` is called.

Part of #5
  • Loading branch information
Zoramite committed Dec 17, 2020
1 parent d8ada40 commit a96f7cb
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 73 deletions.
6 changes: 4 additions & 2 deletions example/content/pages/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ title: All routes
description: Page description
partials:
- partial: hero
title: Routes
body: Browse all the routes below.
title: !a.String
value: Routes
body: !a.String
value: Browse all the routes below.
- partial: spacer
options:
- large
Expand Down
4 changes: 2 additions & 2 deletions example/views/partials/hero.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<div class="hero {{partial.class}}">
<div class="hero__content">
<div class="hero__title">
{{partial.title|t}}
{{partial.title}}
</div>
<div class="hero__body">
{{partial.body|t}}
{{partial.body}}
</div>
{% if partial.buttons %}
<div class="hero__buttons">
Expand Down
2 changes: 1 addition & 1 deletion example/views/partials/routes.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<tr>
<td>{{item.locale.id}}</td>
<td><a href="{{item.url.path}}">{{item.url.path}}</a></td>
<td>{{item.doc.fields.title|t}}</td>
<td>{{item.doc.fields.title}}</td>
</tr>
{% endfor %}
</table>
Expand Down
59 changes: 36 additions & 23 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import * as utils from './utils';
const DEFAULT_RENDERER = 'njk';
const DEFAULT_VIEW = '/views/base.njk';

interface DocumentParts {
body?: string | null;
fields?: any | null; // TODO: See if we can limit this.
}

export class Document {
path: string;
pod: Pod;
renderer: Renderer;
locale: Locale;
readonly ext: string;
private _fields: any; // TODO: See if we can limit this.
private _body: string | null;
private _parts: DocumentParts;
private _content: string | null;
static SupportedExtensions = new Set(['.md', '.yaml']);

Expand All @@ -26,8 +30,10 @@ export class Document {
this.locale = locale;
this.ext = fsPath.extname(this.path);

this._body = null;
this._fields = null;
this._parts = {
body: null,
fields: null,
};
this._content = null;
}

Expand Down Expand Up @@ -120,15 +126,18 @@ export class Document {
}

get fields() {
if (this._fields) {
return this._fields;
if (this._parts.fields) {
return this._parts.fields;
}
if (this.ext === '.md') {
this.initPartsFromFrontMatter(); // Set this._fields.
this._parts = this.initPartsFromFrontMatter();
} else {
this._fields = this.pod.readYaml(this.path);
this._parts.fields = utils.localizeData(
this.pod.readYaml(this.path),
this.locale
);
}
return this._fields;
return this._parts.fields;
}

get content() {
Expand All @@ -140,28 +149,32 @@ export class Document {
}

get body() {
if (this._body !== null) {
return this._body;
if (this._parts.body !== null) {
return this._parts.body;
}
if (this.ext === '.yaml') {
this._body = '';
this._parts.body = '';
} else if (this.ext === '.md') {
this.initPartsFromFrontMatter(); // Set this._body.
this._parts = this.initPartsFromFrontMatter();
}
return this._body;
return this._parts.body;
}

private initPartsFromFrontMatter() {
private initPartsFromFrontMatter(): DocumentParts {
// If the body value is not null, assume the front matter has been split.
if (this._body !== null) {
return;
if (this._parts.body !== null) {
return this._parts;
}
const result = utils.splitFrontMatter(this.content);
this._body = result.body;
if (result.frontMatter === null) {
this._fields = {};
} else {
this._fields = this.pod.readYamlString(result.frontMatter, this.path);
}
return {
body: result.body || null,
fields:
result.frontMatter === null
? {}
: utils.localizeData(
this.pod.readYamlString(result.frontMatter, this.path),
this.locale
),
};
}
}
23 changes: 14 additions & 9 deletions src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export class Locale {

toTranslationString(value: string | TranslationString) {
if (typeof value === 'string') {
return this.pod.string({
value: value as string,
});
return this.pod.string(
{
value: value as string,
},
this
);
}
return value;
}
Expand All @@ -53,10 +56,12 @@ export class Locale {
}

const string = this.toTranslationString(value);

this.recordString(string, location);
if (!this.pod.fileExists(this.podPath) || !this.translations) {
this.recordString(string, location);
return value;
return string.value;
}

// Check for the translation of the preferred value and return it. This
// permits specification of a "preferred" string, for instances where some
// locales may have translations and others may not. Usually, this is useful
Expand All @@ -67,16 +72,16 @@ export class Locale {
if (preferredValue) {
return preferredValue;
}
// Collect the string because the preferred translation is missing.
this.recordString(string, location);
}

// Collect the string because the preferred translation is missing.
const foundValue = this.translations[string.value];
if (foundValue) {
return foundValue;
}
this.recordString(string, location);

// No translation was found at all, fall back to the source string.
return value;
return string.value;
}

get isRtl() {
Expand Down
9 changes: 7 additions & 2 deletions src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ export class Pod {
return this.cache.collections[path];
}

string(options: StringOptions) {
string(options: StringOptions, locale?: Locale) {
locale = locale || this.defaultLocale;
if (this.cache.strings[options.value]) {
return this.cache.strings[options.value];
}
this.cache.strings[options.value] = new TranslationString(this, options);
this.cache.strings[options.value] = new TranslationString(
this,
options,
locale
);
return this.cache.strings[options.value];
}

Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class DocumentRoute extends Route {
try {
return await this.doc.render();
} catch (err) {
console.log(`Error buildng: ${this.doc}`);
console.error(`Error buildng: ${this.doc}`);
throw err;
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Locale} from './locale';
import {Pod} from './pod';

export interface StringOptions {
Expand All @@ -9,17 +10,33 @@ export interface StringOptions {
// the URLs of the pages where the string is used, forcing a newer version of a
// string to use for multi-level translation selection, etc.
export class TranslationString {
locale?: Locale;
pod: Pod;
value: string;
prefer?: string;
value: string;

constructor(pod: Pod, options: StringOptions) {
constructor(pod: Pod, options: StringOptions, locale?: Locale) {
this.pod = pod;
this.value = options.value;
this.prefer = options.prefer;
this.value = options.value;
this.locale = locale;
}

toLocale(locale: Locale) {
return new TranslationString(
this.pod,
{
prefer: this.prefer,
value: this.value,
},
locale
);
}

toString() {
if (this.locale) {
return this.locale.getTranslation(this);
}
return this.value;
}
}
Loading

0 comments on commit a96f7cb

Please sign in to comment.