UNPKG

angular-l10n

Version:

An Angular library to translate messages, dates and numbers

322 lines 11.9 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ import { Injectable, Inject } from "@angular/core"; import { Router, NavigationStart, NavigationEnd } from "@angular/router"; import { Location } from "@angular/common"; import { filter } from "rxjs/operators"; import { LocaleService } from "../services/locale.service"; import { InjectorRef } from "./injector-ref"; import { L10N_CONFIG } from "./l10n-config"; import { ISOCode, ExtraCode } from "./types"; var LocalizedRouting = /** @class */ (function () { function LocalizedRouting(configuration, locale) { this.configuration = configuration; this.locale = locale; } Object.defineProperty(LocalizedRouting.prototype, "router", { get: /** * @return {?} */ function () { return InjectorRef.get(Router); }, enumerable: true, configurable: true }); Object.defineProperty(LocalizedRouting.prototype, "location", { get: /** * @return {?} */ function () { return InjectorRef.get(Location); }, enumerable: true, configurable: true }); /** * @return {?} */ LocalizedRouting.prototype.init = /** * @return {?} */ function () { var _this = this; if (this.configuration.localizedRouting.format) { // Parses the url to find the locale when the app starts. /** @type {?} */ var path = this.location.path(true); this.parsePath(path); // Parses the url to find the locale when a navigation starts. this.router.events.pipe(filter(function (event) { return event instanceof NavigationStart; })).subscribe(function (data) { _this.redirectToPath(data.url); }); // Replaces url when a navigation ends. this.router.events.pipe(filter(function (event) { return event instanceof NavigationEnd; })).subscribe(function (data) { /** @type {?} */ var url = (!!data.url && data.url != "/" && data.url == data.urlAfterRedirects) ? data.url : data.urlAfterRedirects; _this.replacePath(_this.locale.composeLocale((/** @type {?} */ (_this.configuration.localizedRouting.format))), url); }); // Replaces url when locale changes. this.locale.languageCodeChanged.subscribe(function () { _this.replacePath(_this.locale.composeLocale((/** @type {?} */ (_this.configuration.localizedRouting.format)))); }); this.locale.defaultLocaleChanged.subscribe(function () { _this.replacePath(_this.locale.composeLocale((/** @type {?} */ (_this.configuration.localizedRouting.format)))); }); } }; /** * Parses path to find the locale. * @param path The path to be parsed */ /** * Parses path to find the locale. * @param {?=} path The path to be parsed * @return {?} */ LocalizedRouting.prototype.parsePath = /** * Parses path to find the locale. * @param {?=} path The path to be parsed * @return {?} */ function (path) { if (!path) return; /** @type {?} */ var segment = this.getLocalizedSegment(path); if (segment != null) { /** @type {?} */ var locale = (/** @type {?} */ (segment)).replace(/\//gi, ""); /** @type {?} */ var localeCodes = this.splitLocale(locale, (/** @type {?} */ (this.configuration.localizedRouting.format))); // Unrecognized segment. if (this.configuration.localizedRouting.schema) { if (!this.compareLocale(localeCodes, { languageCode: localeCodes.languageCode, scriptCode: this.getSchema(ISOCode.Script, localeCodes), countryCode: this.getSchema(ISOCode.Country, localeCodes) })) return; } if (this.configuration.locale.language) { this.locale.setCurrentLanguage(localeCodes.languageCode); } if (this.configuration.locale.defaultLocale) { this.locale.setDefaultLocale(localeCodes.languageCode, localeCodes.countryCode || this.getSchema(ISOCode.Country, localeCodes), localeCodes.scriptCode || this.getSchema(ISOCode.Script, localeCodes), this.getSchema(ExtraCode.NumberingSystem, localeCodes), this.getSchema(ExtraCode.Calendar, localeCodes)); } if (this.configuration.locale.currency) { /** @type {?} */ var currency = this.getSchema(ExtraCode.Currency, localeCodes); if (currency) { this.locale.setCurrentCurrency(currency); } } if (this.configuration.locale.timezone) { /** @type {?} */ var timezone = this.getSchema(ExtraCode.Timezone, localeCodes); if (timezone) { this.locale.setCurrentTimezone(timezone); } } } }; /** * Removes the locale from the path and navigates without pushing a new state into history. * @param path Localized path */ /** * Removes the locale from the path and navigates without pushing a new state into history. * @param {?} path Localized path * @return {?} */ LocalizedRouting.prototype.redirectToPath = /** * Removes the locale from the path and navigates without pushing a new state into history. * @param {?} path Localized path * @return {?} */ function (path) { /** @type {?} */ var segment = this.getLocalizedSegment(path); if (segment != null) { /** @type {?} */ var url = path.replace(segment, "/"); // navigateByUrl keeps the query params. this.router.navigateByUrl(url, { skipLocationChange: true }); } }; /** * Replaces the path with the locale without pushing a new state into history. * @param locale The current locale * @param path The path to be replaced */ /** * Replaces the path with the locale without pushing a new state into history. * @param {?} locale The current locale * @param {?=} path The path to be replaced * @return {?} */ LocalizedRouting.prototype.replacePath = /** * Replaces the path with the locale without pushing a new state into history. * @param {?} locale The current locale * @param {?=} path The path to be replaced * @return {?} */ function (locale, path) { if (path) { if (!this.isDefaultRouting()) { this.location.replaceState(this.getLocalizedPath(locale, path)); } } else { path = this.location.path(true); // Parses the path to find the locale. /** @type {?} */ var segment = this.getLocalizedSegment(path); if (segment != null) { // Removes the locale from the path. path = path.replace(segment, "/"); if (this.isDefaultRouting()) { this.location.replaceState(path); } } if (!this.isDefaultRouting()) { this.location.replaceState(this.getLocalizedPath(locale, path)); } } }; /** * @param {?} path * @return {?} */ LocalizedRouting.prototype.getLocalizedSegment = /** * @param {?} path * @return {?} */ function (path) { for (var _i = 0, _a = this.locale.getAvailableLanguages(); _i < _a.length; _i++) { var lang = _a[_i]; /** @type {?} */ var regex = new RegExp("(/" + lang + "/)|(/" + lang + "$)|(/" + lang + "-.*?/)|(/" + lang + "-.*?$)"); /** @type {?} */ var segments = path.match(regex); if (segments != null) { return segments[0]; } } return null; }; /** * @param {?} locale * @param {?} codes * @return {?} */ LocalizedRouting.prototype.splitLocale = /** * @param {?} locale * @param {?} codes * @return {?} */ function (locale, codes) { /** @type {?} */ var values = locale.split("-"); /** @type {?} */ var localeCodes = { languageCode: "" }; if (codes.length > 0) { for (var i = 0; i < codes.length; i++) { if (values[i]) localeCodes[codes[i]] = values[i]; } } return localeCodes; }; /** * @param {?} locale1 * @param {?} locale2 * @return {?} */ LocalizedRouting.prototype.compareLocale = /** * @param {?} locale1 * @param {?} locale2 * @return {?} */ function (locale1, locale2) { return locale1.languageCode == locale2.languageCode && (!!locale1.scriptCode ? locale1.scriptCode == locale2.scriptCode : true) && (!!locale1.countryCode ? locale1.countryCode == locale2.countryCode : true); }; /** * @param {?} code * @param {?} locale * @return {?} */ LocalizedRouting.prototype.getSchema = /** * @param {?} code * @param {?} locale * @return {?} */ function (code, locale) { var _this = this; if (!this.configuration.localizedRouting.schema) return undefined; /** @type {?} */ var schema = this.configuration.localizedRouting.schema .find((function (s) { return _this.compareLocale(locale, s); })); return schema ? schema[code] : undefined; }; /** * @return {?} */ LocalizedRouting.prototype.isDefaultRouting = /** * @return {?} */ function () { if (!this.configuration.localizedRouting.defaultRouting) return false; if (this.configuration.locale.language) { return this.locale.getCurrentLanguage() == this.configuration.locale.language; } if (this.configuration.locale.defaultLocale) { return this.compareLocale({ languageCode: this.locale.getCurrentLanguage(), scriptCode: this.locale.getCurrentScript(), countryCode: this.locale.getCurrentCountry() }, this.configuration.locale.defaultLocale); } return false; }; /** * @param {?} locale * @param {?} path * @return {?} */ LocalizedRouting.prototype.getLocalizedPath = /** * @param {?} locale * @param {?} path * @return {?} */ function (locale, path) { return Location.stripTrailingSlash("/" + locale + path); }; LocalizedRouting.decorators = [ { type: Injectable } ]; /** @nocollapse */ LocalizedRouting.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] }, { type: LocaleService } ]; }; return LocalizedRouting; }()); export { LocalizedRouting }; if (false) { /** @type {?} */ LocalizedRouting.prototype.configuration; /** @type {?} */ LocalizedRouting.prototype.locale; } //# sourceMappingURL=localized-routing.js.map