wp_get_installed_translations()
云策文档标注
概述
wp_get_installed_translations() 函数用于检索 WordPress 中已安装的翻译文件,支持插件、主题和核心的翻译搜索。它通过扫描 wp-content/languages 目录,返回语言数据的数组。
关键要点
- 函数接受一个必需参数 $type,指定搜索类型,可选值为 'plugins'、'themes' 或 'core'。
- 返回一个数组,包含翻译的语言数据,结构为 $language_data[$textdomain][$language]。
- 函数内部使用 WP_Textdomain_Registry 来获取翻译文件,并优先处理 .po 文件,如果存在则跳过对应的 .l10n.php 文件。
- 如果 $type 参数无效或目录不存在,函数返回空数组。
代码示例
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;
}注意事项
- 函数依赖于 WP_Textdomain_Registry 类,确保其已正确初始化。
- 翻译文件命名需符合正则表达式模式,否则会被跳过。
- 在 WordPress 3.7.0 版本中引入,使用时需注意版本兼容性。
原文内容
Gets installed translations.
Description
Looks in the wp-content/languages directory for translations of plugins or themes.
Parameters
$typestringrequired-
What to search for. Accepts
'plugins','themes','core'.
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |