@aarondewes/wp-i18n 中文文档教程

发布于 3年前 浏览 32 项目主页 更新于 3年前

Internationalization (i18n)

用于客户端本地化的国际化实用程序。 有关服务器端文档,请参阅如何国际化您的插件

Installation

安装模块:

npm install @aarondewes/wp-i18n --save

此包假定您的代码将在 ES2015+ 环境中运行。 如果您使用的环境对 ES2015+ 的支持有限或不支持,例如 IE 浏览器,那么使用 core-js 将添加这些方法的 polyfill。

Usage

import { sprintf, _n } from '@aarondewes/wp-i18n';

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

有关完整示例,请参阅块编辑器手册的国际化部分。

API

# createI18n

创建一个 i18n 实例

参数

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

返回

  • I18n: I18n instance

# defaultI18n

I18n 的默认单例实例。

# getLocaleData

以 Jed 格式的 JSON 对象形状按域返回区域设置数据。

相关

参数

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

返回

  • LocaleData: Locale data.

# hasTranslation

检查给定字符串是否有翻译(单数形式)。

参数

  • 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.

返回

  • boolean: Whether the translation exists or not.

# isRTL

检查当前语言环境是否为 RTL。

RTL(从右到左) 是一个语言环境属性,表示文本是从右到左书写的。 例如,he 语言环境(希伯来语)指定从右到左。 阿拉伯语 (ar) 是另一种常见的 语言书面RTL。 与RTL相反,LTR(Left To Right)在其他语言中使用, 包括英语(enen-USen-GB等)、西班牙语(es)、和法语 (fr)。

返回

  • boolean: Whether locale is RTL.

# resetLocaleData

重置所有当前的 Tannin 实例区域设置数据并设置指定的 域的语言环境数据。 接受 Jed 格式的 JSON 对象形状的数据。

相关

参数

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

# setLocaleData

将语言环境数据合并到 Tannin 实例中按域。 接受数据 Jed 格式的 JSON 对象形状。

相关

参数

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

# sprintf

返回格式化字符串。 如果在应用格式时发生错误,则 返回原始格式字符串。

相关

参数

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

返回值

  • string: The formatted string.

# 订阅

订阅语言环境数据的变化

参数

  • callback SubscribeCallback: Subscription callback

返回

  • UnsubscribeCallback: Unsubscribe callback

# _n

根据所提供的翻译和检索单数或复数形式 数字。

相关

参数

  • 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.

返回值

  • string: The translated singular or plural form.

# _nx

根据所提供的翻译和检索单数或复数形式 数字,带有 gettext 上下文。

相关

参数

  • 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.

返回

  • string: The translated singular or plural form.

# _x

使用 gettext 上下文检索翻译后的字符串。

相关

参数

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

返回值

  • string: Translated context string without pipe.

# __

检索文本的翻译。

相关

参数

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

返回值

  • string: Translated text.



代码就是诗歌。

Internationalization (i18n)

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

Installation

Install the module:

npm install @aarondewes/wp-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 ES2015+ such as IE browsers then using core-js will add polyfills for these methods.

Usage

import { sprintf, _n } from '@aarondewes/wp-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.



Code is Poetry.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文