get_translations_for_domain()WP 2.8.0

Возвращает объект перевода для указанного домена перевода (textdomain).

Основа для: translate()
1 раз — 0.000001 сек (скорость света) | 50000 раз — 0.43 сек (очень быстро) | PHP 7.2.16, WP 5.1.1

Хуков нет.

Возвращает

Translations|NOOP_Translations.

Если для указанного домена перевода есть .mo файла перевода (перевод загружен), то вернет объект класса MO.

MO Object
(
	[_nplurals] => 3
	[filename:MO:private] => F:\server\sites\wp-test.ru/wp-content/languages/plugins/classic-editor-ru_RU.mo
	[entries] => Array()
	[headers] => Array()
)

Если для указанного домена перевода нет .mo файла перевода (перевод не загружен), то вернет объект класса NOOP_Translations:

NOOP_Translations Object (
	[entries] => Array()
	[headers] => Array()
)

Использование

get_translations_for_domain( $domain );
$domain(строка) (обязательный)
Домен перевода (текстовый домен). Уникальный идентификатор для получения переведенных строк.

Примеры

0

#1 Получим данные о переводах для плагина Classic Editor

У плагина Classic Editor текстовый домен classic-editor. Получим данные по нему:

$data = get_translations_for_domain( 'classic-editor' );

print_r( $data )

Результат:

GitHub

