main ] instanceof NOOP_Translations; } /** * 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. * * @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. * Default 'default'. * @return string Translated role name on success, original name on failure. */ function translate_user_role( $name, $domain = 'default' ) { return translate_with_gettext_context( before_last_bar( $name ), 'User role', $domain ); } /** * Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory. * * The default directory is WP_LANG_DIR. * * @since 3.0.0 * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter. * @since 6.5.0 The initial file list is now cached and also takes into account *.l10n.php files. * * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. * * @param string $dir A directory to search for language files. * Default 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 file extension from the language file names. */ function get_available_languages( $dir = null ) { global $wp_textdomain_registry; $languages = array(); $path = is_null( $dir ) ? WP_LANG_DIR : $dir; $lang_files = $wp_textdomain_registry->get_language_files_from_path( $path ); if ( $lang_files ) { foreach ( $lang_files as $lang_file ) { $lang_file = basename( $lang_file, '.mo' ); $lang_file = basename( $lang_file, '.l10n.php' ); if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) && ! str_starts_with( $lang_file, 'admin-' ) ) { $languages[] = $lang_file; } } } /** * Filters the list of available language codes. * * @since 4.7.0 * * @param string[] $languages An array of available language codes. * @param string $dir The directory where the language files were found. */ return apply_filters( 'get_available_languages', array_unique( $languages ), $dir ); } /** * Gets installed translations. * * Looks in the wp-content/languages directory for translations of * plugins or themes. * * @since 3.7.0 * * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. * * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'. * @return array Array of language data. */ function wp_get_installed_translations( $type ) { global $wp_textdomain_registry; if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) { return array(); } $dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . "/$type"; if ( ! is_dir( $dir ) ) { return array(); } $files = $wp_textdomain_registry->get_language_files_from_path( $dir ); if ( ! $files ) { return array(); } $language_data = array(); foreach ( $files as $file ) { if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/', basename( $file ), $match ) ) { continue; } list( , $textdomain, $language ) = $match; if ( '' === $textdomain ) { $textdomain = 'default'; } if ( str_ends_with( $file, '.mo' ) ) { $pofile = substr_replace( $file, '.po', - strlen( '.mo' ) ); if ( ! file_exists( $pofile ) ) { continue; } $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( $pofile ); } else { $pofile = substr_replace( $file, '.po', - strlen( '.l10n.php' ) ); // If both a PO and a PHP file exist, prefer the PO file. if ( file_exists( $pofile ) ) { continue; } $language_data[ $textdomain ][ $language ] = wp_get_l10n_php_file_data( $file ); } } return $language_data; } /** * Extracts headers from a PO file. * * @since 3.7.0 * * @param string $po_file Path to PO file. * @return string[] Array of PO file header values keyed by header name. */ function wp_get_pomo_file_data( $po_file ) { $headers = get_file_data( $po_file, array( 'POT-Creation-Date' => '"POT-Creation-Date', 'PO-Revision-Date' => '"PO-Revision-Date', 'Project-Id-Version' => '"Project-Id-Version', 'X-Generator' => '"X-Generator', ) ); foreach ( $headers as $header => $value ) { // Remove possible contextual '\n' and closing double quote. $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value ); } return $headers; } /** * Extracts headers from a PHP translation file. * * @since 6.6.0 * * @param string $php_file Path to a `.l10n.php` file. * @return string[] Array of file header values keyed by header name. */ function wp_get_l10n_php_file_data( $php_file ) { $data = (array) include $php_file; unset( $data['messages'] ); $headers = array( 'POT-Creation-Date' => 'pot-creation-date', 'PO-Revision-Date' => 'po-revision-date', 'Project-Id-Version' => 'project-id-version', 'X-Generator' => 'x-generator', ); $result = array( 'POT-Creation-Date' => '', 'PO-Revision-Date' => '', 'Project-Id-Version' => '', 'X-Generator' => '', ); foreach ( $headers as $po_header => $php_header ) { if ( isset( $data[ $php_header ] ) ) { $result[ $po_header ] = $data[ $php_header ]; } } return $result; } /** * Displays or returns a Language selector. * * @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. * * @see get_available_languages() * @see wp_get_available_translations() * * @param string|array $args { * Optional. Array or string of arguments for outputting the language selector. * * @type string $id ID attribute of the select element. Default 'locale'. * @type string $name Name attribute of the select element. Default 'locale'. * @type string[] $languages List of installed languages, contain only the locales. * Default empty array. * @type array $translations List of available translations. Default result of * wp_get_available_translations(). * @type string $selected Language which should be selected. Default empty. * @type bool|int $echo Whether to echo the generated markup. Accepts 0, 1, or their * boolean equivalents. Default 1. * @type bool $show_available_translations Whether to show available translations. Default true. * @type bool $show_option_site_default Whether to show an option to fall back to the site's locale. Default false. * @type bool $show_option_en_us Whether to show an option for English (United States). Default true. * @type bool $explicit_option_en_us Whether the English (United States) option uses an explicit value of en_US * instead of an empty value. Default false. * } * @return string HTML dropdown list of languages. */ function wp_dropdown_languages( $args = array() ) { $parsed_args = wp_parse_args( $args, array( 'id' => 'locale', 'name' => 'locale', 'languages' => array(), 'translations' => array(), 'selected' => '', 'echo' => 1, 'show_available_translations' => true, 'show_option_site_default' => false, 'show_option_en_us' => true, 'explicit_option_en_us' => false, ) ); // Bail if no ID or no name. if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) { return; } // English (United States) uses an empty string for the value attribute. if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) { $parsed_args['selected'] = ''; } $translations = $parsed_args['translations']; if ( empty( $translations ) ) { require_once ABSPATH . 'wp-admin/includes/translation-install.php'; $translations = wp_get_available_translations(); } /* * $parsed_args['languages'] should only contain the locales. Find the locale in * $translations to get the native name. Fall back to locale. */ $languages = array(); foreach ( $parsed_args['languages'] as $locale ) { if ( isset( $translations[ $locale ] ) ) { $translation = $translations[ $locale ]; $languages[] = array( 'language' => $translation['language'], 'native_name' => $translation['native_name'], 'lang' => current( $translation['iso'] ), ); // Remove installed language from available translations. unset( $translations[ $locale ] ); } else { $languages[] = array( 'language' => $locale, 'native_name' => $locale, 'lang' => '', ); } } $translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] ); // Holds the HTML markup. $structure = array(); // List installed languages. if ( $translations_available ) { $structure[] = ''; } // Site default. if ( $parsed_args['show_option_site_default'] ) { $structure[] = sprintf( '', selected( 'site-default', $parsed_args['selected'], false ), _x( 'Site Default', 'default site language' ) ); } if ( $parsed_args['show_option_en_us'] ) { $value = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : ''; $structure[] = sprintf( '', esc_attr( $value ), selected( '', $parsed_args['selected'], false ) ); } // List installed languages. foreach ( $languages as $language ) { $structure[] = sprintf( '', esc_attr( $language['language'] ), esc_attr( $language['lang'] ), selected( $language['language'], $parsed_args['selected'], false ), esc_html( $language['native_name'] ) ); } if ( $translations_available ) { $structure[] = ''; } // List available translations. if ( $translations_available ) { $structure[] = ''; foreach ( $translations as $translation ) { $structure[] = sprintf( '', esc_attr( $translation['language'] ), esc_attr( current( $translation['iso'] ) ), selected( $translation['language'], $parsed_args['selected'], false ), esc_html( $translation['native_name'] ) ); } $structure[] = ''; } // Combine the output string. $output = sprintf( ''; if ( $parsed_args['echo'] ) { echo $output; } return $output; } /** * 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. * * @since 3.0.0 * * @global WP_Locale $wp_locale WordPress date and time locale object. * * @return bool Whether locale is RTL. */ function is_rtl() { global $wp_locale; if ( ! ( $wp_locale instanceof WP_Locale ) ) { return false; } return $wp_locale->is_rtl(); } /** * Switches the translations according to the given locale. * * @since 4.7.0 * * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. * * @param string $locale The locale. * @return bool True on success, false on failure. */ function switch_to_locale( $locale ) { /* @var WP_Locale_Switcher $wp_locale_switcher */ global $wp_locale_switcher; if ( ! $wp_locale_switcher ) { return false; } return $wp_locale_switcher->switch_to_locale( $locale ); } /** * Switches the translations according to the given user's locale. * * @since 6.2.0 * * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. * * @param int $user_id User ID. * @return bool True on success, false on failure. */ function switch_to_user_locale( $user_id ) { /* @var WP_Locale_Switcher $wp_locale_switcher */ global $wp_locale_switcher; if ( ! $wp_locale_switcher ) { return false; } return $wp_locale_switcher->switch_to_user_locale( $user_id ); } /** * Restores the translations according to the previous locale. * * @since 4.7.0 * * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. * * @return string|false Locale on success, false on error. */ function restore_previous_locale() { /* @var WP_Locale_Switcher $wp_locale_switcher */ global $wp_locale_switcher; if ( ! $wp_locale_switcher ) { return false; } return $wp_locale_switcher->restore_previous_locale(); } /** * Restores the translations according to the original locale. * * @since 4.7.0 * * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. * * @return string|false Locale on success, false on error. */ function restore_current_locale() { /* @var WP_Locale_Switcher $wp_locale_switcher */ global $wp_locale_switcher; if ( ! $wp_locale_switcher ) { return false; } return $wp_locale_switcher->restore_current_locale(); } /** * Determines whether switch_to_locale() is in effect. * * @since 4.7.0 * * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object. * * @return bool True if the locale has been switched, false otherwise. */ function is_locale_switched() { /* @var WP_Locale_Switcher $wp_locale_switcher */ global $wp_locale_switcher; return $wp_locale_switcher->is_switched(); } /** * Translates the provided settings value using its i18n schema. * * @since 5.9.0 * @access private * * @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. * * @return string|string[]|array[] Translated settings. */ function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) { if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) { return $settings; } if ( is_string( $i18n_schema ) && is_string( $settings ) ) { return translate_with_gettext_context( $settings, $i18n_schema, $textdomain ); } if ( is_array( $i18n_schema ) && is_array( $settings ) ) { $translated_settings = array(); foreach ( $settings as $value ) { $translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain ); } return $translated_settings; } if ( is_object( $i18n_schema ) && is_array( $settings ) ) { $group_key = '*'; $translated_settings = array(); foreach ( $settings as $key => $value ) { if ( isset( $i18n_schema->$key ) ) { $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain ); } elseif ( isset( $i18n_schema->$group_key ) ) { $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain ); } else { $translated_settings[ $key ] = $value; } } return $translated_settings; } return $settings; } /** * Retrieves the list item separator based on the locale. * * @since 6.0.0 * * @global WP_Locale $wp_locale WordPress date and time locale object. * * @return string Locale-specific list item separator. */ function wp_get_list_item_separator() { global $wp_locale; if ( ! ( $wp_locale instanceof WP_Locale ) ) { // Default value of WP_Locale::get_list_item_separator(). /* translators: Used between list items, there is a space after the comma. */ return __( ', ' ); } return $wp_locale->get_list_item_separator(); } /** * Retrieves the word count type based on the locale. * * @since 6.2.0 * * @global WP_Locale $wp_locale WordPress date and time locale object. * * @return string Locale-specific word count type. Possible values are `characters_excluding_spaces`, * `characters_including_spaces`, or `words`. Defaults to `words`. */ function wp_get_word_count_type() { global $wp_locale; if ( ! ( $wp_locale instanceof WP_Locale ) ) { // Default value of WP_Locale::get_word_count_type(). return 'words'; } return $wp_locale->get_word_count_type(); }