Custom strings for translation using Polylang plugin

Use this pll_register_string() on functions.php Use it like this: pll_register_string Allows plugins to add their own strings in the “strings translation” panel. The function must be called on admin side (the functions.php file is OK for themes). Usage: pll_register_string($name, $string, $multiline); ‘$name’ => (required) name provided for sorting convenience (ex: ‘myplugin’) ‘$string’ => (required) the … Read more

Is it ok to use a function to output the text domain name in a wordpress theme

No, this is not okay. NO NO NO. Here’s my favourite Otto quote: Inside all translation functions, no PHP variables are allowed in the strings, for any reason, ever. Otto explains this in more detail, but the simple way to say it is that translation calls are parsed, not executed . So your variable (let … Read more

How to get a translated string from a language other than the current one?

To find the answer to this question, you just need to look at how WordPress retrieves the translations. Ultimately it is the load_textdomain() function that does this. When we take a look at its source we find that it creates a MO object and loads the translations from a .mo file into it. Then it … Read more

Override parent theme translation on child theme

I think I found a solution, but before a little Premise load_theme_textdomain() and load_child_theme_textdomain() are basically equal, the only difference is the default path they use: they get the current language (using get_locale()) and add the relative .mo file to the path passed as argument; then they call load_textdomain() passing as argument both the textdomain … Read more

Different Language for Frontend vs Backend

You can do the following: Get the the language pack (e.g. de_DE.mo) from wordpress.org. If the language pack isn’t available as a standalone download, you could also use the .mo file which is bundled in the WordPress ZIP-file for your language. Located under wp-content/languages. Move the .mo file to wp-content/languages/ of your default (english) WordPress … Read more

pass object/JSON to wp_localize_script

Indeed, wp_localize_script() is simple, it just adds quotes around the values and escapes the content, expecting all of them to be strings. However, there is the l10n_print_after key of the array, which will be printed without any interference at all. It can be used to execute arbitrary code after the strings are passed. You can … Read more

Override default WordPress core translation

You could use gettext filter: add_filter( ‘gettext’, ‘cyb_filter_gettext’, 10, 3 ); function cyb_filter_gettext( $translated, $original, $domain ) { // Use the text string exactly as it is in the translation file if ( $translated == “Categorie: %s” ) { $translated = “Sectie: %s”; } return $translated; } If you need to filter a translation with … Read more

Theme localization of “slugs” (custom post types, taxonomies)

I wouldn’t try to localize your slugs. Instead, why not give your users the option to change them by adding another field to the permalink settings page? Hook into load-options-permalink.php and set up some things to catch the $_POST data to save your slug. Also add a settings field to the page. <?php add_action( ‘load-options-permalink.php’, … Read more