0

First of all, sorry for the really confusing title, but I have no idea how to make it more reader friendly. On my website, there are three languages: English, Finnish and Swedish. The user can change between these languages from the default Drupal language switcher. Now the key part of the website comes from an API that I think is in JS. That JS element has its own language changing parameter.

src="//www.websiteofapi.com/alpportal/oa_head.js?proj=api-of-our-company&key=api-key-goes-here&lang=sv&leaflet_gshim=1">

So in order to change the language of the JS element I would need to change that "lang=" part to what language I want. I have that script with the address to the API in my page.html.twig file to make the API call to work all across the site.

Is there a way to change the lang=sv to lang=en and lang=fi when the user changes the language through Druapl language switcher?

From the companys API support they said:

you have different pages for different languages: website.fi/drupal/ (englisch) website.fi/drupal/sv website.fi/drupal/fi This means that you should have a different script tag for each of these paths. I guess Drupal allows that with help of a language variable that is accessible in your page template. (But I really know little about Drupal, so that's just a guess)

I am sorry for a long and confusing post, but it is really crucial to get this to work and I am losing my mind. PS! I am really new to Drupal.

1

1 Answer 1

0

Something like this is what you're looking for I think.

theme_js_alter($js) {
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  foreach ($js as $key => $value) {
    if (strpos($value['data'], 'www.websiteofapi.com/alpportal/oa_head.js') !== 0) {
      $value['data'] = "//www.websiteofapi.com/alpportal/oa_head.js?proj=api-of-our-company&key=api-key-goes-here&lang=$language&leaflet_gshim=1"
    }
  }
}

Also see Get current language in template

1
  • Thanks! But where should I put this piece of code?
    – Hert Niks
    May 5, 2017 at 10:12

Your Answer

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

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