WordPress.org

Ready to get started?Download WordPress

Forums

automatic language selection and security holes (3 posts)

  1. jcv
    Member
    Posted 5 years ago #

    Hi all,

    I own a blog mainly in French, but sometimes I also write some English messages. Moreover, most plugins/blog are both languages capable. If I had defined WPLANG to en_EN (config.php file), almost all the blog would be in English.

    Thus, I was wondering why WordPress doesn't implement a fully automated option, which is on my very config.php, for instance, to recognize the user browser language. Google does this. I implemented a code on my blgo to do (part) of this :

    $browserlanguageis = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    if ( $browserlanguageis == "en" ) {
    define ('WPLANG', 'en_EN');
    } else {
    define ('WPLANG', 'fr_FR');
    }

    which replaces

    define ('WPLANG', 'fr_FR');

    I'm wondering if this code (may) involve any security issue? I can understand WordPress doesn't implement all languages available (it would be a huge waste of space), but I don't know if this kind of piece of code could be a security hole...

    What do you think?

  2. Sorin
    Member
    Posted 5 years ago #

    I totally agree with you that WPLANG need rework. Still your code is not wright. Auot-selecting the best language can be made using HTTP_ACCEPT_LANGUAGE and the list of available translations (PO/MO files).

    But I'm pretty sure that we can found this code in another open-source project.

  3. gbateson
    Member
    Posted 5 years ago #

    Here's some slightly more rubust code to check the browser preferences and offer the fuirst language that is available on the WordPress site:

    // ====================================================
    if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

    // default language
    $wplang = '';

    // try and set language from browser preferences
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $langs = explode(',', str_replace(' ', '', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
    foreach ($langs as $lang) {
    if ($strpos = strpos($lang, ';')) {
    $lang = substr($lang, 0, $strpos);
    }
    if ($lang=='en' || is_file(ABSPATH . "wp-content/languages/$lang.mo")) {
    $wplang = $lang;
    break;
    }
    if ($strpos = strpos($lang, '-')) {
    $lang = substr($lang, 0, $strpos);
    if ($lang=='en' || is_file(ABSPATH . "wp-content/languages/$lang.mo")) {
    $wplang = $lang;
    break;
    }
    }
    }
    }
    define ('WPLANG', $wplang);
    unset($langs, $lang, $strpos, $wplang);
    // ====================================================

Topic Closed

This topic has been closed to new replies.

About this Topic