Internationalization (i18n)
The@wordpress/i18n package provides internationalization utilities for client-side localization in WordPress. It allows you to translate text strings in your JavaScript code, similar to how PHP functions work on the server side.
Installation
You can install the package using npm:This package assumes your code will run in an ES2015+ environment. If you’re targeting environments with limited support for modern JavaScript features, include the polyfill from
@wordpress/babel-preset-default.Basic Usage
Translation Functions
__( text, domain )
Retrieves the translation of text.Text to translate.
Domain to retrieve the translated text.
Translated text.
_x( text, context, domain )
Retrieves translated string with gettext context.Text to translate.
Context information for the translators.
Domain to retrieve the translated text.
Translated context string without pipe.
_n( single, plural, number, domain )
Translates and retrieves the singular or plural form based on the supplied number.The text to be used if the number is singular.
The text to be used if the number is plural.
The number to compare against to use either the singular or plural form.
Domain to retrieve the translated text.
The translated singular or plural form.
_nx( single, plural, number, context, domain )
Translates and retrieves the singular or plural form based on the supplied number, with gettext context.The text to be used if the number is singular.
The text to be used if the number is plural.
The number to compare against to use either the singular or plural form.
Context information for the translators.
Domain to retrieve the translated text.
The translated singular or plural form.
String Formatting
sprintf( format, …args )
Returns a formatted string. This function is similar to PHP’ssprintf().
The format of the string to generate.
Arguments to apply to the format.
The formatted string.
Locale Management
setLocaleData( data, domain )
Merges locale data into the Tannin instance by domain. Accepts data in a Jed-formatted JSON object shape.Locale data configuration.
Domain for which configuration applies.
getLocaleData( domain )
Returns locale data by domain in a Jed-formatted JSON object shape.Domain for which to get the data.
Locale data.
resetLocaleData( data, domain )
Resets all current Tannin instance locale data and sets the specified locale data for the domain.Locale data configuration.
Domain for which configuration applies.
Utility Functions
hasTranslation( single, context, domain )
Checks if there is a translation for a given string (in singular form).Singular form of the string to look up.
Context information for the translators.
Domain to retrieve the translated text.
Whether the translation exists or not.
isRTL()
Checks if current locale is RTL (Right To Left).Whether locale is RTL.
RTL (Right To Left) is a locale property indicating that text is written from right to left. For example, Hebrew (
he) and Arabic (ar) are RTL languages. The opposite, LTR (Left To Right), is used in languages like English (en), Spanish (es), and French (fr).Advanced Features
createI18n( initialData, initialDomain, hooks )
Creates an i18n instance for isolated translation contexts.Locale data configuration.
Domain for which configuration applies.
Hooks implementation.
I18n instance.
defaultI18n
The default, singleton instance ofI18n used by all the top-level translation functions.
subscribe( callback )
Subscribes to changes of locale data.Subscription callback function.
Unsubscribe callback.