Options
All
  • Public
  • Public/Protected
  • All
Menu

typed-intl API documentation

Be sure to read the overview documentation before you dive into the API details.

Index

Type aliases

Language

Language: LanguageTag | string

Either a LanguageTag or a string which can be provided to languageTag().

LocalizedMessages

LocalizedMessages: function

Sometimes you may need the current language when defining Messages, for example to define language dependent formatting. Instead of directly providing a Messages object you may provide a function of this type then.

Type declaration

Messages

Messages: object

Messages are defined as simple JavaScript objects where the property key is the message key and the value is the message. Messages should either be simple strings or functions returning a string.

Type declaration

MessagesParameter

MessagesParameter: M | LocalizedMessages<M>

Some functions allow you to either directly specify a Messages object or a LocalizedMessages function.

Functions

addFormats

format

  • Format messages using ICU message syntax. Instead of using an object as parameter like formatObject this function accepts up to five parameters, which are then available by the names 1 to 5 inside the message string.

    Example:

    format<number>(languageTag('en'), 'Current message count is {1, number}')(3);
    

    Type parameters

    • P1

    • P2

    • P3

    • P4

    • P5

    Parameters

    • language: LanguageTag

      the language to be used for message formatting (e.g. for numbers and plurals)

    • msgFormat: string

      the message using ICU message syntax.

    • Default value formatOptions: FormatOptions = formats()

      custom format options used by the message. Defaults to the globally registered formats.

    Returns function

    a function accepting the specified number of parameters.

      • (p1: P1, p2?: P2, p3?: P3, p4?: P4, p5?: P5): string
      • Parameters

        • p1: P1
        • Optional p2: P2
        • Optional p3: P3
        • Optional p4: P4
        • Optional p5: P5

        Returns string

formatObject

  • Format message using ICU message syntax.

    Example:

    const msg = formatObject<{msgCount: number}>(languageTag('en'),
                                                 'Current message count is {msgCount, number}');
    msg({msgCount: 3}); // 'Current message count is 3'
    

    Type parameters

    • P

    Parameters

    • language: LanguageTag

      the locale to be used for message formatting (e.g. for numbers and plurals)

    • msgFormat: string

      the message using ICU message syntax.

    • Default value formatOptions: FormatOptions = formats()

      custom format options used by the message. Defaults to the globally registered formats.

    Returns function

    a function accepting the parameter object.

      • (parameters: P): string
      • Parameters

        • parameters: P

        Returns string

formats

  • Global FormatOptions used by the format and formatObject function if nothing else is specified there. You can use setFormats and addFormats to register custom formats.

    By default the number formats #, #.#, #.## to #.##### are defined. You can use them in a message string like eg. {1, number, #.##} to format a number with the specified number of fraction digits.

    Returns FormatOptions

languageTag

  • Provides a LanguageTag for the specified string representation of a language tag.

    It is guaranteed, that this function returns the same LanguageTag instance for the same string (ignoring case). Thus it is save to use strict equality (===) when comparing LanguageTags.

    throws

    an exception if the provided string is not a valid language tag.

    Parameters

    • tag: string

      a language tag string as defined in BCP-47

    Returns LanguageTag

    the resulting language tag

pickPreferredLanguage

  • Picks the best supported language based on the user's preferred languages.

    Parameters

    • availableTranslations: LanguageTag[]

      the translations provided by this application

    • usersPreferredLanguages: LanguageTag[]

      the user's preferred languages ordered by preference (most preferred first)

    Returns LanguageTag

    the best matching language from usersPreferredLanguages or the first if no one matches.

plural

  • Creates a pluralized message which will return the adequate message based on the provided number and language.

    Example:

    const msg = plural(languageTag('en'), {
      zero: 'You have no new messages',
      one: 'You have one new message',
      other: n => format<number>(languageTag('en'), 'You have {1, number} new messages')(n)
    });
    msg(5); // 'You have 5 new messages'
    
    see

    Format.JS message syntax for details

    Parameters

    • language: LanguageTag

      the language to be used for decision which of the plural forms to choose.

    • p: Plural

      the messages for the different cases.

    Returns function

    a function accepting a numeral argument used to pick the right message.

      • (n: number): string
      • Parameters

        • n: number

        Returns string

preferredLanguage

select

  • Selects a message from SelectOptions based on the provided selection parameter.

    see

    selectObject for an example and if you need further formatting capabilities inside the messages

    Parameters

    Returns function

    a function expecting a selection string to pick the correct message from the provided options.

      • (selection: string): string
      • Parameters

        • selection: string

        Returns string

selectObject

  • Selects and formats a message from SelectOptions based on the provided parameter object.

    Example:

    const msg = selectObject<Person>(languageTag('en'), p => p.gender, {
        female: 'Dear Mrs. {name}',
        male: 'Dear Mr. {name}',
        other: 'Dear {name}'
    });
    msg({gender: 'female', name: 'Granger'}); // 'Dear Mrs. Granger'
    
    see

    select if you don't need the additional formatting options inside the messages.

    Type parameters

    • P

    Parameters

    • language: LanguageTag
    • selector: function

      a function that extracts the selection key from the provided parameter object.

        • (parameters: P): string
        • Parameters

          • parameters: P

          Returns string

    • options: SelectOptions

      the possible cases where each message will be formatted using formatObject based on the parameters object.

    Returns function

    a function expecting a single parameter object containing the selector and the values referenced by the messages.

      • (parameters: P): string
      • Parameters

        • parameters: P

        Returns string

selectPreferredLanguage

  • selectPreferredLanguage(availableTranslations: string[], usersPreferredLanguages?: string[]): void
  • Sets the preferredLanguage based on the translations supported by this application and the user's language preferences.

    see

    pickPreferredLanguage

    see

    setPreferredLanguage

    Parameters

    • availableTranslations: string[]

      the languages this app provides translations for

    • Default value usersPreferredLanguages: string[] = navigatorLanguages

      the user's preferred languages ordered by preference (most preferred first).

    Returns void

setFormats

setPreferredLanguage

translate

  • Create a Translator with the specified default messages.

    Type parameters

    • M

    Parameters

    • defaultMessages: MessagesParameter<M>

      messages to be used if no messages for a selected translation are available.

    Returns Translator<M>

    translator with the specified default messages.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc