|
1 | 1 | import { Injectable } from "@angular/core"; |
| 2 | +import { BehaviorSubject } from "rxjs"; |
| 3 | +import { map } from "rxjs/operators"; |
2 | 4 |
|
3 | 5 | const EN = require("./en.json"); |
4 | 6 |
|
| 7 | +/** |
| 8 | + * Takes the `Observable` returned from `i18n.get` and an object of variables to replace. |
| 9 | + * |
| 10 | + * The keys specify the variable name in the string. |
| 11 | + * |
| 12 | + * Example: |
| 13 | + * ```typescript |
| 14 | + * service.set({ "TEST": "{{foo}} {{bar}}" }); |
| 15 | + * |
| 16 | + * service.replace(service.get("TEST"), { foo: "test", bar: "asdf" }) |
| 17 | + * ``` |
| 18 | + * |
| 19 | + * Produces: `"test asdf"` |
| 20 | + * |
| 21 | + * @param subject the translation to replace variables on |
| 22 | + * @param variables object of variables to replace |
| 23 | + */ |
| 24 | +export const replace = (subject, variables) => subject.pipe( |
| 25 | + map<string, void>(str => { |
| 26 | + const keys = Object.keys(variables); |
| 27 | + for (const key of keys) { |
| 28 | + const value = variables[key]; |
| 29 | + while (str.includes(`{{${key}}}`)) { |
| 30 | + str = str.replace(`{{${key}}}`, value); |
| 31 | + } |
| 32 | + } |
| 33 | + return str; |
| 34 | + }) |
| 35 | +); |
| 36 | + |
| 37 | +/** |
| 38 | + * The I18n service is a minimal internal service used to supply our components with translated strings. |
| 39 | + * |
| 40 | + * All the components that support I18n also support directly passed strings. |
| 41 | + * Usage of I18n is optional, and it is not recommended for application use (libraries like ngx-translate |
| 42 | + * are a better choice) |
| 43 | + * |
| 44 | + */ |
5 | 45 | @Injectable() |
6 | 46 | export class I18n { |
7 | 47 | protected translationStrings = EN; |
8 | 48 |
|
| 49 | + protected translations = new Map(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Set/update the translations from an object. Also notifies all participating components of the update. |
| 53 | + * |
| 54 | + * @param strings an object of strings, should follow the same format as src/i18n/en.json |
| 55 | + */ |
9 | 56 | public set(strings) { |
10 | 57 | this.translationStrings = Object.assign({}, EN, strings); |
| 58 | + const translations = Array.from(this.translations); |
| 59 | + for (const [path, subject] of translations) { |
| 60 | + subject.next(this.getValueFromPath(path)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * When a path is specified returns an observable that will resolve to the translation string value. |
| 66 | + * |
| 67 | + * Returns the full translations object if path is not specified. |
| 68 | + * |
| 69 | + * @param path optional, looks like `"NOTIFICATION.CLOSE_BUTTON"` |
| 70 | + */ |
| 71 | + public get(path?) { |
| 72 | + if (!path) { |
| 73 | + return this.translationStrings; |
| 74 | + } |
| 75 | + try { |
| 76 | + // we run this here to validate the path exists before adding it to the translation map |
| 77 | + const value = this.getValueFromPath(path); |
| 78 | + if (this.translations.has(path)) { |
| 79 | + return this.translations.get(path); |
| 80 | + } |
| 81 | + const translation = new BehaviorSubject(value); |
| 82 | + this.translations.set(path, translation); |
| 83 | + return translation; |
| 84 | + } catch (err) { |
| 85 | + console.error(err); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Takes the `Observable` returned from `i18n.get` and an object of variables to replace. |
| 91 | + * |
| 92 | + * The keys specify the variable name in the string. |
| 93 | + * |
| 94 | + * Example: |
| 95 | + * ``` |
| 96 | + * service.set({ "TEST": "{{foo}} {{bar}}" }); |
| 97 | + * |
| 98 | + * service.replace(service.get("TEST"), { foo: "test", bar: "asdf" }) |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * Produces: `"test asdf"` |
| 102 | + * |
| 103 | + * @param subject the translation to replace variables on |
| 104 | + * @param variables object of variables to replace |
| 105 | + */ |
| 106 | + public replace(subject, variables) { |
| 107 | + return replace(subject, variables); |
11 | 108 | } |
12 | 109 |
|
13 | | - public get() { |
14 | | - return this.translationStrings; |
| 110 | + /** |
| 111 | + * Trys to resolve a value from the provided path. |
| 112 | + * |
| 113 | + * @param path looks like `"NOTIFICATION.CLOSE_BUTTON"` |
| 114 | + */ |
| 115 | + protected getValueFromPath(path) { |
| 116 | + let value = this.translationStrings; |
| 117 | + for (const segment of path.split(".")) { |
| 118 | + if (value[segment]) { |
| 119 | + value = value[segment]; |
| 120 | + } else { |
| 121 | + throw new Error(`no key ${segment} at ${path}`); |
| 122 | + } |
| 123 | + } |
| 124 | + return value; |
15 | 125 | } |
16 | 126 | } |
0 commit comments