[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> l10n.php (summary)

Core Translation API

File Size: 1787 lines (59 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 0 files

Defines 47 functions

  get_locale()
  get_user_locale()
  determine_locale()
  translate()
  before_last_bar()
  translate_with_gettext_context()
  __()
  esc_attr__()
  esc_html__()
  _e()
  esc_attr_e()
  esc_html_e()
  _x()
  _ex()
  esc_attr_x()
  esc_html_x()
  _n()
  _nx()
  _n_noop()
  _nx_noop()
  translate_nooped_plural()
  load_textdomain()
  unload_textdomain()
  load_default_textdomain()
  load_plugin_textdomain()
  load_muplugin_textdomain()
  load_theme_textdomain()
  load_child_theme_textdomain()
  load_script_textdomain()
  load_script_translations()
  _load_textdomain_just_in_time()
  _get_path_to_translation()
  _get_path_to_translation_from_lang_dir()
  get_translations_for_domain()
  is_textdomain_loaded()
  translate_user_role()
  get_available_languages()
  wp_get_installed_translations()
  wp_get_pomo_file_data()
  wp_dropdown_languages()
  is_rtl()
  switch_to_locale()
  restore_previous_locale()
  restore_current_locale()
  is_locale_switched()
  translate_settings_using_i18n_schema()
  wp_get_list_item_separator()

Functions
Functions that are not part of a class:

get_locale()   X-Ref
Retrieves the current locale.

If the locale is set, then it will filter the locale in the {@see 'locale'}
filter hook and return the value.

If the locale is not set already, then the WPLANG constant is used if it is
defined. Then it is filtered through the {@see 'locale'} filter hook and
the value for the locale global set and the locale is returned.

The process to get the locale should only be done once, but the locale will
always be filtered using the {@see 'locale'} hook.

return: string The locale of the blog or from the {@see 'locale'} hook.
since: 1.5.0

get_user_locale( $user_id = 0 )   X-Ref
Retrieves the locale of a user.

If the user has a locale set to a non-empty string then it will be
returned. Otherwise it returns the locale of get_locale().

return: string The locale of the user.
since: 4.7.0
param: int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.

determine_locale()   X-Ref
Determine the current locale desired for the request.

return: string The determined locale.
since: 5.0.0

translate( $text, $domain = 'default' )   X-Ref
Retrieve the translation of $text.

If there is no translation, or the text domain isn't loaded, the original text is returned.

*Note:* Don't use translate() directly, use __() or related functions.

return: string Translated text.
since: 2.2.0
since: 5.5.0 Introduced gettext-{$domain} filter.
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

before_last_bar( $string )   X-Ref
Remove last item on a pipe-delimited string.

Meant for removing the last item in a string, such as 'Role name|User role'. The original
string will be returned if no pipe '|' characters are found in the string.

return: string Either $string or everything before the last pipe.
since: 2.8.0
param: string $string A pipe-delimited string.

translate_with_gettext_context( $text, $context, $domain = 'default' )   X-Ref
Retrieve the translation of $text in the context defined in $context.

If there is no translation, or the text domain isn't loaded, the original text is returned.

*Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.

return: string Translated text on success, original text on failure.
since: 2.8.0
since: 5.5.0 Introduced gettext_with_context-{$domain} filter.
param: string $text    Text to translate.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

__( $text, $domain = 'default' )   X-Ref
Retrieve the translation of $text.

If there is no translation, or the text domain isn't loaded, the original text is returned.

return: string Translated text.
since: 2.1.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

esc_attr__( $text, $domain = 'default' )   X-Ref
Retrieve the translation of $text and escapes it for safe use in an attribute.

If there is no translation, or the text domain isn't loaded, the original text is returned.

return: string Translated text on success, original text on failure.
since: 2.8.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

esc_html__( $text, $domain = 'default' )   X-Ref
Retrieve the translation of $text and escapes it for safe use in HTML output.

If there is no translation, or the text domain isn't loaded, the original text
is escaped and returned.

return: string Translated text.
since: 2.8.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

_e( $text, $domain = 'default' )   X-Ref
Display translated text.

since: 1.2.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

esc_attr_e( $text, $domain = 'default' )   X-Ref
Display translated text that has been escaped for safe use in an attribute.

Encodes `< > & " '` (less than, greater than, ampersand, double quote, single quote).
Will never double encode entities.

If you need the value for use in PHP, use esc_attr__().

since: 2.8.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

esc_html_e( $text, $domain = 'default' )   X-Ref
Display translated text that has been escaped for safe use in HTML output.

If there is no translation, or the text domain isn't loaded, the original text
is escaped and displayed.

If you need the value for use in PHP, use esc_html__().

since: 2.8.0
param: string $text   Text to translate.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

_x( $text, $context, $domain = 'default' )   X-Ref
Retrieve translated string with gettext context.

Quite a few times, there will be collisions with similar translatable text
found in more than two places, but with different translated context.

By including the context in the pot file, translators can translate the two
strings differently.

return: string Translated context string without pipe.
since: 2.8.0
param: string $text    Text to translate.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

_ex( $text, $context, $domain = 'default' )   X-Ref
Display translated string with gettext context.

since: 3.0.0
param: string $text    Text to translate.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

esc_attr_x( $text, $context, $domain = 'default' )   X-Ref
Translate string with gettext context, and escapes it for safe use in an attribute.

If there is no translation, or the text domain isn't loaded, the original text
is escaped and returned.

return: string Translated text.
since: 2.8.0
param: string $text    Text to translate.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

esc_html_x( $text, $context, $domain = 'default' )   X-Ref
Translate string with gettext context, and escapes it for safe use in HTML output.

If there is no translation, or the text domain isn't loaded, the original text
is escaped and returned.

return: string Translated text.
since: 2.9.0
param: string $text    Text to translate.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

_n( $single, $plural, $number, $domain = 'default' )   X-Ref
Translates and retrieves the singular or plural form based on the supplied number.

Used when you want to use the appropriate form of a string based on whether a
number is singular or plural.

Example:

printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );

return: string The translated singular or plural form.
since: 2.8.0
since: 5.5.0 Introduced ngettext-{$domain} filter.
param: string $single The text to be used if the number is singular.
param: string $plural The text to be used if the number is plural.
param: int    $number The number to compare against to use either the singular or plural form.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

_nx( $single, $plural, $number, $context, $domain = 'default' )   X-Ref
Translates and retrieves the singular or plural form based on the supplied number, with gettext context.

This is a hybrid of _n() and _x(). It supports context and plurals.

Used when you want to use the appropriate form of a string with context based on whether a
number is singular or plural.

Example of a generic phrase which is disambiguated via the context parameter:

printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );

