How to translate “$before” with get text in get_the_term_list?

Use the __() function, just as you see in the the_content() example: <?php echo get_the_term_list( // ID $post->ID, // taxonomy ‘book’, // Before __( ‘Books: ‘, ‘ft’ ), // Separator, see http://core.trac.wordpress.org/ticket/7897 __( ‘, ‘ ), // After ‘ ‘, // Looks like you’ve added an extra parameter? ” ); ?> Note 1: I split … Read more

Using wordpress translations strings in plugin

Turns out that wordpress uses context strings for this piece of text: _x(‘Add New’, ‘post’) and _x(‘Add New’, ‘page’) This is because the string ‘Add New’ is different depending on the following noun in some languages. i.e in Danish: ‘Add New Post’ => ‘Tilføj Nyt Indløg’ ‘Add New Page’ => ‘Tilføj Ny Side’. Notice the … Read more

How can I see my theme’s text domain?

Your theme’s text domain is defined in your Theme Header in style.css. For example: /* * Plugin Name: My Plugin * Author: Otto * Text Domain: my-plugin */ The text domain should match your theme’s ‘slug’ (ie. the name of the folder where your theme is stored). Read more: https://codex.wordpress.org/I18n_for_WordPress_Developers#Text_Domains

Using WordPress gettext functions in a library outside plugin or theme scope

WordPress doesn’t quite have a practice of localizing something that isn’t core/plugin/theme. My educated guess would be that it will work just fine with same concepts, but you will have to write custom loading logic. Use lower level load_textdomain(), since higher level functions are meant for plugins/themes specifically. As long as you determine and load … Read more

How to set translations in javascripts for my plugin?

The WordPress way to do this is the wp_localize_script() function. When you enqueue a script, also add a call to wp_localize_script(): wp_register_script( ‘my-script’, ‘path/to/script.js’ ); wp_localize_script( ‘my-script’, ‘myScript’, array( ‘msg_must_match’ => __( ‘Message must match’, ‘my-plugin-domain’ ), ) ); This creates a global JavaScript object called myScript that will contain the keys and values passed … Read more

What does the underscore translate function do in my code without a textdoman

In the __() documentation, it says If there is no translation, or the text domain isn’t loaded, the original text is returned. Also, if you don’t provide a textdomain, then ‘default’ is used. So, essentially, __( ‘Name’ ) will try to find a translation in the ‘default’ domain, and, failing that, return ‘Name’. If you … Read more

Can’t we use strings defined as PHP constants if we want to translate them in a plugin?

I think this is happening because Constants cannot be redefined later. Once they are set, they are fixed. http://php.net/manual/en/language.constants.php I’m not exactly sure how WP language constructs work, but part of me thinks that they are defined, then changed later on the fly when plugins/themes use them. I don’t have a reference for this but … Read more