Tag Info

Hot answers tagged

15

1. Write with localization in mind Don't use echo or print() to produce text output, instead use the WordPress functions __() and _e(): /** Not localization friendly */ echo "Welcome to my plugin"; // OR print("Welcome to my plugin"); /** Localization friendly */ _e('Welcome to my plugin', 'my-plugin'); // OR $my_text = __('Welcome to my plugin', ...


14

Internationalization and localization (commonly abbreviated as i18n and l10n respectively) are terms used to describe the effort to make WordPress (and other such projects) available in languages other than English, for people from different locales, who use different dialects and local preferences. __() is used when the message is passed as an ...


12

Never rely on core strings for translation, they may change or get a context parameter any time. Once that happens your users get a partially translated interface, and your translators have no way to fix that. Also keep in mind the same string is not necessary translated everywhere with the same word. Even without a context parameter it might be useful to ...


10

__ (double underscore) is the base translate function. It translates a string and returns it as a string. _e does the same as __, but echo's the result immediately. _x is the contextual translate function. It has a second option to provide context to people doing the translation. _ex is the same as _x, but echo's the result. Example of using _x: ...


9

Use the fourth parameter for get_post_time(): $time = get_post_time( 'F j, Y', // format TRUE, // GMT get_the_ID(), // Post ID TRUE // translate, use date_i18n() ); get_post_time() calls mysql2date() internally, and it passes the $translate argument through. In mysql2date() we find this: if ( $translate ) ...


8

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', ...


7

The Editor There are others, but this is most used: Poedit, a cross-platform gettext catalogs (.po files) editor. The Formats .mo stands for Machine Object -- compiled export of the .po file which is used by WordPress .po stands for Portable Object -- editable text file with the translations strings -- based on the master .pot file, using Update from POT ...


6

__(), _e() and _x(), _ex() __() and _e() are essentially both a wrapper of translate() (do not use directly) and almost the same. The difference lies in that __() returns the translated string and _e() echoes it. Both need to be fed a string as a required parameter and usually, though optional, also a textdomain. Analogously, there's _x() and _ex(), which ...


6

The second part is not required, it just loads a PHP file with language specific functions. Examples In some countries/regions/religions it is not allowed to use capital letters in a word for anything else than the name of some god. In these cases you probably want to remove the Wordpress to WordPress filter. Some languages (Chinese) do not use spaces (in ...


6

Use the filter 'mce_external_languages'. From wp-includes/class-wp-editor.php: The following filter loads external language files for TinyMCE plugins. It takes an associative array 'plugin_name' => 'path', where path is the include path to the file. The language file should follow the same format as /tinymce/langs/wp-langs.php and ...


6

This is not a problem if every string gets its own template for translation. If you use the string You can include a %s in a childtheme. more than one time for different nouns it is not translatable. An example, taken from a recent article: New %s is in German (yes, we have gendered nouns too) … Neues Buch (for a book, neutral) Neuer Film (for a movie, ...


6

It's used for translate text. The second argument is a kind of namespace (called domain here) to retrieve the translation (for example from a dedicated file or something else). So Anyword here, should be the guy behind the template, or the company or what ever that can be a domain/namespace. edit: The doc from wordpress give more explanation on how to ...


6

If you're creating this theme or plugin for inclusion in the official plugin or theme directories on WordPress.org, then you need to use the plugin or theme's slug as the text-domain. You cannot have multiple text domains, and the text domain cannot be anything other than the slug of the plugin or theme. This is due to the new language pack support coming ...


5

Instead of using 'if' loop it's better to use special function: <?php echo _n( 'article', 'articles', $count, 'my-plugin-domain' ); ?> http://codex.wordpress.org/Function_Reference/_n


5

Should such messages be localized at all or are they out of scope for localization? Yes, they should be localized ... but don't depend on the text returned by the API. Does something like __( $message ); even make sense? Not really. First of all, you're not providing a text domain for the string to use in localization. It should really be __( ...


5

Filter template_include: add_filter( 'template_include', 'prefix_translate_template' ); function prefix_translate_template( $template ) { if ( 'category-' . __( 'news', 'your_textdomain' ) . '.php' === $template ) return 'category-news.php'; return $template; } But I think templates based on slugs are not a good idea in that case.


4

Yes, but please don't. This is like coding standard, better follow it even when you can get a small advantage by bypassing it. Better reasons: In version 3.5 WordPress don't have a monolith translation files, it was broken into 3 part for performance reasons. If this trend continues, can you be sure that the default domain will be loaded at all when you ...


4

WordPress has a nice function mainly for that wp_localize_script To use it first queue your script: wp_enqueue_script( 'My_Script_handle', 'path/to/script.js' ); then create an array of strings you want to localize: $data = array( 'exit' => __( 'Exit','my-plugin-domain' ), 'open' => __( 'Open','my-plugin-domain' ), 'close' => __( ...


4

Within the code, can you see any syntax like either of the following? __( 'Hello, dear user!', 'my-text-domain' ) _e( 'Your Ad here', 'my-text-domain' ) That is, strings starting with __ (double underscores) or _e. If so, where the above says my-text-domain, you should find the name of your text domain.


3

I belive you can use a load_textdomain_mofile filter... add_filter('load_textdomain_mofile', 'custom_load_textdomain_mofile', 10, 2); function custom_load_textdomain_mofile( $mofile, $domain){ if ($domain == 'bp-ass') $mofile = 'somepath/to/your/mo/file.mo'; return $mofile; } Difference (comparing Otto) is you can actually specify your mo ...


3

Use number_format_i18n( $number ). It will format the number with respect to the current blog’s language setting. It is a wrapper for PHP’s native number_format().


3

You should use a localized XML format like XLIFF or TMX , there is a tool to convert XLIFF to .po. If you cannot change the source XML you have a few options but it really depends how your outputting this XML info: Parse the XML ( for label) and create a .po with gettext output for these strings (then possibly merge the .po files). Duplicate the XML ...


3

I'm not an expert on translations, but the WordPress Codex Page has good documentation and explains the reason to use each instance. From the codex pages: __() Is used when the message is passed as an argument to another function; _e() is used to write the message directly to the page. More detail on these two functions: __('message') Searches the ...


3

The text domain is a unique identifier, which makes sure WordPress can distinguish between all loaded translations so Using the basename of your plugin/theme is always a good choice because plugin/theme basename is always unique though it is not mandatory to have plugin/theme basename as text domain, you can use any unique identifier. If you're translating ...


3

The translation strings not only get parsed during rendering (output on screen/in browser), but also by the GNU gettext parser. This one is not a PHP parser, so it can't fetch variables. This is the only part of a Theme or a Plugin, where you need to repeat yourself and add the plain string to every translation/gettext function call. // Wrong: __( 'External ...


3

Your example is correct, simply because it works. There are hundreds of ways to localize strings wich are displayed including html, all good and elegant in their own way. I like to do this as follows: printf( '<div class="updated"> <p>%1$s</p> </div>', __( 'All options are restored successfully.', 'mytextdomain' ) ); ...


2

If your code, be it a theme or a plugin, does not support translation, then don’t use the translation functions. And if you don’t use these functions, you cannot use a text domain. :) There is a very common error that looks like this: echo __( 'Portfolio' ); Portfolio is not part of WordPress’ core strings, so this code will waste an expensive look-up in ...


2

This questions was already asked half a year ago: "Switch language by the click of a button" The result of it can now be found as a plugin maintained by @StephenHarris and me. WCM User Language Switcher It also got a place in the official repo. The development version can be found in our wecodemore GitHub account. What the plugin does: It adds a ...


2

It's interesting idea and I think that such approach will work for you. What I would recommend you is that it will be better to use constructions like this: // $singular = 1 - for 'me', 2 - for 'us' $like_on = _n('Like me on %s', 'Like us on %s', $singular, 'my-plugin'); In this case it will be easier to understand how to translate the phrase. Also read ...


2

wp_schedule_event can not be used for PRECISE time measurements as you are trying to do. An event scheduled with wp_schedule_event rarely runs exactly when it is scheduled to run, it runs sometime after the time which it has been configured for. To understand this, you must understand that php doesn't know what time it is until you ask it to look. Wordpress ...



Only top voted, non community-wiki answers of a minimum length are eligible