0

I am developing a theme that implements multiple languages. I have a few strings that will have to be translated in the theme itself. I would like to use a .po and .mo file. For some reason, when I try to load the files, they don't load properly and I have no idea why.

I have tried using the following code to load the correct text domain.

function my_theme_localized($locale)
{
    if (isset($_GET['lang'])){
        return sanitize_key($_GET['lang']);
    }

    return $locale;
}
add_filter('locale', 'my_theme_localized');

function my_theme_setup(){
    load_theme_textdomain('theme', TRANSLATION_URL);
}
add_action('after_setup_theme', 'my_theme_setup');

I debugged a bit and the link to the mo-file that is generated in the class-wp-theme should be correct. The locale I am using is returned properly and the corresponding files are in the directory (pl_PL.mo for example).

I have tried everything that I can think of and I still don't know what is wrong with this piece of code. Anyone who can help me?

  • Which translation plugin you have? Normally they set the locale and you don't have to do it manually. Also when you set the local as your lang ie pl then the local is pl and not pl_PL, so if pl.mo don't exist the translation won't be loaded. – Laxmana Jun 30 '15 at 12:58
  • I am using the "multilanguage" plugin by BestWebSoft :) The locale is set as pl_PL, I checked using get_locale and I also checked their local variable for the locale :) – Michiel Standaert Jun 30 '15 at 13:09
  • 2
    I am not familiar with the plugin but it shouldn't matter which one it is. Is the TRANSLATION_URL correct ? You have to use an absolute path to the directory and not a url. Ie get_stylesheet_directory() . '/languages' – Laxmana Jun 30 '15 at 13:30
  • This is how I defined the URL's. I checked the url that is used in the textdomain function and it takes me to the correct file :) define("THEME_URL", get_bloginfo("template_url")); define("TRANSLATION_URL", THEME_URL . "/languages"); – Michiel Standaert Jun 30 '15 at 13:52
  • Martin got me. That's the correct answer. – Laxmana Jun 30 '15 at 14:38
3

load_theme_textdomain() needs an absolute path to the directory, so you should set your THEME_URL like so

define("THEME_URL", get_template_directory());
define("TRANSLATION_URL", THEME_URL . "/languages");

It wasn't working for you because get_bloginfo("template_url") is an URL and not an absolute path on your server.

  • Aha, so that's what the problem was! Thank you! :) – Michiel Standaert Jun 30 '15 at 14:35
  • 1
    I would use get_stylesheet_directory() if you use a child theme because get_template_directory() returns the parent theme absolute path. – Laxmana Jun 30 '15 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.