4.53.0 • Published 13 days ago

@wordpress/i18n v4.53.0

Weekly downloads
30,305
License
GPL-2.0-or-later
Repository
github
Last release
13 days ago

Internationalization (i18n)

Internationalization utilities for client-side localization. See How to Internationalize Your Plugin for server-side documentation.

Installation

Install the module:

npm install @wordpress/i18n --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress/babel-preset-default in your code.

Usage

import { sprintf, _n } from '@wordpress/i18n';

sprintf( _n( '%d hat', '%d hats', 4, 'text-domain' ), 4 );
// 4 hats

For a complete example, see the Internationalization section of the Block Editor Handbook.

API

createI18n

Create an i18n instance

Parameters

  • initialData [LocaleData]: Locale data configuration.
  • initialDomain [string]: Domain for which configuration applies.
  • hooks [Hooks]: Hooks implementation.

Returns

  • I18n: I18n instance.

defaultI18n

Default, singleton instance of I18n.

getLocaleData

Returns locale data by domain in a Jed-formatted JSON object shape.

Related

Parameters

  • domain [string]: Domain for which to get the data.

Returns

  • LocaleData: Locale data.

hasTranslation

Check if there is a translation for a given string (in singular form).

Parameters

  • single string: Singular form of the string to look up.
  • context [string]: Context information for the translators.
  • domain [string]: Domain to retrieve the translated text.

Returns

  • boolean: Whether the translation exists or not.

isRTL

Check if current locale is RTL.

RTL (Right To Left) is a locale property indicating that text is written from right to left. For example, the he locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages, including English (en, en-US, en-GB, etc.), Spanish (es), and French (fr).

Returns

  • boolean: Whether locale is RTL.

resetLocaleData

Resets all current Tannin instance locale data and sets the specified locale data for the domain. Accepts data in a Jed-formatted JSON object shape.

Related

Parameters

  • data [LocaleData]: Locale data configuration.
  • domain [string]: Domain for which configuration applies.

setLocaleData

Merges locale data into the Tannin instance by domain. Accepts data in a Jed-formatted JSON object shape.

Related

Parameters

  • data [LocaleData]: Locale data configuration.
  • domain [string]: Domain for which configuration applies.

sprintf

Returns a formatted string. If an error occurs in applying the format, the original format string is returned.

Related

Parameters

  • format string: The format of the string to generate.
  • args ...*: Arguments to apply to the format.

Returns

  • string: The formatted string.

subscribe

Subscribes to changes of locale data

Parameters

  • callback SubscribeCallback: Subscription callback

Returns

  • UnsubscribeCallback: Unsubscribe callback

_n

Translates and retrieves the singular or plural form based on the supplied number.

Related

Parameters

  • single string: The text to be used if the number is singular.
  • plural string: The text to be used if the number is plural.
  • number number: The number to compare against to use either the singular or plural form.
  • domain [string]: Domain to retrieve the translated text.

Returns

  • string: The translated singular or plural form.

_nx

Translates and retrieves the singular or plural form based on the supplied number, with gettext context.

Related

Parameters

  • single string: The text to be used if the number is singular.
  • plural string: The text to be used if the number is plural.
  • number number: The number to compare against to use either the singular or plural form.
  • context string: Context information for the translators.
  • domain [string]: Domain to retrieve the translated text.

Returns

  • string: The translated singular or plural form.

_x

Retrieve translated string with gettext context.

Related

Parameters

  • text string: Text to translate.
  • context string: Context information for the translators.
  • domain [string]: Domain to retrieve the translated text.

Returns

  • string: Translated context string without pipe.

__

Retrieve the translation of text.

Related

Parameters

  • text string: Text to translate.
  • domain [string]: Domain to retrieve the translated text.

Returns

  • string: Translated text.

Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project's main contributor guide.

