Jump to content

Using different javascript for multilanguage website


Edward Ver
 Share

Recommended Posts

How to load a different js for another language? I'm new to PW, and my first time developing a multi language website in PW too. I came across a problem where I have to load a different js for english for example.

Norwegian (default lang): <script src="<?php echo $config->urls->templates; ?>assets/scripts/cookieconsent-init-no.js" defer></script>

How to load it if the url is mydomain.com/en.

 

Thank you in advance.

 

Link to comment
Share on other sites

That works! Thank you so much!

<?php if($user->language->name == "en-gb"): ?>
    <script src="<?php echo $config->urls->templates; ?>assets/scripts/cookieconsent-init-en.js" async></script>
    <?php else: ?>
        <script src="<?php echo $config->urls->templates; ?>assets/scripts/cookieconsent-init-no.js" async></script>
<?php endif; ?>

 

  • Like 2
Link to comment
Share on other sites

Hi,

just my two cents with a trick i use very often, not only for js files...
assuming your default language is norvegian, in your templates/_init.php file

$liso = $user->language->name == 'default' ? 'no' : $user->language->name;

(i use $liso for language iso but of course, the var name ia up to you) and then whereve you need a different file/img... named after the user language, let's do it with your js file

<script src="<?php echo $config->urls->templates; ?>assets/scripts/cookieconsent-init-<?= $liso; ?>.js" async></script>

will work whatrever number of languages your website uses
in case it may help

have a nice day

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...