2

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');
}
2
  • Could you add the code you use for the ajax integration in your form? How is the language negotiation configured (in URL, session, browser, ...)? Commented Oct 18, 2013 at 16:35
  • I have added the code. I use url detection for the language. nl/de/en
    – DD dev
    Commented Oct 18, 2013 at 21:17

4 Answers 4

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'];
}
3
  • 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
    Commented Oct 19, 2013 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/) Commented Oct 19, 2013 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
    Commented Oct 19, 2013 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.

1
  • This is not an answer.
    – tvanc
    Commented Mar 20, 2017 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.