• Resolved HeikoH

    (@heikoh)


    I made a wordpress theme and set it up in two languages (EN and DE). I also made the required mo-files. I defined german as default language in wp-config.php in the beginning. Everything works fine but I have some problems with AJAX requests: It doesn’t load the correct language files.

    My scenario:

    I made a Button/Link requesting data via AJAX. Something like this (javascript):

    jQuery('#button').click(function(){
     jQuery.ajax({
      url: "http://myexampledomain.com/wp-admin/admin-ajax.php?lang=en",
      data: { action: 'my_action', id : 123 },
      type: "POST",
     }).done(function (data){
      alert(data);
     });
    });

    As you can see, I am trying to load my information in english (url => ?lang=en). I wrote this code in my functions.php:

    add_action("wp_ajax_nopriv_my_action", "my_action");
    add_action("wp_ajax_my_action", "my_action");
    function my_action(){
     echo __( 'ID nicht gefunden:', 'mytheme' ) . $_POST['id'] ;
     exit;
    }

    My Problem is, that the alert “ID nicht gefunden:123” pops up although it should be “ID not found:123“.

    It seems wordpress is not loading the correct language mo-file.

    To fix this I tried the following in my functions.php (but it didn’t work):

    add_filter( 'locale', 'my_theme_localized' );
    function my_theme_localized( $locale ){
     if ( isset( $_GET['lang'] ) && $_GET['lang']=='en' ){
      return 'en_US';
     }
     return 'de_DE';
    }

    I also experemented with some plugins: XILI-language, qTranslate and Polylang.
    The result is that only qTranslate was able to load the correct language file via AJAX request.
    But qTranslate is not the best solution for some tasks I am working on.

    I also found a “workaround” to load the correct mo-file by writing the following into wp-config.php:

    if(isset( $_GET['lang'] ) && $_GET['lang']=='en')
     define('WPLANG', 'en_US');
    else
     define('WPLANG', 'de_DE');

    But I don’t like this hack (modifing wordpress core files).

    Is there are good solution to make wordpress load the corret mo-file by using $_GET[‘lang’] parameter in the AJAX request? What piece of code do I need in my functions.php? Any other working solutions?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thanks for you question,

    If you read sources (in admin side), xili-language is able in ajax to sub-select posts according language. The js file xili-findposts.dev.js contains an example. This file is used in process where users can choose posts to link from another languages in post edit form (a window with sub-list appears in front).

    Hope that help you to progress..

    M.

    Thread Starter HeikoH

    (@heikoh)

    Thanks for your answer but that’s not what i am looking for. I don’t want to find posts in a language as admin.
    I want wordpress to load the correct language files (theme and plugins) when I make an AJAX request from frontend. XILI doesn’t make wordpress to load the correct mo-file when a user makes an AJAX call (load en_US.mo instead of de_DE.mo when $_GET[‘lang’] = ‘en’ is set)

    I understood what you need but I think it will be too late to change the .mo inside the process of webpage building or when a webpage is built and refresh in part by AJAX. In xili-language born more than 5 years ago, the change is done inside the action hook “wp” where all infos are known and just before the web building by php…
    Why do you want to use AJAX ? At what moment ?
    M.

    Thread Starter HeikoH

    (@heikoh)

    I am writing different plugins and applications. And sometimes it is necessary that the user makes an ajax request and showing the result in an overlay(thickbox, lightbox). And (for example) if something goes wrong the error message should be displayed in the user’s language. Not in the default language defined by wp-config.php because the error message is written in the mo-file.

    I am now trying to write a plugin which sets the language. I read that plugins are loaded before the $locale is set so this may be a solution (adding a filter using a plugin).

    Or is there a solution to unload the default mo-file and load a specific one on the fly? That would also be useful.

    load unload of mo files can be done only at a very precise moment via filter or action…
    In your case, via ajax, the wp_settings timeline is not really the same like the default when requesting the site via pure php…
    If you want to sub-select posts, no problem to make it via ajax in front or backend sides..

    via AJAX, if you want to change language of returned message, it will be more complex than changing language according (connected ?) language of users (where is set this language, by default = to wplang) . do you use a plugin for language user ?

    M.

    Thread Starter HeikoH

    (@heikoh)

    In one project I use xili and I solved the problem:
    Plugins are loaded before the normal theme. So I wrote a little plugin which contains just a view lines of code to set the language and load the correct MO-files.
    The plugin simply does two things:
    1. include filter
    add_filter('locale', 'my_function', 99);
    The priority must be high. Here: 99. The higher priority overwrittes locals that are loaded earlier.
    2. In the my_function($locale){….} function I wrote the code to determine which language file should be loaded.
    For example:

    my_function($locale){
      if($_GET['lang'] == 'en')
        return 'en_US';
      return $locale;
    }

    So my_function makes wordpress to load en_US.mo file if lang=en. Otherwise the default mo-file is loaded.

    But that little “bug-fix-plugin” is not useable for wide range of people. It just fits for my special needs.

    In addition:
    I also wrote a little plugin to rewrite the xili-URL. example.com?lang=en is not very SEO friendly. With this second plugin I had to make some changes to the first mentioned plugin.

    Thread Starter HeikoH

    (@heikoh)

    Problem solved by previous post.

    It would be good if you publish this plugin or communicate me this sources (use support contact form ) a good way to return free contributions of xili-language since 5 years.
    Note also, that management of permalinks including language are available yet for donators as add ons in child theme functions and soon, more improved, for all after a series of tests that started end 2013…
    M.

    /* if qTranslate is installed */
    /* set front locale for ajax calls requested from front-end */

    function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( ‘DOING_AJAX’ ) && DOING_AJAX
    && substr( $_SERVER[‘HTTP_REFERER’], 0, strlen( admin_url() ) ) != admin_url() ) {

    load_theme_textdomain( ‘your-theme-domain-name’, get_template_directory() . ‘/languages’ );
    }
    }

    add_action( ‘admin_init’, ‘set_locale_for_frontend_ajax_calls’ );

    add_action(‘wp_head’,’jsURLs’);

    function jsURLs(){

    global $q_config;

    ?><script type=”text/javascript”>
    /* <![CDATA[ */
    var ajaxurl = “<?php echo admin_url(‘admin-ajax.php?lang=’.$q_config[‘language’]); ?>”;
    /* ]]> */
    </script><?php

    }

    it works for me if qTranslate is installed
    but if not following maybe work:
    /* if qTranslate is not installed */
    /* set front locale for ajax calls requested from front-end */

    function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( ‘DOING_AJAX’ ) && DOING_AJAX
    && substr( $_SERVER[‘HTTP_REFERER’], 0, strlen( admin_url() ) ) != admin_url() ) {
    setlocale(LC_ALL, $_GET[‘lang]);
    load_theme_textdomain( ‘your-theme-domain-name’, get_template_directory() . ‘/languages’ );
    }
    }

    add_action( ‘admin_init’, ‘set_locale_for_frontend_ajax_calls’ );

    add_action(‘wp_head’,’jsURLs’);

    function jsURLs(){

    global $q_config;

    ?><script type=”text/javascript”>
    /* <![CDATA[ */
    var ajaxurl = “<?php echo admin_url(‘admin-ajax.php?lang=’.get_locale()); ?>”;
    /* ]]> */
    </script><?php

    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘WordPress doesn't load correct mo-file via AJAX request’ is closed to new replies.