返回介绍

load_theme_textdomain()

发布于 2017-09-11 01:42:00 字数 4917 浏览 1284 评论 0 收藏 0

load_theme_textdomain( string $domain,  string $path = false )

Load the theme’s translated strings.


description

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.


参数

$domain

(string) (Required) Text domain. Unique identifier for retrieving translated strings.

$path

(string) (Optional) Path to the directory containing the .mo file.

Default value: false


返回值

(bool) True when textdomain is successfully loaded, false otherwise.


源代码

File: wp-includes/l10n.php

function load_theme_textdomain( $domain, $path = false ) {
	/**
	 * Filters a theme's locale.
	 *
	 * @since 3.0.0
	 *
	 * @param string $locale The theme's current locale.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$locale = apply_filters( 'theme_locale', is_admin() ? get_user_locale() : get_locale(), $domain );

	$mofile = $domain . '-' . $locale . '.mo';

	// Try to load from the languages directory first.
	if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) {
		return true;
	}

	if ( ! $path ) {
		$path = get_template_directory();
	}

	return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
}

更新日志

Versiondescription
4.6.0The function now tries to load the .mo file from the languages directory first.
1.5.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: get_user_locale()
  • wp-includes/theme.php: get_template_directory()
  • wp-includes/l10n.php: load_textdomain()
  • wp-includes/l10n.php: theme_locale
  • wp-includes/l10n.php: get_locale()
  • wp-includes/load.php: is_admin()
  • wp-includes/plugin.php: apply_filters()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/l10n.php: load_child_theme_textdomain()
  • wp-includes/class-wp-theme.php: WP_Theme::load_textdomain()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    1st example
    The load_theme_textdomain() function should generally be called from within the after_setup_theme action hook.

    
    add_action('after_setup_theme', 'wpdocs_theme_setup');
    
    /**
     * Load translations for wpdocs_theme
     */
    function wpdocs_theme_setup(){
        load_theme_textdomain('wpdocs_theme', get_template_directory() . '/languages');
    }
    

    The .mo files must use language-only filenames, like languages/de_DE.mo in your theme directory.

    Unlike plugin language files, a name like my_theme-de_DE.mo will NOT work. Although plugin language files allow you to specify the text-domain in the filename, this will NOT work with themes. Language files for themes should include the language shortcut ONLY.

  2. 2nd example
    you can use this example if you wish to switch theme language using a variable passed within the URL, for example to load the Tamazikht language, your URL would look like; www.example.com/?l=tz_MA, this will search for a .mo file with name tz_MA.mo in the language directory inside your theme.

    
    // CHANGE LOCAL LANGUAGE
    // must be called before load_theme_textdomain()
    
    add_filter( 'locale', 'wpdocs_theme_localized' );
    
    /**
     * Switch to locale given as query parameter l, if present
     */
    function wpdocs_theme_localized( $locale )
    {
    	if ( isset( $_GET['l'] ) )
    	{
    		return sanitize_key( $_GET['l'] );
    	}
    
    	return $locale;
    }
    
    // SET THEME LANGUAGES DIRECTORY
    // Theme translations can be filed in the my_theme/languages/ directory
    // Wordpress translations can be filed in the wp-content/languages/ directory
    load_theme_textdomain( 'wpdocs_theme_textdomain', get_template_directory().'/languages' );
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文