MO Object
(
    [_nplurals] => 3
    [filename:MO:private] => F:\server\sites\wp-test.ru/wp-content/languages/plugins/classic-editor-ru_RU.mo
    [entries] => Array
        (
            [Allow site admins to change settings] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Allow site admins to change settings
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Разрешить администраторам сайта изменять настройки
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Allow users to switch editors] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Allow users to switch editors
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Разрешить пользователям переключение редакторов
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [By default the Block Editor is replaced with the Classic Editor and users cannot switch editors.] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => By default the Block Editor is replaced with the Classic Editor and users cannot switch editors.
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => По умолчанию редактор блоков будет заменен на классический редактор и пользователи не смогут выбирать редактор.
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Change settings] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Change settings
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Изменить настройки
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Change the %1$sClassic Editor settings%2$s.] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Change the %1$sClassic Editor settings%2$s.
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Изменить настройки %1$sClassic Editor%2$s.
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Classic Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Classic Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Classic Editor
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Default Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Default Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактор по умолчанию
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Default editor for all sites] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Default editor for all sites
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактор по умолчанию для всех сайтов
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Default editor for all users] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Default editor for all users
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактор по умолчанию для всех пользователей
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Edit “%s” in the Block Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Edit “%s” in the Block Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактировать “%s” в редакторе блоков
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Edit “%s” in the Classic Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Edit “%s” in the Classic Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактировать “%s” в классическом редакторе
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактор
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor NameBlock Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => Editor Name
                    [singular] => Block Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Редактор блоков
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor NameClassic Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => Editor Name
                    [singular] => Classic Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Классический редактор
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor NameEdit (Block Editor)] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => Editor Name
                    [singular] => Edit (Block Editor)
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Изменить (редактор блоков)
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor NameEdit (Classic Editor)] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => Editor Name
                    [singular] => Edit (Classic Editor)
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Изменить (классический редактор)
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Editor Settings] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Editor Settings
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Настройки редактора
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Enables the WordPress classic editor and the old-style Edit Post screen with TinyMCE, Meta Boxes, etc. Supports the older plugins that extend this screen.] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Enables the WordPress classic editor and the old-style Edit Post screen with TinyMCE, Meta Boxes, etc. Supports the older plugins that extend this screen.
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Включает классический редактор WordPress, старый вариант экрана редактирования записей (TinyMCE, мета поля и др.). Поддерживает другие плагины расширения старого редактора.
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [No] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => No
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Нет
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Settings] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Settings
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Настройки
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Switch to Block Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Switch to Block Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Переключиться на редактор блоков
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Switch to Classic Editor] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Switch to Classic Editor
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Переключиться на классический редактор
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [The Classic Editor plugin prevents use of the new Block Editor.] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => The Classic Editor plugin prevents use of the new Block Editor.
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Плагин Classic Editor предотвращает использование нового редактора блоков.
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [WordPress Contributors] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => WordPress Contributors
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Участники разработки WordPress
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [Yes] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => Yes
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => Да
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [https://github.com/WordPress/classic-editor/] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => https://github.com/WordPress/classic-editor/
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => https://github.com/WordPress/classic-editor/
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

            [https://wordpress.org/plugins/classic-editor/] => Translation_Entry Object
                (
                    [is_plural] => 
                    [context] => 
                    [singular] => https://wordpress.org/plugins/classic-editor/
                    [plural] => 
                    [translations] => Array
                        (
                            [0] => https://ru.wordpress.org/plugins/classic-editor/
                        )

                    [translator_comments] => 
                    [extracted_comments] => 
                    [references] => Array
                        (
                        )

                    [flags] => Array
                        (
                        )

                )

        )

    [headers] => Array
        (
            [PO-Revision-Date] => 2019-02-20 19:00:57+0000
            [MIME-Version] => 1.0
            [Content-Type] => text/plain; charset=UTF-8
            [Content-Transfer-Encoding] => 8bit
            [Plural-Forms] => nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);
            [X-Generator] => GlotPress/2.4.0-alpha
            [Language] => ru
            [Project-Id-Version] => Plugins - Classic Editor - Stable (latest release)
        )

    [_gettext_select_plural_form] => Array
        (
            [0] => Plural_Forms Object
                (
                    [tokens:protected] => Array
                        (
                            [0] => Array
                                (
                                    [0] => var
                                )

                            [1] => Array
                                (
                                    [0] => value
                                    [1] => 10
                                )

                            [2] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [3] => Array
                                (
                                    [0] => value
                                    [1] => 1
                                )

                            [4] => Array
                                (
                                    [0] => op
                                    [1] => ==
                                )

                            [5] => Array
                                (
                                    [0] => var
                                )

                            [6] => Array
                                (
                                    [0] => value
                                    [1] => 100
                                )

                            [7] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [8] => Array
                                (
                                    [0] => value
                                    [1] => 11
                                )

                            [9] => Array
                                (
                                    [0] => op
                                    [1] => !=
                                )

                            [10] => Array
                                (
                                    [0] => op
                                    [1] => &&
                                )

                            [11] => Array
                                (
                                    [0] => value
                                    [1] => 0
                                )

                            [12] => Array
                                (
                                    [0] => var
                                )

                            [13] => Array
                                (
                                    [0] => value
                                    [1] => 10
                                )

                            [14] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [15] => Array
                                (
                                    [0] => value
                                    [1] => 2
                                )

                            [16] => Array
                                (
                                    [0] => op
                                    [1] => >=
                                )

                            [17] => Array
                                (
                                    [0] => var
                                )

                            [18] => Array
                                (
                                    [0] => value
                                    [1] => 10
                                )

                            [19] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [20] => Array
                                (
                                    [0] => value
                                    [1] => 4
                                )

                            [21] => Array
                                (
                                    [0] => op
                                    [1] => <=
                                )

                            [22] => Array
                                (
                                    [0] => op
                                    [1] => &&
                                )

                            [23] => Array
                                (
                                    [0] => var
                                )

                            [24] => Array
                                (
                                    [0] => value
                                    [1] => 100
                                )

                            [25] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [26] => Array
                                (
                                    [0] => value
                                    [1] => 10
                                )

                            [27] => Array
                                (
                                    [0] => op
                                    [1] => <
                                )

                            [28] => Array
                                (
                                    [0] => var
                                )

                            [29] => Array
                                (
                                    [0] => value
                                    [1] => 100
                                )

                            [30] => Array
                                (
                                    [0] => op
                                    [1] => %
                                )

                            [31] => Array
                                (
                                    [0] => value
                                    [1] => 20
                                )

                            [32] => Array
                                (
                                    [0] => op
                                    [1] => >=
                                )

                            [33] => Array
                                (
                                    [0] => op
                                    [1] => ||
                                )

                            [34] => Array
                                (
                                    [0] => op
                                    [1] => &&
                                )

                            [35] => Array
                                (
                                    [0] => value
                                    [1] => 1
                                )

                            [36] => Array
                                (
                                    [0] => value
                                    [1] => 2
                                )

                            [37] => Array
                                (
                                    [0] => op
                                    [1] => ?:
                                )

                            [38] => Array
                                (
                                    [0] => op
                                    [1] => ?:
                                )

                        )

                    [cache:protected] => Array
                        (
                        )

                )

            [1] => get
        )

)

Выведем все оригинальные строки и их переводы. Данные переводы хранятся в свойстве entries, которое содержит массив объектов Translation_Entry для каждой строки.

$data = get_translations_for_domain( 'classic-editor' );

if ( $data instanceof MO && $data->entries ) {
	foreach ( $data->entries as $entry ) {
		echo "Оригинальная строка: {$entry->singular} \n";
		echo "Переведенная строка: {$entry->translations[0]} \n\n";
	}
}

Результат:

GitHub

Оригинальная строка: Allow site admins to change settings 
Переведенная строка: Разрешить администраторам сайта изменять настройки 

Оригинальная строка: Allow users to switch editors 
Переведенная строка: Разрешить пользователям переключение редакторов 

Оригинальная строка: By default the Block Editor is replaced with the Classic Editor and users cannot switch editors. 
Переведенная строка: По умолчанию редактор блоков будет заменен на классический редактор и пользователи не смогут выбирать редактор. 

Оригинальная строка: Change settings 
Переведенная строка: Изменить настройки 

Оригинальная строка: Change the %1$sClassic Editor settings%2$s. 
Переведенная строка: Изменить настройки %1$sClassic Editor%2$s. 

Оригинальная строка: Classic Editor 
Переведенная строка: Classic Editor 

Оригинальная строка: Default Editor 
Переведенная строка: Редактор по умолчанию 

Оригинальная строка: Default editor for all sites 
Переведенная строка: Редактор по умолчанию для всех сайтов 

Оригинальная строка: Default editor for all users 
Переведенная строка: Редактор по умолчанию для всех пользователей 

Оригинальная строка: Edit &#8220;%s&#8221; in the Block Editor 
Переведенная строка: Редактировать &#8220;%s&#8221; в редакторе блоков 

Оригинальная строка: Edit &#8220;%s&#8221; in the Classic Editor 
Переведенная строка: Редактировать &#8220;%s&#8221; в классическом редакторе 

Оригинальная строка: Editor 
Переведенная строка: Редактор 

Оригинальная строка: Block Editor 
Переведенная строка: Редактор блоков 

Оригинальная строка: Classic Editor 
Переведенная строка: Классический редактор 

Оригинальная строка: Edit (Block Editor) 
Переведенная строка: Изменить (редактор блоков) 

Оригинальная строка: Edit (Classic Editor) 
Переведенная строка: Изменить (классический редактор) 

Оригинальная строка: Editor Settings 
Переведенная строка: Настройки редактора 

Оригинальная строка: Enables the WordPress classic editor and the old-style Edit Post screen with TinyMCE, Meta Boxes, etc. Supports the older plugins that extend this screen. 
Переведенная строка: Включает классический редактор WordPress, старый вариант экрана редактирования записей (TinyMCE, мета поля и др.). Поддерживает другие плагины расширения старого редактора. 

Оригинальная строка: No 
Переведенная строка: Нет 

Оригинальная строка: Settings 
Переведенная строка: Настройки 

Оригинальная строка: Switch to Block Editor 
Переведенная строка: Переключиться на редактор блоков 

Оригинальная строка: Switch to Classic Editor 
Переведенная строка: Переключиться на классический редактор 

Оригинальная строка: The Classic Editor plugin prevents use of the new Block Editor. 
Переведенная строка: Плагин Classic Editor предотвращает использование нового редактора блоков. 

Оригинальная строка: WordPress Contributors 
Переведенная строка: Участники разработки WordPress 

Оригинальная строка: Yes 
Переведенная строка: Да 

Оригинальная строка: https://github.com/WordPress/classic-editor/ 
Переведенная строка: https://github.com/WordPress/classic-editor/ 

Оригинальная строка: https://wordpress.org/plugins/classic-editor/ 
Переведенная строка: https://ru.wordpress.org/plugins/classic-editor/ 

Заметки

  • Global. MO[]. $l10n An array of all currently loaded text domains.

Список изменений

С версии 2.8.0 Введена.

Код get_translations_for_domain() WP 6.4.3

function get_translations_for_domain( $domain ) {
	global $l10n;
	if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
		return $l10n[ $domain ];
	}

	static $noop_translations = null;
	if ( null === $noop_translations ) {
		$noop_translations = new NOOP_Translations();
	}

	$l10n[ $domain ] = &$noop_translations;

	return $noop_translations;
}