gutenberg-block-componentsguten-block-package@beaverbuilder/vendors@wpshopify/componentscodewing-ui@infinitebrahmanuniverse/nolb-_wor@everything-registry/sub-chunk-1021@babyandy0111/block-editor@bernskioldmedia/wp-editor-components@bigwing/components@automattic/i18n-utils@automattic/isolated-block-editor@automattic/jetpack-ai-client@automattic/jetpack-blocks@automattic/jetpack-boost-score-api@automattic/jetpack-components@automattic/jetpack-connection@automattic/jetpack-shared-extension-utils@automattic/social-previews@automattic/tour-kit@automattic/composite-checkout@lifterlms/components@kaiseki/gutenberg-components@inusha1234/edit-site@iris.interactive/wp-etourisme@irvingjs/styled-components@jonathansadowski/wpc-test@media-manager/block-editor-complements@media-manager/components@media-manager/format-library@types/wordpress__i18n@sixa/wp-block-components@sixa/wp-block-utils@sixa/wp-react-hooks@shopwp/utils@wordpress/a11y@wordpress/annotations@wordpress/api-fetch@wordpress/block-directory@wordpress/block-editor@wordpress/block-library@wordpress/core-commands@wordpress/core-data@wordpress/commands@wordpress/components@wordpress/blocks@wordpress/customize-widgets@wordpress/dataviews@wordpress/keycodes@wordpress/edit-post@wordpress/edit-widgets@wordpress/editor@wordpress/format-library@wordpress/interface@wordpress/edit-site@woocommerce/components@woocommerce/currency@woocommerce/customer-effort-score@woocommerce/data@woocommerce/date@woocommerce/experimental@woocommerce/expression-evaluation@woocommerce/onboarding@woocommerce/product-editor@wordpress/widgets@wordpress/rich-text@wordpress/server-side-render@wordpress/nux@wordpress/patterns@wordpress/preferences@wordpress/list-reusable-blocks@wordpress/media-utils@wordpress/react-i18n@wordpress/reusable-blocks@yoast/algolia-search-box@yoast/analysis-report@yoast/components@yoast/configuration-wizard@yoast/helpers@yoast/replacement-variable-editor@yoast/schema-blocks@yoast/search-metadata-previews@yoast/social-metadata-forms@yoast/social-metadata-previews@technote-space/gutenberg-test-helper@technote-space/gutenberg-test-utils@xwp/block-helperswordpress-icon-picker@muv/inspectortools@mypreview/unicorn-js-upsell@mypreview/unicorn-js-utils@mypreview/unicorn-react-components@mypreview/unicorn-react-hooks@quillforms/form-integrations@surecart/components@superhuit/starterpack-customizer@superhuit/wordpress-components@rtcamp/gb-components@frontkom/gutenberg-js@elementor/editor
4.53.0

13 days ago

4.52.0

26 days ago

4.51.1

28 days ago

4.51.0

1 month ago

4.50.0

2 months ago

4.49.0

2 months ago

4.48.0

3 months ago

4.37.0

9 months ago

4.40.1

7 months ago

4.40.0

7 months ago

4.44.0

5 months ago

4.26.2

5 months ago

4.43.0

6 months ago

4.47.0

4 months ago

4.39.0

7 months ago

4.35.2

5 months ago

4.42.13

4 months ago

4.42.1

6 months ago

4.42.0

6 months ago

4.42.5

5 months ago

4.46.0

4 months ago

4.42.10

5 months ago

4.42.4

5 months ago

4.42.11

5 months ago

4.42.3

6 months ago

4.42.12

4 months ago

4.42.2

6 months ago

4.38.0

8 months ago

4.41.0

7 months ago

4.45.0

5 months ago

4.42.9

5 months ago

4.42.8

5 months ago

4.42.7

5 months ago

4.42.6

5 months ago

4.33.0

10 months ago

4.36.0

9 months ago

4.35.1

9 months ago

4.35.0

10 months ago

4.34.0

10 months ago

4.29.0

1 year ago

4.32.0

11 months ago

4.28.0

1 year ago

4.31.0

11 months ago

4.30.0

12 months ago

4.16.0

2 years ago

4.18.0

1 year ago

4.9.0

2 years ago

4.23.0

1 year ago

4.25.0

1 year ago

4.21.0

1 year ago

4.26.0

1 year ago

4.26.1

1 year ago

4.4.1

2 years ago

4.4.0

2 years ago

4.6.1

2 years ago

4.6.0

2 years ago

4.12.0

2 years ago

4.14.0

2 years ago

4.10.0

2 years ago

4.19.0

1 year ago

4.15.0

2 years ago

4.17.0

2 years ago

4.17.1

1 year ago

4.8.0

2 years ago

4.22.0

1 year ago

4.24.0

1 year ago

4.20.0

1 year ago

4.27.0

1 year ago

4.5.0

2 years ago

4.7.0

2 years ago

4.11.0

2 years ago

4.13.0

2 years ago

4.2.4

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.3

2 years ago

4.2.2

3 years ago

4.1.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

3.20.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.19.2

3 years ago

3.19.1

3 years ago

3.19.0

3 years ago

3.17.1-next.1

3 years ago

3.18.0

3 years ago

3.17.2

3 years ago

3.17.1

3 years ago

3.17.1-next.0

3 years ago

3.17.0

3 years ago

3.16.0

3 years ago

3.15.0

4 years ago

3.14.0

4 years ago

3.13.1-rc.0

4 years ago

3.13.0

4 years ago

3.12.0

4 years ago

3.11.0

4 years ago

3.10.0

4 years ago

3.9.0

4 years ago

3.8.0

4 years ago

3.7.0

4 years ago

3.6.1

4 years ago

3.6.0

5 years ago

3.5.0

5 years ago

3.4.0

5 years ago

3.3.0

5 years ago

3.2.0

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago