Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#419 - Added support for components localization #479

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/internal/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { AsyncDirective, directive } from 'lit/async-directive.js';
import { noChange } from 'lit';
import { PartInfo } from 'lit/directive';

interface Language {
translations: Translations;
code: string;
default: boolean;
}

interface Translations {
[key: string]: string;
}

class Locale {
public static dateTimeFormat: Intl.DateTimeFormatOptions;
public static numberFormat: Intl.NumberFormatOptions;

private static languages = new Map<string, Language>();
private static registeredDirectives = new Set<TranslateDirective>();
private static code: string = 'en-US';

public static addLanguage(lang: Language, use: boolean = false) {
Locale.languages.set(lang.code, lang);
if (use || lang.default) Locale.setLanguage(lang.code);
}

public static setLanguage(code: string) {
Locale.code = code;
Locale.registeredDirectives.forEach(dir => dir.evaluate());
}

public static clean() {
Locale.languages = new Map<string, Language>();
}

public static connectDirective(dir: TranslateDirective) {
Locale.registeredDirectives.add(dir);
}

public static disconnectDirective(dir: TranslateDirective) {
Locale.registeredDirectives.delete(dir);
}

public static translate(keys: TemplateStringsArray, ...values: unknown[]): string {
let translations: string | undefined;
let key = Locale.buildKey(keys);
let lang = Locale.languages.get(Locale.code);
let empty = keys.join('').length === 0;

if (lang) translations = lang.translations[key];
else console.log(`missing locale: ${Locale.code}`);

if (!translations && !empty) console.log(`missing key: ${key}`);

let localizedValues = values.map(Locale.localize);

return Locale.buildMessage(translations || key, ...localizedValues);
}

private static localize(value: string | Date | Number) {
if (value instanceof Date) return value.toLocaleString(Locale.code, Locale?.dateTimeFormat);
if (value instanceof Number) return value.toLocaleString(Locale.code, Locale?.numberFormat);
return value;
}

private static buildKey(keys: TemplateStringsArray) {
let lastPartialKey = keys[keys.length - 1];
let prependPartialKey = (prev: string, curr: string, i: number) => `${curr}{${i}}${prev}`;

return keys.slice(0, -1).reduceRight(prependPartialKey, lastPartialKey);
}

private static buildMessage(content: string, ...values: unknown[]) {
return content.replace(/{(\d)}/g, (_, i: number) => values[Number(i)] as string);
}
}

class TranslateDirective extends AsyncDirective {
protected keys: TemplateStringsArray;
protected values: unknown[];

constructor(part: PartInfo) {
super(part);
Locale.connectDirective(this);
}

render(keys: TemplateStringsArray, ...values: unknown[]) {
if (this.keys && keys.length === this.keys.length && keys.every(element => this.keys.indexOf(element) !== -1)) {
// don't render
return noChange;
}

this.keys = keys;
this.values = values;

return Locale.translate(this.keys, ...this.values);
}

disconnected = () => Locale.disconnectDirective(this);

reconnected = () => Locale.connectDirective(this);

evaluate = () => this.setValue(Locale.translate(this.keys, ...this.values));
}

export { Locale };
export const t = directive(TranslateDirective);