return: string The translated singular or plural form.
since: 2.8.0
since: 5.5.0 Introduced ngettext_with_context-{$domain} filter.
param: string $single  The text to be used if the number is singular.
param: string $plural  The text to be used if the number is plural.
param: int    $number  The number to compare against to use either the singular or plural form.
param: string $context Context information for the translators.
param: string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.

_n_noop( $singular, $plural, $domain = null )   X-Ref
Registers plural strings in POT file, but does not translate them.

Used when you want to keep structures with translatable plural
strings and use them later when the number is known.

Example:

$message = _n_noop( '%s post', '%s posts', 'text-domain' );
...
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );

return: array {
since: 2.5.0
param: string $singular Singular form to be localized.
param: string $plural   Plural form to be localized.
param: string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.

_nx_noop( $singular, $plural, $context, $domain = null )   X-Ref
Registers plural strings with gettext context in POT file, but does not translate them.

Used when you want to keep structures with translatable plural
strings and use them later when the number is known.

Example of a generic phrase which is disambiguated via the context parameter:

$messages = array(
'people'  => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
);
...
$message = $messages[ $type ];
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );

return: array {
since: 2.8.0
param: string $singular Singular form to be localized.
param: string $plural   Plural form to be localized.
param: string $context  Context information for the translators.
param: string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.

translate_nooped_plural( $nooped_plural, $count, $domain = 'default' )   X-Ref
Translates and retrieves the singular or plural form of a string that's been registered
with _n_noop() or _nx_noop().

Used when you want to use a translatable plural string once the number is known.

Example:

$message = _n_noop( '%s post', '%s posts', 'text-domain' );
...
printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );

