3

To simplify the question and maybe reproduce the problem, I made a simplier version of what I am doing. I have a form field with ajax. When it triggers it reloads a small piece of html. In it is t('text in one of tree languages'); If I open the page for the first time the same html is loaded and it is in the correct language. But after the reload from the ajax call it is in the website's default language. This only happens when logged in. It is possible that this has to do with the administrator language module. Any help would very much be appreciated.

Ajax Code at hook_form

function hook_form($form, &$form_state) {
  $form['update'] = array(
    '#value' => t('Update'),
    '#type' => 'button',
    '#ajax' => array(
              'callback' => 'callback_function',
              'wrapper' => 'div-to-replace',
              'method' => 'replace',
              'effect' => 'fade')   
  );

  // Other form elements

}

Ajax callback function

function callback_function($form, $form_state) {
    return t('test string that should get translated');
}
  • Could you add the code you use for the ajax integration in your form? How is the language negotiation configured (in URL, session, browser, ...)? – Mike Vranckx Oct 18 '13 at 16:35
  • I have added the code. I use url detection for the language. nl/de/en – DD dev Oct 18 '13 at 21:17
1

The ajax callback should return the dynamic form element (and not directly the value). I've created some sample code where the translated content / string is correct after an ajax reload.

function YOURMODULE_form($form, &$form_state, $file_group)
{
    $form['update'] = array(
        '#value' => t('Update'),
        '#type' => 'button',
        '#ajax' => array(
            'callback' => 'YOURMODULE_form_ajax_cb',
            'wrapper' => 'ajaxWrapper',
            'method' => 'replace',
            'effect' => 'fade'
        )   
    );

    $form['ajax'] = array(
        '#type' => 'container',
        '#attributes' => array(
            'id' => 'ajaxWrapper',
        ),
    );

    $form['ajax']['text'] = array(
        '#type' => 'markup',
        '#markup' => t('My translated string'),
    );

    // Rest of the form
}

function YOURMODULE_form_ajax_cb($form, &$form_state)
{
    return $form['ajax'];
}
  • No this is not is. I found the problem all i need now is a sollution. I added the problem to my original question. – DD dev Oct 19 '13 at 12:34
  • If you disable the administration language module, do you still have the same problem? When Drupal executes the ajax, does the language is included in the URL request? (i.e.: /nl/system/ajax/) – Mike Vranckx Oct 19 '13 at 13:36
  • I removed system/ajax from the basic configuration. This however is likely to cause problems when making ajax calls on administrator pages wehere administrator language should be used. – DD dev Oct 19 '13 at 17:40
0

I see in my installations that when i disable the administration language module, the problem is solved :)

0

$referrer = explode('/', $_SERVER['HTTP_REFERER']);

$current_lang = $referrer[5];

This can solve your problem

-1

Having the same problem. On ajax callback t function is complealty ignored and strings are not even on TI. I can enforce them to appear in TI using locale function, but then I also need to pass language to it. What is the real problem is that I can't pass language to ajax callback, whenever I try to pass it, it goes to default english.

  • This is not an answer. – tvanc Mar 20 '17 at 16:12

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.