|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import { EventDispatcher, EventHandler } from './EventDispatcher'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper class for Localization |
| 12 | + * |
| 13 | + * |
| 14 | + * @export |
| 15 | + * @class LocalizationHelper |
| 16 | + */ |
| 17 | +export class LocalizationHelper { |
| 18 | + static _strings: any; |
| 19 | + |
| 20 | + static _stringsEventDispatcher: EventDispatcher<any> = new EventDispatcher(); |
| 21 | + |
| 22 | + static _directionEventDispatcher: EventDispatcher<any> = new EventDispatcher(); |
| 23 | + |
| 24 | + private static mutationObserver; |
| 25 | + |
| 26 | + public static get strings() { |
| 27 | + return this._strings; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Set strings to be localized |
| 32 | + * |
| 33 | + * @static |
| 34 | + * @memberof LocalizationHelper |
| 35 | + */ |
| 36 | + public static set strings(value: any) { |
| 37 | + this._strings = value; |
| 38 | + this._stringsEventDispatcher.fire(null); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * returns body dir attribute to determine rtl or ltr |
| 43 | + * |
| 44 | + * @static |
| 45 | + * @returns {string} dir |
| 46 | + * @memberof LocalizationHelper |
| 47 | + */ |
| 48 | + public static getDocumentDirection() { |
| 49 | + return document.body.getAttribute('dir') || document.documentElement.getAttribute('dir'); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Fires event when LocalizationHelper changes state |
| 54 | + * |
| 55 | + * @static |
| 56 | + * @param {EventHandler<ProvidersChangedState>} event |
| 57 | + * @memberof LocalizationHelper |
| 58 | + */ |
| 59 | + public static onStringsUpdated(event: EventHandler<any>) { |
| 60 | + this._stringsEventDispatcher.add(event); |
| 61 | + } |
| 62 | + |
| 63 | + public static removeOnStringsUpdated(event: EventHandler<any>) { |
| 64 | + this._stringsEventDispatcher.remove(event); |
| 65 | + } |
| 66 | + |
| 67 | + public static onDirectionUpdated(event: EventHandler<any>) { |
| 68 | + this._directionEventDispatcher.add(event); |
| 69 | + this.initDirection(); |
| 70 | + } |
| 71 | + |
| 72 | + public static removeOnDirectionUpdated(event: EventHandler<any>) { |
| 73 | + this._directionEventDispatcher.remove(event); |
| 74 | + } |
| 75 | + |
| 76 | + private static _isDirectionInit = false; |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks for direction setup and adds mutationObserver |
| 80 | + * |
| 81 | + * @private |
| 82 | + * @static |
| 83 | + * @returns |
| 84 | + * @memberof LocalizationHelper |
| 85 | + */ |
| 86 | + private static initDirection() { |
| 87 | + if (this._isDirectionInit) { |
| 88 | + return; |
| 89 | + } |
| 90 | + this._isDirectionInit = true; |
| 91 | + this.mutationObserver = new MutationObserver(mutations => { |
| 92 | + mutations.forEach(mutation => { |
| 93 | + if (mutation.attributeName == 'dir') { |
| 94 | + this._directionEventDispatcher.fire(null); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + const options = { attributes: true, attributeFilter: ['dir'] }; |
| 99 | + this.mutationObserver.observe(document.body, options); |
| 100 | + this.mutationObserver.observe(document.documentElement, options); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Provided helper method to determine localized or defaultString for specific string is returned |
| 105 | + * |
| 106 | + * @static updateStringsForTag |
| 107 | + * @param {string} tagName |
| 108 | + * @param stringsObj |
| 109 | + * @returns |
| 110 | + * @memberof LocalizationHelper |
| 111 | + */ |
| 112 | + public static updateStringsForTag(tagName: string, stringObj) { |
| 113 | + tagName = tagName.toLowerCase(); |
| 114 | + |
| 115 | + if (tagName.startsWith('mgt-')) { |
| 116 | + tagName = tagName.substring(4); |
| 117 | + } |
| 118 | + |
| 119 | + if (this._strings && stringObj) { |
| 120 | + //check for top level strings, applied per component, overridden by specific component def |
| 121 | + for (let prop of Object.entries(stringObj)) { |
| 122 | + if (this._strings[prop[0]]) { |
| 123 | + stringObj[prop[0]] = this._strings[prop[0]]; |
| 124 | + } |
| 125 | + } |
| 126 | + //strings defined component specific |
| 127 | + if (this._strings['_components'] && this._strings['_components'][tagName]) { |
| 128 | + let strings: any = this._strings['_components'][tagName]; |
| 129 | + for (let key of Object.keys(strings)) { |
| 130 | + if (stringObj[key]) { |
| 131 | + stringObj[key] = strings[key]; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return stringObj; |
| 138 | + } |
| 139 | +} |
0 commit comments