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 they don't find the context's translations (OR, they think it's not another language, so show the default).

How to make them available even here? Or, let them believe the current language is another from the default one?

Topic l10n translation Wordpress

Category Web


Already found out:

load_textdomain('my-text-domain', TEMPLATEPATH . '/languages/'. $my_locale . '.mo');

In some cases it could be useful to restore the previous text domain, so we should make a backup. An old function I had hidden in my code that does all, is this one:

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;
}

About

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