WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Polylang and WordPress SEO - string translation (15 posts)

  1. lucabarbetti
    Member
    Posted 2 years ago #

    Hi there,
    I am testing Polylang 0.7.2 on my dev website and I like it a lot (compliments to Chouby and all the contributing people). I have read the documentation, faqs and almost all the posts in this forum, but I just cannot translate the user defined strings that WordPress SEO introduces (in permalinks, titles, RSS, ecc). I have found useful pieces of information here and there (e.g. http://wordpress.org/support/topic/plugin-polylang-register-taxonomy-so-that-its-name-can-be-translated?replies=4#post-2547846, http://wordpress.org/support/topic/any-seo-plugin-that-works-fine-with-polylang) but, because of my lack of programming experience, I need a little help in putting it all together to get those strings translated. Thanks a lot

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Do you mean you want to translate strings you can enter in the WordPress SEO titles admin page ?

  3. lucabarbetti
    Member
    Posted 2 years ago #

    Exactly Chouby, but not only the titles, also breadcrumbs, rss and all the strings that are defined by the admin/webmaster in WordPress SEO settings. I think that those strings, if defined, should be translated because they either appear in google searches or on the front-end of the website (e.g. the breadcrumbs string "You are here:") or rss feeds.
    In WordPress SEO plugin there is a WPML.xml which has tags that refer to all those strings, I don't use WPML but I guess that they are there to make them translatable through WPML.
    Thanks a lot Chouby

  4. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    In fact, you will have to do manually what WPML is doing automatically thanks to this config file. You can do something like what has been done for the atahualpa theme:
    http://wordpress.org/support/topic/plugin-polylang-multilingual-theme-options

    First make a list of all translatable strings:

    pll_wpseo_strings = array(
      "title-home",
      "metadesc-home",
    );

    the wpml config file will help you building the whole list

    then create a function to translate this list:

    function translate_wpseo_strings($options) {
      foreach ($options as $key=>str){
        if (in_array($key, $pll_wpseo_strings)) {
          if (is_admin())
            pll_register_string('wpseo', stripslashes($str));
          else
            $options[$key] = pll__($str);
        }
      }
      return $options;
    }

    and finally filter the wpseo options with the function above

    if (function_exists('pll_register_string')) {
      add_filter('option_wpseo_titles', 'translate_wpseo_strings');
      add_filter('option_wpseo_internallinks', 'translate_wpseo_strings');
      add_filter('option_wpseo_rss', 'translate_wpseo_strings');
    }
  5. lucabarbetti
    Member
    Posted 2 years ago #

    Dear Chouby,
    your code sounds good to me and I tried to use it, unfortunately I am not quite a programmer and I must have put it in the wrong place because now I get only a white screen... on a lighter note, this way I have got nothing to translate! :o)
    I use a custom (twenty ten) child time and I added your code to its functions.php, completing the list of all the WordPress SEO strings that I want to translate, but it doesn't work.
    Would you please tell me where I have to add that code?
    Thanks a lot, have a good day!

  6. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    Sorry, I wrote that too quickly and made some typos and errors. The first two code snippets must be combined:

    function translate_wpseo_strings($options) {
      $pll_wpseo_strings = array(
        "title-home",
        "metadesc-home",
      );
    
      foreach ($options as $key=>$str){
        if (in_array($key, $pll_wpseo_strings)) {
          if (is_admin())
            pll_register_string('wpseo', stripslashes($str));
          else
            $options[$key] = pll__($str);
        }
      }
      return $options;
    }

    The $ was missing in two variables. The third snippet is OK.

    The functions.php file would be a possible place in general cases but not with wpseo :( because wpseo retrieves its options as soon as the plugin is loaded which will prevent to filter them in functions.php which is loaded later.

    Of course, you could add the code directly in wp-seo.php before the call to the function $options = get_wpseo_options(); but it would be removed at each plugin update.

    So it's better to create a plugin and ensure it is loaded *before* WordPress SEO (thanks to the fact that WordPress sorts plugins by name :) )

    So create a new php file called pw-my-plugin.php (the name is important and is chosen to make it loaded after Polylang and before WordPress SEO). Add these lines:

    <?php
    /*
    Plugin Name: MyPlugin
    */

    As well as all the code mentionned above. Then upload it in your plugins directory. And Go activate the plugin.

    And finally, you will have to upgrade to Polylang to 0.8.1 (or current development version) (thanks to you, I discovered that previous versions will create duplicate versions of the strings when it is registered several times as it is the case here).

  7. lucabarbetti
    Member
    Posted 2 years ago #

    Hi Chouby,
    this code works much better! In fact I followed your instructions and now all the WordPress SEO strings that I want to traslate appear in the strings page of Polylang settings ready to be translated.
    Since I added accidentally more strings than I needed to (e.g. breadcrumbs separator), I wonder if it is possible to disable those strings. I commented them out from MyPlugin but they still appear in the strings page of Polylang settings. It isn't a big deal but it would be nice to get rid of those added by mistake or that aren't used anymore.

    In order to test this combination of plugins I have set up a fresh WordPress 3.3.1 installation with only default plugins, Polylang, MyPlugin and WordPress SEO. As you suggested I updated Polylang to the 0.8.0.5 release (couldn't find the 0.8.1 though). I have also translated all the (few) posts, pages and categories that I use as a test. Everything seems to work properly but the latest posts page (homepage in this case) doesn't switch language, it sticks to the default english language.
    Thanks a lot Chouby, I really appreciate it.

  8. lucabarbetti
    Member
    Posted 2 years ago #

    Quick update to my previous post.
    I said:

    Everything seems to work properly but the latest posts page (homepage in this case) doesn't switch language, it sticks to the default english language.

    Not exactly. In fact I have two languages other than the default one, and I have realized that with one of them the latest posts page works correctly while with the other it is 'redirected' to the default language (http://localhost:port), even if the mouseover shows the right link (http://localhost:port/fr/).
    I checked the configuration of the languages, posts, pages and categories and they seem to be the same for both languages.

  9. Chouby
    Member
    Plugin Author

    Posted 2 years ago #

    0.8.1 is not released yet... I have still a few things to correct before releasing it (as trying to understand what's wrong with your home page), but I upload my development version time to time as it may help some people.

    Could you precise all your Polylang settings ? Your permalink structure ? And if you are using static page as front page (and page for posts) ?

  10. lucabarbetti
    Member
    Posted 2 years ago #

    Hi Chouby,
    Good news, I don't know what happened, but it seems that restarting my pc fixed the problem of the homepage.
    Anyway I have these settings:

    • three languages;
    • three menus, one for each language;
    • Language switcher at the end of the menu, with flags, hides current language;
    • when the front page is visited, set the language as the browser preference;
    • remove /language/ in pretty permalinks;
    • hide URL language information for default language

    The Permalink structure is localhost:port/post-name/;
    The Front page is the (wordpress default) latest posts page.

    Thanks a lot!

  11. decaff
    Member
    Posted 1 year ago #

    I'm sorry for bringing this thread up again, but I was looking for exactly this solution and it seems that it is not working for me. If my request is wrong in here, just say it and I'll put up another thread.

    1. I set up the plugin and the strings finally appeared in the polylang settings->strings menu
    2. I translated the meta description because I think it would be helpful to have meta descriptions in two languages
    3. I realized that WordPress SEO just takes the Meta Description of the default language (English in my case). So even if I had translated the strings, they don't appear in the source code in another language than the default language.

    Maybe there is a solution anywhere out there I haven't found yet, but please help me with this. Thanks a lot!

  12. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I remade a test and it works fine for me with the code above

  13. decaff
    Member
    Posted 1 year ago #

    Funny thing, in my WP Test Environment it is still not working, but I implemented it now in my project and it works fine.

    Thanks a lot for your quick reply!

  14. Baz11
    Member
    Posted 1 year ago #

    Is it possible to make similar decision for All in one Seo ?

  15. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I am glad to announce that Polylang v1.0 will no more request all these manipulations to be compatible with WordPress SEO :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic