Edward Ver Posted November 29 Share Posted November 29 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 More sharing options...
elabx Posted November 29 Share Posted November 29 Try maybe: if($user->language->name == "en"){ //Processwire magic } 2 1 Link to comment Share on other sites More sharing options...
Edward Ver Posted November 29 Author Share Posted November 29 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; ?> 2 Link to comment Share on other sites More sharing options...
virtualgadjo Posted December 5 Share Posted December 5 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now