return: string Either $single or $plural translated text.
since: 3.1.0
param: array  $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
param: int    $count         Number of objects.
param: string $domain        Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains

load_textdomain( $domain, $mofile )   X-Ref
Load a .mo file into the text domain $domain.

If the text domain already exists, the translations will be merged. If both
sets have the same string, the translation from the original value will be taken.

On success, the .mo file will be placed in the $l10n global by $domain
and will be a MO object.

return: bool True on success, false on failure.
since: 1.5.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.
param: string $mofile Path to the .mo file.

unload_textdomain( $domain )   X-Ref
Unload translations for a text domain.

return: bool Whether textdomain was unloaded.
since: 3.0.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.

load_default_textdomain( $locale = null )   X-Ref
Load default translated strings based on locale.

Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
The translated (.mo) file is named based on the locale.

return: bool Whether the textdomain was loaded.
since: 1.5.0
param: string $locale Optional. Locale to load. Default is the value of get_locale().

load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false )   X-Ref
Loads a plugin's translated strings.

If the path is not given then it will be the root of the plugin directory.

The .mo file should be named based on the text domain with a dash, and then the locale exactly.

return: bool True when textdomain is successfully loaded, false otherwise.
since: 1.5.0
since: 4.6.0 The function now tries to load the .mo file from the languages directory first.
param: string       $domain          Unique identifier for retrieving translated strings
param: string|false $deprecated      Optional. Deprecated. Use the $plugin_rel_path parameter instead.
param: string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.

load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' )   X-Ref
Load the translated strings for a plugin residing in the mu-plugins directory.

return: bool True when textdomain is successfully loaded, false otherwise.
since: 3.0.0
since: 4.6.0 The function now tries to load the .mo file from the languages directory first.
param: string $domain             Text domain. Unique identifier for retrieving translated strings.
param: string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo

load_theme_textdomain( $domain, $path = false )   X-Ref
Load the theme's translated strings.

If the current locale exists as a .mo file in the theme's root directory, it
will be included in the translated strings by the $domain.

The .mo files must be named based on the locale exactly.

return: bool True when textdomain is successfully loaded, false otherwise.
since: 1.5.0
since: 4.6.0 The function now tries to load the .mo file from the languages directory first.
param: string       $domain Text domain. Unique identifier for retrieving translated strings.
param: string|false $path   Optional. Path to the directory containing the .mo file.

load_child_theme_textdomain( $domain, $path = false )   X-Ref
Load the child themes translated strings.

If the current locale exists as a .mo file in the child themes
root directory, it will be included in the translated strings by the $domain.

The .mo files must be named based on the locale exactly.

return: bool True when the theme textdomain is successfully loaded, false otherwise.
since: 2.9.0
param: string       $domain Text domain. Unique identifier for retrieving translated strings.
param: string|false $path   Optional. Path to the directory containing the .mo file.

load_script_textdomain( $handle, $domain = 'default', $path = null )   X-Ref
Loads the script translated strings.

return: string|false The translated strings in JSON encoding on success,
since: 5.0.0
since: 5.0.2 Uses load_script_translations() to load translation data.
since: 5.1.0 The `$domain` parameter was made optional.
param: string $handle Name of the script to register a translation domain to.
param: string $domain Optional. Text domain. Default 'default'.
param: string $path   Optional. The full file path to the directory containing translation files.

load_script_translations( $file, $handle, $domain )   X-Ref
Loads the translation data for the given script handle and text domain.

return: string|false The JSON-encoded translated strings for the given script handle and text domain.
since: 5.0.2
param: string|false $file   Path to the translation file to load. False if there isn't one.
param: string       $handle Name of the script to register a translation domain to.
param: string       $domain The text domain.

_load_textdomain_just_in_time( $domain )   X-Ref
Loads plugin and theme textdomains just-in-time.

When a textdomain is encountered for the first time, we try to load
the translation file from `wp-content/languages`, removing the need
to call load_plugin_texdomain() or load_theme_texdomain().

return: bool True when the textdomain is successfully loaded, false otherwise.
since: 4.6.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.

