How to 'manually' load translations/test domain outside theme context?

So I need to build a pdf file starting from a php/html template (currently using mpdf library). So my-pdf-template.php file will never be shown on the website frontend, and sits on the main theme's folder. It gets loaded at some point by mpdf and used as a template to format the resulting pdf. To use wp functions on that template I'm currently importing wp-blog-header.php, and all goes fine, except l10n functions (_e, __) do not work. No errors, but simply …
Category: Web

Is this really always bad? (variables in l10n functions)

I needed to translate strings on another language than the current $locale, so I need to change the locale and restore it later, and I really couldn't repeat all the code for the 30++ strings I had to translate. So I ended up with this function: function __2($string, $textdomain, $locale){ global $l10n; if(isset($l10n[$textdomain])) $backup = $l10n[$textdomain]; load_textdomain($textdomain, get_template_directory() . '/languages/'. $locale . '.mo'); $translation = __($string,$textdomain); if(isset($bkup)) $l10n[$textdomain] = $backup; return $translation; } Now, as per this famous article, I …
Category: Web

PHP Warning missing Argument 1 for __()

I am getting this error in my server error logs: [Thu Dec 11 10:06:30 2014] [error] [client 11.111.111.111] PHP Warning: Missing argument 1 for __(), called in /var/www/vhosts/mysite.org/public_html/wp-content/themes/mysite_v2.2/functions-cpt-projects.php on line 15 and defined in /var/www/vhosts/mysite.org/public_html/wp-includes/l10n.php on line 146, referer: http://www.mysite.org/wp-admin/edit.php I've never encountered this before and I'm wondering what might be causing it. The line of code it refers to in the l10n.php file is: function __( $text, $domain = 'default' ) { return translate( $text, $domain ); } I'm …
Category: Web

What about Deferring a localized script?

I would love to defer the loading of the script for a plugin, Simple Locator, which uses Google.maps API. However it uses wp_localize_script, which prints a wpsl_locator javascript object in the HTML page (to include options), which calls on google-maps API's google object, which isn't loaded yet. I thought that possibly I could fake it out by hacking the plugin and adding wpsl_locator = new Object() via another (deferred) script, which would include hard-coded options: wpsl_locator.map_options={[truncated]{style:google.maps.ZoomControlStyle.SMALL,}}; But one problem is …
Category: Web

Single translation for many locales

Greetings to everyone, Is it possible to do make a single translation for many locales? Let me explain it better: Can I create a single translation file for my theme called es.po and es.mo so it matches all possible variants for locales of the Spanish language? (i.e. es_ES, es_MX, etc.) The idea here is to avoid copying the file several time to match all locales of the Spanish language.
Category: Web

Localization of menu items

In register_nav_menu() function is possible to pass the second parameter inside wp l10n function: register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) ); but how to localize the menu items?
Category: Web

Localize strings from db

I'm using this method to translate strings in my template. The question is, what if some strings are dynamically loaded from the db? How can i translate them? I've been googling for days and even asked on other communities but no solutions came out. Is there any trick / workaround to achieve this?
Category: Web

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

If I have a library available through packagist that could be used in WordPress plugin or theme development, how should I address multilanguage support if I have a few strings that could be localized? I know that there has been some debate on composer usage in the WordPress world with very different views on it. (For example: 1, 2, 3) But my question here is not about opinions on Composer in WordPress development, rather what would be the best practice …
Category: Web

switch_to_blog(): Load textdomain

I am using the WordPress multisite feature with Multilingual Press Pro to create a multilingual site with linked posts. In a network activated plugin the custom post types are registered. For translating the slug I use l10n: 'rewrite' => array( 'slug' => __( 'products', 'text-domain' ) . '/%product-category%', 'with_front' => false ) This works fine. But, to link to the translated post on another site within the network, switch_to_blog() is used. As within this function no plugins are (re-)loaded, my …
Category: Web

Localized WordPress content for different sub-locales of same language?

I have a site which serves Ireland & England. I want to move it to Wordpress. Currently all the content is the same when you visit the site & it runs off two urls, the Irish one: mysite.ie & the english mysite.co.uk I need to show different prices based on whether the user is visiting the Irish site or the English one. Currently I do something like: $host = $_SERVER['HTTP_HOST']; if($host == "www.mysite.ie") { //show Irish prices } I don't …
Category: Web

Working with l10n contexts (_x etc)

Basic l10n from a .mo file generated out of PoEdit works fine. But as soon as I introduce _x, or variants, the link breaks, because as best as I can tell, PoEdit, completely ignores the context, and does not generate the corresponding msgctxt line in the .po/.mo files. _x, _ex and other variants are all added to the keywords, and PoEdit is finding and parsing _x() occurrences within my source code. It's just not capturing the context and it is …
Category: Web

Theme doesn't want to load localization files

Here is my functions.php excerpt: function get_theme_text_domain() { //Same behavior if I return the string as 'return 'whatever' $theme = wp_get_theme(); return $theme->get('TextDomain'); } function tamtando_theme_setup() { load_textdomain(get_theme_text_domain(), get_template_directory() . '/languages'); [...] } add_action('after_setup_theme', 'tamtando_theme_setup'); $theme is an array with all theme's properties: as expected. $theme->get('TextDomain') returns 'tamtando': as expected. I've noticed that the theme wasn't translating any string when using __('somestring', get_theme_text_domain()). I've then tried to see what was going on with the debugger, while executing load_textdomain(get_theme_text_domain(), get_template_directory() . …
Category: Web

How to keep a variable in COOKIE or GET query string?

I'm working on adding a language variable (locale) in my custom WordPress theme. I made the translation working by adding ?l=en_US to the URL. I would like to add the parameter into COOKIE, such that the site will remember the choice. Also, I would like to build a language switcher ( allow visitors to choose their own language ). How should I achieve it ? I am able to get the locale by (and load the text domain): add_action('after_setup_theme', 'DYtheme_setup'); …
Category: Web

Gettext details

I've got a few gettext() questions that are not covered anywhere in WordPress Codex but I think they are important. __('string') in THEME should be used for strings that are likely to be present also in main WordPress translation file. No textdomain necessary. Examples: one, two, submit, cancel, remove and other common words. Correct? __('string', 'textdomain') in THEME should be used for strings that are theme-specific. Examples: 'this option enables X feature'. Correct? I think it's better to make one …
Category: Web

Singular name Plugin localization

I'm trying to localize the singular name of a custom post type. ... 'singular_name' => __('key', 'plugindomain') ... I created a .po and compiled it as .mo. I also loaded the translation using load_plugin_textdomain using the init action. // $directory plugin directory relative to plugins dir load_plugin_textdomain('plugindomain', false, $directory); The string are localized correctly in the backend sidebar. However, the strings are not translated in the frontend with: // $post_type returned from get_post_type_object echo $post_type->labels->singular_name; Am I doing something wrong? …
Category: Web

Multisite WPLANG won't save

I have a multisite workpress install (v 3.3.2). I'm trying to save the WPLANG field but nothing happens. I get the confirmation message "Site options updated", but the field is still blank. And when I query it with get_locale, it get the default 'en_US'. I've tried entering in: ja_JP 'ja_JP' "ja_JP" ja-JP 'ja-JP' "ja-JP" Thanks in advance!
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.