Performance: Theme translation readiness
This affects page load speed by controlling how text strings are loaded and rendered in different languages, impacting initial content display.
load_theme_textdomain('mytheme', get_template_directory() . '/languages'); // Use selective loading or lazy load translations only when needed
load_theme_textdomain('mytheme', get_template_directory() . '/languages'); // Loads translation files synchronously on every page load
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all translations synchronously | N/A | 0 | Blocks initial paint until translations load | [X] Bad |
| Load translations selectively or lazy load | N/A | 0 | Allows faster initial paint by reducing blocking | [OK] Good |
__() returns the translated string without printing it, while _e() echoes it immediately.__() is the correct choice.load_theme_textdomain() in a WordPress theme?load_theme_textdomain() function should be called early enough to load translations before theme output.after_setup_theme is the recommended hook for theme setup tasks including loading text domain.functions.php:
add_action('after_setup_theme', function() {
load_theme_textdomain('mytheme', get_template_directory() . '/languages');
});
echo __('Welcome', 'mytheme');
What will be the output if the translation file for 'mytheme' contains a Spanish translation for 'Welcome' as 'Bienvenido'?__() function uses 'mytheme' text domain to translate 'Welcome'. Since Spanish translation exists, it outputs 'Bienvenido'.function mytheme_setup() {
load_theme_textdomain('mytheme', get_template_directory() . '/languages');
}
add_action('init', 'mytheme_setup');
echo __('Hello World', 'mytheme');_e() or __(), load text domain in after_setup_theme, and place translation files in /languages folder.
B) Use _e() only, load text domain in init hook, and place translation files in theme root.
C) Wrap strings with __(), load text domain in wp_loaded hook, and place translation files in /lang folder.
D) Use _e() and __() without loading text domain, place translation files anywhere._e() and __() should be used to wrap user-facing strings for translation.after_setup_theme hook, and translation files should be stored in a /languages folder inside the theme.Pass the test and this pattern moves unknown