angular-l10n
Version:
An Angular library to translate messages, dates and numbers
333 lines • 14.3 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { LocaleService } from './locale.service';
import { TranslationService } from './translation.service';
import { IntlAPI } from './intl-api';
/**
* @record
*/
export function ICollator() { }
if (false) {
/**
* @param {?} key1
* @param {?} key2
* @param {?=} extension
* @param {?=} options
* @return {?}
*/
ICollator.prototype.compare = function (key1, key2, extension, options) { };
/**
* @param {?} list
* @param {?} keyName
* @param {?=} order
* @param {?=} extension
* @param {?=} options
* @return {?}
*/
ICollator.prototype.sort = function (list, keyName, order, extension, options) { };
/**
* @param {?} list
* @param {?} keyName
* @param {?=} order
* @param {?=} extension
* @param {?=} options
* @return {?}
*/
ICollator.prototype.sortAsync = function (list, keyName, order, extension, options) { };
/**
* @param {?} s
* @param {?} list
* @param {?} keyNames
* @param {?=} options
* @return {?}
*/
ICollator.prototype.search = function (s, list, keyNames, options) { };
/**
* @param {?} s
* @param {?} list
* @param {?} keyNames
* @param {?=} options
* @return {?}
*/
ICollator.prototype.searchAsync = function (s, list, keyNames, options) { };
}
/**
* Intl.Collator APIs.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator
*/
var Collator = /** @class */ (function () {
function Collator(locale, translation) {
this.locale = locale;
this.translation = translation;
}
/**
* Compares two keys by the value of translation according to the current language.
* @param key1, key2 The keys of the values to compare
* @param extension Unicode extension key, e.g. 'co-phonebk'
* @param options Default is { usage: 'sort', sensitivity: 'variant' }
* @return A negative value if the value of translation of key1 comes before the value of translation of key2;
* a positive value if key1 comes after key2;
* 0 if they are considered equal or Intl.Collator is not supported
*/
/**
* Compares two keys by the value of translation according to the current language.
* @param {?} key1
* @param {?} key2
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} A negative value if the value of translation of key1 comes before the value of translation of key2;
* a positive value if key1 comes after key2;
* 0 if they are considered equal or Intl.Collator is not supported
*/
Collator.prototype.compare = /**
* Compares two keys by the value of translation according to the current language.
* @param {?} key1
* @param {?} key2
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} A negative value if the value of translation of key1 comes before the value of translation of key2;
* a positive value if key1 comes after key2;
* 0 if they are considered equal or Intl.Collator is not supported
*/
function (key1, key2, extension, options) {
if (options === void 0) { options = { usage: 'sort', sensitivity: 'variant' }; }
if (!IntlAPI.hasCollator())
return 0;
/** @type {?} */
var value1 = this.translation.translate(key1);
/** @type {?} */
var value2 = this.translation.translate(key2);
/** @type {?} */
var locale = this.addExtension(this.locale.getCurrentLocale(), extension);
return new Intl.Collator(locale, options).compare(value1, value2);
};
/**
* Sorts an array of objects or an array of arrays according to the current language.
* @param list The array to be sorted
* @param keyName The column that contains the keys of the values to be ordered
* @param order 'asc' or 'desc'. The default value is 'asc'
* @param extension Unicode extension key, e.g. 'co-phonebk'
* @param options Default is { usage: 'sort', sensitivity: 'variant' }
* @return The same sorted list or the same list if Intl.Collator is not supported
*/
/**
* Sorts an array of objects or an array of arrays according to the current language.
* @param {?} list The array to be sorted
* @param {?} keyName The column that contains the keys of the values to be ordered
* @param {?=} order 'asc' or 'desc'. The default value is 'asc'
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} The same sorted list or the same list if Intl.Collator is not supported
*/
Collator.prototype.sort = /**
* Sorts an array of objects or an array of arrays according to the current language.
* @param {?} list The array to be sorted
* @param {?} keyName The column that contains the keys of the values to be ordered
* @param {?=} order 'asc' or 'desc'. The default value is 'asc'
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} The same sorted list or the same list if Intl.Collator is not supported
*/
function (list, keyName, order, extension, options) {
var _this = this;
if (order === void 0) { order = "asc"; }
if (options === void 0) { options = { usage: 'sort', sensitivity: 'variant' }; }
if (!list || !keyName || !IntlAPI.hasCollator())
return list;
list.sort(function (key1, key2) {
return _this.compare(key1[keyName], key2[keyName], extension, options);
});
if (order == "desc") {
list.reverse();
}
return list;
};
/**
* Sorts asynchronously an array of objects or an array of arrays according to the current language.
* @param list The array to be sorted
* @param keyName The column that contains the keys of the values to be ordered
* @param order 'asc' or 'desc'. The default value is 'asc'
* @param extension Unicode extension key, e.g. 'co-phonebk'
* @param options Default is { usage: 'sort', sensitivity: 'variant' }
* @return An observable of the sorted list or of the same list if Intl.Collator is not supported
*/
/**
* Sorts asynchronously an array of objects or an array of arrays according to the current language.
* @param {?} list The array to be sorted
* @param {?} keyName The column that contains the keys of the values to be ordered
* @param {?=} order 'asc' or 'desc'. The default value is 'asc'
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} An observable of the sorted list or of the same list if Intl.Collator is not supported
*/
Collator.prototype.sortAsync = /**
* Sorts asynchronously an array of objects or an array of arrays according to the current language.
* @param {?} list The array to be sorted
* @param {?} keyName The column that contains the keys of the values to be ordered
* @param {?=} order 'asc' or 'desc'. The default value is 'asc'
* @param {?=} extension Unicode extension key, e.g. 'co-phonebk'
* @param {?=} options Default is { usage: 'sort', sensitivity: 'variant' }
* @return {?} An observable of the sorted list or of the same list if Intl.Collator is not supported
*/
function (list, keyName, order, extension, options) {
var _this = this;
if (options === void 0) { options = { usage: 'sort', sensitivity: 'variant' }; }
return new Observable(function (observer) {
observer.next(_this.sort(list, keyName, order, extension, options));
observer.complete();
});
};
/**
* Matches a string into an array of objects or an array of arrays according to the current language.
* @param s The string to search
* @param list The array in which to search
* @param keyNames An array that contains the columns to look for
* @param options Default is { usage: 'search' }
* @return A filtered list or the same list if Intl.Collator is not supported
*/
/**
* Matches a string into an array of objects or an array of arrays according to the current language.
* @param {?} s The string to search
* @param {?} list The array in which to search
* @param {?} keyNames An array that contains the columns to look for
* @param {?=} options Default is { usage: 'search' }
* @return {?} A filtered list or the same list if Intl.Collator is not supported
*/
Collator.prototype.search = /**
* Matches a string into an array of objects or an array of arrays according to the current language.
* @param {?} s The string to search
* @param {?} list The array in which to search
* @param {?} keyNames An array that contains the columns to look for
* @param {?=} options Default is { usage: 'search' }
* @return {?} A filtered list or the same list if Intl.Collator is not supported
*/
function (s, list, keyNames, options) {
var _this = this;
if (options === void 0) { options = { usage: 'search' }; }
if (!list || !keyNames || s == "" || s == null || !IntlAPI.hasCollator())
return list;
/** @type {?} */
var locale = this.locale.getCurrentLocale();
/** @type {?} */
var collator = new Intl.Collator(locale, options);
/** @type {?} */
var matches = list.filter(function (key) {
/** @type {?} */
var found = false;
for (var i = 0; i < keyNames.length; i++) {
if (_this.match(key[keyNames[i]], s, collator)) {
found = true;
break;
}
}
return found;
});
return matches;
};
/**
* Matches asynchronously a string into an array of objects or an array of arrays according to the current language.
* @param s The string to search
* @param list The array in which to search
* @param keyNames An array that contains the columns to look for
* @param options Default is { usage: 'search' }
* @return An observable of the filtered list or the same list if Intl.Collator is not supported
*/
/**
* Matches asynchronously a string into an array of objects or an array of arrays according to the current language.
* @param {?} s The string to search
* @param {?} list The array in which to search
* @param {?} keyNames An array that contains the columns to look for
* @param {?=} options Default is { usage: 'search' }
* @return {?} An observable of the filtered list or the same list if Intl.Collator is not supported
*/
Collator.prototype.searchAsync = /**
* Matches asynchronously a string into an array of objects or an array of arrays according to the current language.
* @param {?} s The string to search
* @param {?} list The array in which to search
* @param {?} keyNames An array that contains the columns to look for
* @param {?=} options Default is { usage: 'search' }
* @return {?} An observable of the filtered list or the same list if Intl.Collator is not supported
*/
function (s, list, keyNames, options) {
var _this = this;
if (options === void 0) { options = { usage: 'search' }; }
return new Observable(function (observer) {
observer.next(_this.search(s, list, keyNames, options));
observer.complete();
});
};
/**
* @param {?} locale
* @param {?=} extension
* @return {?}
*/
Collator.prototype.addExtension = /**
* @param {?} locale
* @param {?=} extension
* @return {?}
*/
function (locale, extension) {
if (!!extension) {
locale = locale + "-u-" + extension;
}
return locale;
};
/**
* @param {?} key
* @param {?} s
* @param {?} collator
* @return {?}
*/
Collator.prototype.match = /**
* @param {?} key
* @param {?} s
* @param {?} collator
* @return {?}
*/
function (key, s, collator) {
/** @type {?} */
var value = this.translation.translate(key);
/** @type {?} */
var valueLength = value.length;
/** @type {?} */
var sLength = s.length;
if (sLength > valueLength) {
return false;
}
if (sLength == valueLength) {
return collator.compare(value, s) == 0;
}
/** @type {?} */
var found = false;
for (var i = 0; i < valueLength - (sLength - 1); i++) {
/** @type {?} */
var str = value.substr(i, sLength);
if (collator.compare(str, s) == 0) {
found = true;
break;
}
}
return found;
};
Collator.decorators = [
{ type: Injectable }
];
/** @nocollapse */
Collator.ctorParameters = function () { return [
{ type: LocaleService },
{ type: TranslationService }
]; };
return Collator;
}());
export { Collator };
if (false) {
/** @type {?} */
Collator.prototype.locale;
/** @type {?} */
Collator.prototype.translation;
}
//# sourceMappingURL=collator.js.map