0

On AJAX Call. It set to default language instead of current language. On AJAX route calling below function

public function ajaxList(Request $request) {
    $response = new AjaxResponse();

    $selector = $request->request->get('selector');
    $path = $request->request->get('base_path');

    $current_path = \Drupal\Core\Path\CurrentPathStack;

    $current_path->setPath('/' . $path, $request);

    $build = $this->processData($request);

    $response->addCommand(new ScrollTopCommand("$selector"));
    $response->addCommand(new ReplaceCommand("$selector", $build));

    return $response;
  }

In processData calling my Custom Form

 private function processData() {
    $form_state = (new FormState())
      ->setStorage([
        'rerender' => TRUE,
      ])
      ->setMethod('get')
      ->setAlwaysProcess()
      ->disableRedirect();

    $form_state->set('ajax', TRUE);

    $form = \Drupal::formBuilder()->buildForm('Drupal\my_module\Form\TestForm', $form_state);

    return $form;
  }

It will reset My Form's Label back to English.

  • How does your site determine current language? – Clive Mar 19 '18 at 9:21
  • On basis of $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); – Julia Mar 19 '18 at 9:40
  • I mean what negotiation method(s) are you using? – Clive Mar 19 '18 at 9:44
  • Didn't get u about negotiation method. – Julia Mar 19 '18 at 9:48
  • 2
    That's by language prefix. You need to make your AJAX request to the correct language domain (so you will need to prefix the path with /ch/). This will ensure that the response is generated in the correct language. – Jaypan Mar 20 '18 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.