_get_path_to_translation( $domain, $reset = false )   X-Ref
Gets the path to a translation file for loading a textdomain just in time.

Caches the retrieved results internally.

return: string|false The path to the translation file or false if no translation file was found.
since: 4.7.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.
param: bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.

_get_path_to_translation_from_lang_dir( $domain )   X-Ref
Gets the path to a translation file in the languages directory for the current locale.

Holds a cached list of available .mo files to improve performance.

return: string|false The path to the translation file or false if no translation file was found.
since: 4.7.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.

get_translations_for_domain( $domain )   X-Ref
Return the Translations instance for a text domain.

If there isn't one, returns empty Translations instance.

return: Translations|NOOP_Translations A Translations instance.
since: 2.8.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.

is_textdomain_loaded( $domain )   X-Ref
Whether there are translations for the text domain.

return: bool Whether there are translations.
since: 3.0.0
param: string $domain Text domain. Unique identifier for retrieving translated strings.

translate_user_role( $name, $domain = 'default' )   X-Ref
Translates role name.

Since the role names are in the database and not in the source there
are dummy gettext calls to get them into the POT file and this function
properly translates them back.

The before_last_bar() call is needed, because older installations keep the roles
using the old context format: 'Role name|User role' and just skipping the
content after the last bar is easier than fixing them in the DB. New installations
won't suffer from that problem.

return: string Translated role name on success, original name on failure.
since: 2.8.0
since: 5.2.0 Added the `$domain` parameter.
param: string $name   The role name.
param: string $domain Optional. Text domain. Unique identifier for retrieving translated strings.

get_available_languages( $dir = null )   X-Ref
Get all available languages based on the presence of *.mo files in a given directory.

The default directory is WP_LANG_DIR.

return: string[] An array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names.
since: 3.0.0
since: 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
param: string $dir A directory to search for language files.

wp_get_installed_translations( $type )   X-Ref
Get installed translations.

Looks in the wp-content/languages directory for translations of
plugins or themes.

return: array Array of language data.
since: 3.7.0
param: string $type What to search for. Accepts 'plugins', 'themes', 'core'.

wp_get_pomo_file_data( $po_file )   X-Ref
Extract headers from a PO file.

return: string[] Array of PO file header values keyed by header name.
since: 3.7.0
param: string $po_file Path to PO file.

wp_dropdown_languages( $args = array()   X-Ref
Language selector.

return: string HTML dropdown list of languages.
since: 4.0.0
since: 4.3.0 Introduced the `echo` argument.
since: 4.7.0 Introduced the `show_option_site_default` argument.
since: 5.1.0 Introduced the `show_option_en_us` argument.
since: 5.9.0 Introduced the `explicit_option_en_us` argument.
param: string|array $args {

is_rtl()   X-Ref
Determines whether the current locale is right-to-left (RTL).

For more information on this and similar theme functions, check out
the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
Conditional Tags} article in the Theme Developer Handbook.

return: bool Whether locale is RTL.
since: 3.0.0

switch_to_locale( $locale )   X-Ref
Switches the translations according to the given locale.

return: bool True on success, false on failure.
since: 4.7.0
param: string $locale The locale.

restore_previous_locale()   X-Ref
Restores the translations according to the previous locale.

return: string|false Locale on success, false on error.
since: 4.7.0

restore_current_locale()   X-Ref
Restores the translations according to the original locale.

return: string|false Locale on success, false on error.
since: 4.7.0

is_locale_switched()   X-Ref
Whether switch_to_locale() is in effect.

return: bool True if the locale has been switched, false otherwise.
since: 4.7.0

translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain )   X-Ref
Translates the provided settings value using its i18n schema.

return: string|string[]|array[] Translated settings.
since: 5.9.0
param: string|string[]|array[]|object $i18n_schema I18n schema for the setting.
param: string|string[]|array[]        $settings    Value for the settings.
param: string                         $textdomain  Textdomain to use with translations.

wp_get_list_item_separator()   X-Ref
Retrieves the list item separator based on the locale.

return: string Locale-specific list item separator.
since: 6.0.0



Generated: Wed Apr 24 01:00:03 2024 Cross-referenced by PHPXref 0.7.1