@hashicorp/design-system-components
Version:
Helios Design System Components
39 lines (34 loc) • 1.11 kB
JavaScript
import Service from '@ember/service';
import { getOwner } from '@ember/owner';
import { isPresent } from '@ember/utils';
import { assert } from '@ember/debug';
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
class HdsIntlService extends Service {
get intl() {
const owner = getOwner(this);
if (typeof owner?.factoryFor === 'function' && owner.factoryFor('service:intl')) {
return owner.lookup('service:intl');
}
return undefined;
}
t(key, options) {
const {
default: defaultString,
...restOptions
} = options;
assert('HdsIntlService requires a key as the first positional argument', typeof key === 'string' && isPresent(key));
// try to use ember-intl if available and a translation key exists
if (this.intl !== undefined) {
const localeIsSet = this.intl.locales && this.intl.locales.length > 0;
if (localeIsSet && this.intl.exists(key)) {
return this.intl.t(key, restOptions);
}
}
return defaultString;
}
}
export { HdsIntlService as default };
//# sourceMappingURL=hds-intl.js.map