Intl
Intl is a light-weight service that provides internationalization (i18n) functionalities.
Classes
Intl
Defined in: intl/intl.service.ts:26
The Intl service provides i18n-related functionalities.
See
https://github.com/Modyfi/vite-plugin-yaml
Example
import { Intl } from '@spuxx/js-utils';import de from './translations/de.yml';
Intl.setup({ dictionaries: [{ locale: 'de', values: de }], fallbackLocale: 'de',});Intl.translate('foo');Extends
Service<Intl,this>
Constructors
Constructor
protectednew Intl():Intl
Defined in: mixin/service-mixin.ts:24
Service classes should not be instantiated directly. Instead, access the instance property
to get the existing singleton instance or to create a new one if it does not yet exist.
Returns
Inherited from
ServiceMixin<Intl>().constructor
Accessors
currentDictionary
Get Signature
get
staticcurrentDictionary():Dictionary
Defined in: intl/intl.service.ts:95
Returns
currentLocale
Get Signature
get
staticcurrentLocale():string
Defined in: intl/intl.service.ts:81
Returns
string
dictionaries
Get Signature
get
staticdictionaries():Dictionary[]
Defined in: intl/intl.service.ts:88
Returns
fallbackLocale
Get Signature
get
staticfallbackLocale():string
Defined in: intl/intl.service.ts:74
Returns
string
instance
Get Signature
get
staticinstance():TService
Defined in: mixin/service-mixin.ts:33
Returns the instance of the service.
Example
const myService = MyService.instance;myService.doSomething();Returns
TService
The instance of the service.
Inherited from
ServiceMixin<Intl>().instance
missingLocalizationPrefix
Get Signature
get
staticmissingLocalizationPrefix():string
Defined in: intl/intl.service.ts:133
Keys that cannot be translated due to missing localization will be returned and prefixed with this string.
Returns
string
Methods
destroy()
staticdestroy():void
Defined in: mixin/service-mixin.ts:47
Destroys the existing instance of the service.
Returns
void
Example
const myService = MyService.instance;MyService.destroy();const myNewService = MyService.instance; // This will be a new instanceInherited from
ServiceMixin<Intl>().destroy
setLocale()
staticsetLocale(locale):void
Defined in: intl/intl.service.ts:59
Parameters
locale
string
Returns
void
setup()
staticsetup(options):void
Defined in: intl/intl.service.ts:44
Sets up Intl with the given options. Needs to be called at application startup.
Parameters
options
SetupOptions
The options to use.
Returns
void
See
https://github.com/Modyfi/vite-plugin-yaml
Example
import { Intl } from '@spuxx/js-utils';import de from './translations/de.yml';
Intl.setup({ dictionaries: [{ locale: 'de', values: de }], fallbackLocale: 'de',});translate()
statictranslate(key,vars?):string
Defined in: intl/intl.service.ts:115
Translates the given key.
Parameters
key
string
The key to translate.
vars?
Record<string, string>
Returns
string
The translated value.
Example
// Using the service directly:console.log(Intl.translate('foo'));// Or using the shorthand:console.log(intl('foo'));
// Using variables:console.log(Intl.translate('foo', { bar: 'baz' })); // 'Hello {bar}' -> 'Hello baz'Interfaces
Dictionary
Defined in: intl/intl.service.ts:4
Properties
locale
locale:
string
Defined in: intl/intl.service.ts:5
values
values:
Record<string,unknown>
Defined in: intl/intl.service.ts:6
Variables
intl()
constintl: (key,vars?) =>string
Defined in: intl/intl.service.ts:156
Translates the given key.
Parameters
key
string
The key to translate.
vars?
Record<string, string>
Returns
string
The translated value.
Example
// Using the service directly:console.log(Intl.translate('foo'));// Or using the shorthand:console.log(intl('foo'));
// Using variables:console.log(Intl.translate('foo', { bar: 'baz' })); // 'Hello {bar}' -> 'Hello baz'