4

I'm developing a module, in my template (MyModule/tmpl/default.php) I'm able to bring my language string to my script by doing this :

 <script type="text/javascript"> 
Mymodule.options.mydz = {
    DefaultMessage: "<?php echo JText::_('MOD_MYMODULE_DEFAULT_MESSAGE');?>" }; 
</script>

I'm wondering if I could call my text with Javascript and without MooTools. I found this :

<script type="text/javascript">
Mymodule.options.mydz = {
    DefaultMessage: (Joomla.JText._('MOD_MYMODULE_DEFAULT_MESSAGE'))
};
</script>

But instead of showing my text the browser return "undefined"... I don't know if it still applies, but I read that's because Joomla.JText._ is a function who needs MooTools.

Should I keep showing text via PHP or there is a better way of using multi-language?

3
  • Using PHP is the simplest way to overcome your problem. Why make things complicated? Whole Joomla is based on PHP.
    – saibbyweb
    Oct 11, 2016 at 16:40
  • 1
    Thanks for your answer, Why make things complicated? : Because I wish to use Ajax and give different answers without refreshing the page, my goal is to improve user experience. Otherwise my second answer is probably not a good answer, but I will go with "Curiosity" I also like to learn new stuff, learn ways of doing things and understand Joomla! programmation. Oct 12, 2016 at 16:55
  • Ajax is definitely worth a shot! Even I use it in my custom PHP modules.
    – saibbyweb
    Oct 13, 2016 at 6:28

2 Answers 2

2

According to this issue, as of Joomla 3.3.0 core.js was rewritten in jQuery, including Joomla.JText. If you're using that version or later and core.js is loading on our page, then the Mootools issue shouldn't be the cause. If core.js isn't present on the page then you'll want to include it via JHtmlBehavior::core() and try again.

As for the question of whether to use PHP or Joomla.JText... I don't think that in the scope of this site (because it's a opinion question about coding styles).

1

Very well, I use this structure to do the translations in my JS, come on:

  1. In the view.html.php file structure put this command

    JText :: script ('fixed translation variable');
    
  2. Now you can use it in JS referenced in view.html.php and add

    Joomla.JText ._ ('fixed translation variable')
    

Ready with that we have the use of translation.

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.