Skip to content

Commit

Permalink
refactor(ivy): remove i18nConfigureLocalize instruction (angular#31609
Browse files Browse the repository at this point in the history
)

This has been replaced by the `loadTranslations()` function in
`@angular/localize/run_time`.

PR Close angular#31609
  • Loading branch information
petebacondarwin authored and mhevery committed Aug 30, 2019
1 parent c024d89 commit a7f61e6
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 333 deletions.
2 changes: 1 addition & 1 deletion integration/_payload-limits.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"master": {
"uncompressed": {
"runtime": 1440,
"main": 13266,
"main": 13264,
"polyfills": 45340
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/core_render3_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export {
ɵɵi18nEnd,
ɵɵi18nApply,
ɵɵi18nPostprocess,
i18nConfigureLocalize as ɵi18nConfigureLocalize,
setLocaleId as ɵsetLocaleId,
setClassMetadata as ɵsetClassMetadata,
ɵɵresolveWindow,
Expand Down
83 changes: 0 additions & 83 deletions packages/core/src/render3/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {InertBodyHelper} from '../sanitization/inert_body';
import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer';
import {addAllToArray} from '../util/array_utils';
import {assertDataInRange, assertDefined, assertEqual, assertGreaterThan} from '../util/assert';
import {global} from '../util/global';
import {attachPatchData} from './context_discovery';
import {bind, setDelayProjection} from './instructions/all';
import {attachI18nOpCodesDebug} from './instructions/lview_debug';
Expand Down Expand Up @@ -1317,88 +1316,6 @@ function replaceNgsp(value: string): string {
return value.replace(NGSP_UNICODE_REGEXP, ' ');
}

export interface I18nLocalizeOptions { translations: {[key: string]: string}; }

/**
* Provide translations for `$localize`.
*
* @deprecated this method is temporary & should not be used as it will be removed soon
*/
export function i18nConfigureLocalize(options: I18nLocalizeOptions = {
translations: {}
}) {
type TranslationInfo = {messageParts: TemplateStringsArray, placeholderNames: string[]};
type MessageInfo = {translationKey: string, replacements: {[placeholderName: string]: any}};
const PLACEHOLDER_MARKER = ':';
const TRANSLATIONS: {[key: string]: TranslationInfo} = {};

Object.keys(options.translations).forEach(key => {
TRANSLATIONS[key] = splitMessage(options.translations[key]);
});

if (ngDevMode) {
if (global.$localize === undefined) {
throw new Error(
'The global function `$localize` is missing. Please add `import \'@angular/localize\';` to your polyfills.ts file.');
}
}
$localize.translate = function(messageParts: TemplateStringsArray, expressions: readonly any[]):
[TemplateStringsArray, readonly any[]] {
const message = parseMessage(messageParts, expressions);
const translation = TRANSLATIONS[message.translationKey];
const result: [TemplateStringsArray, readonly any[]] =
(translation === undefined ? [messageParts, expressions] : [
translation.messageParts,
translation.placeholderNames.map(placeholder => message.replacements[placeholder])
]);
return result;
};

function splitMessage(message: string): TranslationInfo {
const parts = message.split(/{\$([^}]*)}/);
const messageParts = [parts[0]];
const placeholderNames: string[] = [];
for (let i = 1; i < parts.length - 1; i += 2) {
placeholderNames.push(parts[i]);
messageParts.push(parts[i + 1]);
}
const rawMessageParts =
messageParts.map(part => part.charAt(0) === PLACEHOLDER_MARKER ? '\\' + part : part);
return {messageParts: makeTemplateObject(messageParts, rawMessageParts), placeholderNames};
}

function parseMessage(
messageParts: TemplateStringsArray, expressions: readonly any[]): MessageInfo {
const PLACEHOLDER_NAME_MARKER = ':';
const replacements: {[placeholderName: string]: any} = {};
let translationKey = messageParts[0];
for (let i = 1; i < messageParts.length; i++) {
const messagePart = messageParts[i];
const expression = expressions[i - 1];
// There is a problem with synthesized template literals in TS where the raw version
// cannot be found, since there is no original source code to read it from.
// In that case we just fall back on the non-raw version.
// This should be OK because synthesized nodes (from the template compiler) will always have
// placeholder names provided.
if ((messageParts.raw[i] || messagePart).charAt(0) === PLACEHOLDER_NAME_MARKER) {
const endOfPlaceholderName = messagePart.indexOf(PLACEHOLDER_NAME_MARKER, 1);
const placeholderName = messagePart.substring(1, endOfPlaceholderName);
translationKey += `{$${placeholderName}}${messagePart.substring(endOfPlaceholderName + 1)}`;
replacements[placeholderName] = expression;
} else {
translationKey += messagePart;
replacements[`ph_${i}`] = expression;
}
}
return {translationKey, replacements};
}

function makeTemplateObject(cooked: string[], raw: string[]): TemplateStringsArray {
Object.defineProperty(cooked, 'raw', {value: raw});
return cooked as any;
}
}

/**
* The locale id that the application is currently using (for translations and ICU expressions).
* This is the ivy version of `LOCALE_ID` that was defined as an injection token for the view engine
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/render3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export {
ɵɵi18nEnd,
ɵɵi18nApply,
ɵɵi18nPostprocess,
i18nConfigureLocalize,
getLocaleId,
setLocaleId,
} from './i18n';
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/acceptance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ts_library(
"//packages/core",
"//packages/core/src/util",
"//packages/core/testing",
"//packages/localize",
"//packages/localize/run_time",
"//packages/platform-browser",
"//packages/platform-browser-dynamic",
"//packages/platform-browser/animations",
Expand Down
Loading

0 comments on commit a7f61e6

Please sign in to comment.