load_theme_textdomain()

最后更新于:2021-11-27 14:15:30

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

Load the theme’s translated strings.

参数

$domain

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

$path

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

Default value: false

响应

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

源文件

文件: gc-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', determine_locale(), $domain );

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

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

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

	return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
}
// CHANGE LOCAL LANGUAGE
// must be called before load_theme_textdomain()

add_filter( 'locale', 'gcdocs_theme_localized' );

/**
 * Switch to locale given as query parameter l, if present
 */
function gcdocs_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 gc-content/languages/ directory
load_theme_textdomain( 'gcdocs_theme_textdomain', get_template_directory().'/languages' );