|
| 1 | +import { |
| 2 | + AngularElement, |
| 3 | + HtmlTranslationExtractionContext |
| 4 | +} from "./html-translation-extractor"; |
| 5 | +import { Attribute } from "./element-context"; |
| 6 | + |
| 7 | +const I18N_ATTRIBUTE_REGEX = /^i18n-.*$/; |
| 8 | +const I18N_ATTRIBUTE_NAME = "i18n"; |
| 9 | +const ID_INDICATOR = "@@"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Angular uses i18n and i18n-[attr] attributes for internationalization. |
| 13 | + * The angularI18nTranslationsExtractor looks for these attributes on elements |
| 14 | + * and extracts the found ids and default translations from the elements. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * <div i18n="@@translationId">some translation</div> |
| 18 | + * results in a translation with id: 'translationId' and default translation: 'some translation' |
| 19 | + * |
| 20 | + * @example |
| 21 | + * <div i18n-title="@@titleId" title="some title"></div> |
| 22 | + * results in a translation with id: 'titleId' and default translation: 'some title' |
| 23 | + * |
| 24 | + * @param element the element to check for translations |
| 25 | + * @param context the current context |
| 26 | + */ |
| 27 | +export default function angularI18nTranslationsExtractor( |
| 28 | + element: AngularElement, |
| 29 | + context: HtmlTranslationExtractionContext |
| 30 | +): void { |
| 31 | + const i18nElementTranslation = element.attributes.find( |
| 32 | + attribute => attribute.name === I18N_ATTRIBUTE_NAME |
| 33 | + ); |
| 34 | + |
| 35 | + if (i18nElementTranslation) { |
| 36 | + handleTranslationsOfElements(element, context, i18nElementTranslation); |
| 37 | + } |
| 38 | + |
| 39 | + const i18nAttributeTranslations = element.attributes.filter(attribute => |
| 40 | + I18N_ATTRIBUTE_REGEX.test(attribute.name) |
| 41 | + ); |
| 42 | + |
| 43 | + handleTranslationsOfAttributes(element, context, i18nAttributeTranslations); |
| 44 | +} |
| 45 | + |
| 46 | +function handleTranslationsOfElements( |
| 47 | + element: AngularElement, |
| 48 | + context: HtmlTranslationExtractionContext, |
| 49 | + attribute: Attribute |
| 50 | +): void { |
| 51 | + const translationIdExtraction = extractTranslationId(attribute, context); |
| 52 | + |
| 53 | + if (translationIdExtraction.valid === false) { |
| 54 | + context.emitError(translationIdExtraction.error, attribute.startPosition); |
| 55 | + } else if (element.texts.length === 0) { |
| 56 | + context.emitError( |
| 57 | + `The element ${context.asHtml()} with attribute ${ |
| 58 | + attribute.name |
| 59 | + } is empty and is therefore missing the default translation.`, |
| 60 | + attribute.startPosition |
| 61 | + ); |
| 62 | + } else if (element.texts.length === 1) { |
| 63 | + context.registerTranslation({ |
| 64 | + translationId: translationIdExtraction.translationId, |
| 65 | + defaultText: element.texts[0].text, |
| 66 | + position: element.startPosition |
| 67 | + }); |
| 68 | + } else if (element.texts.length > 1) { |
| 69 | + context.emitError( |
| 70 | + `The element ${context.asHtml()} has multiple child elements and, therefore, the default translation cannot be extracted.`, |
| 71 | + attribute.startPosition |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function handleTranslationsOfAttributes( |
| 77 | + element: AngularElement, |
| 78 | + context: HtmlTranslationExtractionContext, |
| 79 | + i18nAttributes: Attribute[] |
| 80 | +): void { |
| 81 | + for (const i18nAttribute of i18nAttributes) { |
| 82 | + handleAttribute(element, context, i18nAttribute); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function handleAttribute( |
| 87 | + element: AngularElement, |
| 88 | + context: HtmlTranslationExtractionContext, |
| 89 | + i18nAttribute: Attribute |
| 90 | +): void { |
| 91 | + const translationIdExtraction = extractTranslationId(i18nAttribute, context); |
| 92 | + if (translationIdExtraction.valid === false) { |
| 93 | + context.emitError( |
| 94 | + translationIdExtraction.error, |
| 95 | + i18nAttribute.startPosition |
| 96 | + ); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const attributeName = i18nAttribute.name.substr( |
| 101 | + `${I18N_ATTRIBUTE_NAME}-`.length |
| 102 | + ); |
| 103 | + const attribute = element.attributes.find( |
| 104 | + attribute => attribute.name === attributeName |
| 105 | + ); |
| 106 | + |
| 107 | + if (!attribute) { |
| 108 | + context.emitError( |
| 109 | + `The element ${context.asHtml()} with ${ |
| 110 | + i18nAttribute.name |
| 111 | + } is missing a corresponding ${attributeName} attribute.`, |
| 112 | + element.startPosition |
| 113 | + ); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const defaultText = attribute.value; |
| 118 | + |
| 119 | + if (!defaultText) { |
| 120 | + context.emitError( |
| 121 | + `The element ${context.asHtml()} with ${ |
| 122 | + i18nAttribute.name |
| 123 | + } is missing a value for the corresponding ${attributeName} attribute.`, |
| 124 | + element.startPosition |
| 125 | + ); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + context.registerTranslation({ |
| 130 | + translationId: translationIdExtraction.translationId, |
| 131 | + defaultText: defaultText, |
| 132 | + position: i18nAttribute.startPosition |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +function extractTranslationId( |
| 137 | + attribute: Attribute, |
| 138 | + context: HtmlTranslationExtractionContext |
| 139 | +): { valid: true; translationId: string } | { valid: false; error: string } { |
| 140 | + const index = attribute.value.indexOf(ID_INDICATOR); |
| 141 | + if (index < 0) { |
| 142 | + return { |
| 143 | + valid: false, |
| 144 | + error: `The attribute ${ |
| 145 | + attribute.name |
| 146 | + } on element ${context.asHtml()} attribute is missing the custom id indicator '${ID_INDICATOR}'.` |
| 147 | + }; |
| 148 | + } else if (index + ID_INDICATOR.length === attribute.value.length) { |
| 149 | + return { |
| 150 | + valid: false, |
| 151 | + error: `The attribute ${ |
| 152 | + attribute.name |
| 153 | + } on element ${context.asHtml()} defines an empty ID.` |
| 154 | + }; |
| 155 | + } else { |
| 156 | + return { |
| 157 | + valid: true, |
| 158 | + translationId: attribute.value.substr(index + ID_INDICATOR.length) |
| 159 | + }; |
| 160 | + } |
| 161 | +} |
0 commit comments