Skip to content

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

protected new 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

Intl

Inherited from

ServiceMixin<Intl>().constructor

Accessors

currentDictionary
Get Signature

get static currentDictionary(): Dictionary

Defined in: intl/intl.service.ts:95

Returns

Dictionary

currentLocale
Get Signature

get static currentLocale(): string

Defined in: intl/intl.service.ts:81

Returns

string

dictionaries
Get Signature

get static dictionaries(): Dictionary[]

Defined in: intl/intl.service.ts:88

Returns

Dictionary[]

fallbackLocale
Get Signature

get static fallbackLocale(): string

Defined in: intl/intl.service.ts:74

Returns

string

instance
Get Signature

get static instance(): 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 static missingLocalizationPrefix(): 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()

static destroy(): 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 instance
Inherited from

ServiceMixin<Intl>().destroy

setLocale()

static setLocale(locale): void

Defined in: intl/intl.service.ts:59

Parameters
locale

string

Returns

void

setup()

static setup(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()

static translate(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()

const intl: (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'