WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Allways fallback to English? (11 posts)

  1. lbs129
    Member
    Posted 1 year ago #

    Hi,

    Thank you very much for the Polylang plugin - it is really good! However, I wonder if it is possible to have a fallback language other than the default language?

    Let's assume that a website has three languages activated; English, Swedish and German. Most of the visitors as well as the administrators of the site will be swedes and therefore I would like to have Swedish as default language to be able to access the Swedish home page at the URL root ("http://www.mydomain.se/"). When a visitor has German or English in their browser settings they should be redirected to their respective languages ("/en" or "/de"). But, if a visitor with e.g. Spanish or Dutch in their browser settings visits the site, I would like them to be redirected to the English site since this is the language that they are most likely to understand. Is this possible with Swedish as the default language? I'm a quite experienced user so I am ready to edit PHP files if necessary but right now I don't know where to look.

    Thank you in advance for your reply!

    Best regards,
    Johan

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

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I believe that is not possible without modifying the plugin. If you want to do so, edit the file polylang/include/core.php at line 182 and replace:

    return isset($pref_lang) ? $pref_lang : $this->get_language($this->options['default_lang']);

    by

    return isset($pref_lang) ? $pref_lang : $this->get_language('en');

    I did not test but it should do what you expect.

  3. lbs129
    Member
    Posted 1 year ago #

    Hi and thanks for your reply!

    I changed that line of code and from what I understand of the code it seems to be what I am looking for. However, I cannot make sure it's working. When I change the default language in the web browser (I've tried Opera and Firefox) and clear the cookies I am still being shown the Swedish version of the site. Is there anything more that needs to be changed or is the site only looking at the operating system's language setting (I'm using Windows in Swedish)?

    Thank you in advance for your reply and happy new year!

    Best regards,
    Johan

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Did you totally suppress both Swedish and English from your language preferences in your browser?

  5. lbs129
    Member
    Posted 1 year ago #

    Hi again,

    In both Firefox and Opera there is a dialogue window where you can choose which languages the websites should be displayed in. You arrange them with the language with the highest priority at the top and the lowest at the bottom. I tested this by putting German at the top and keeping Swedish and English in the list (but with lower priority). Logically, with our changes in Polylang, this should make the website show the English version (since there is no German version), but it still shows the Swedish version.

    However, when I remove all languages except German, the English site is being served as intended. So, for those not having Swedish in the list at all, everything should work as intended. It is still confusing though that the prioritization didn't work - I don't think it's a browser bug since the same error appears in both Firefox and Opera.

    Best regards,
    Johan

  6. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Did you mean that you choose your browser language preferences in that order:
    * German
    * English
    * Swedish
    and it displays Swedish ? In that case it would be a bug of Polylang.

    However, if you chose:
    * German
    * Swedish
    * English
    then Polylang logically displays Swedish because it detects that you prefer Swedish rather than English.

    The modification I proposed is only for people who have neither English nor Swedish in their language preferences.

  7. lbs129
    Member
    Posted 1 year ago #

    Oh, of course, it was the latter... It works as supposed! Thank you for clarifying this and for a great plugin!

    Could this fallback feature be included in the next version of Polylang as setting that could be enabled from Plugin Settings? I think it would be appreciated by many more than me.

    Thanks again!

    Best regards,
    Johan

  8. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    No I won't include a new option in v1.0. The admin interface is frozen now.

    However, I won't push you modifying the plugin a second time :)
    You will have to create your own plugin. Copy paste this in a file called 'polylang-fallback.php', upload it in your plugins directory and activate it. It will work as soon as you will ugrade to 1.0 - when it will be available ;-)

    <?php
    /*
    Plugin Name: Custom fallback for Polylang
    Description: Fallback language is English whatever the default language
    */
    
    add_filter('pll_preferred_language', 'my_language_fallback');
    function my_language_fallback($slug) {
    	return $slug === false ? 'en' : $slug;
    }
  9. lbs129
    Member
    Posted 1 year ago #

    Thanks, but isn't a PHP end tag necessary at the end of that code?

  10. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    The PHP close tag is not necessary at the end of file. I am not a specialist so I adhere to common practices :)
    http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag

  11. lbs129
    Member
    Posted 1 year ago #

    OK, I see. Thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic