Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Using Translation Labels in JavsScript

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 08-22-2016, 09:31 AM
 
Niboon Niboon is offline
 

Newbie
  
Join Date: Feb 2015
Posts: 6
 

Default Using Translation Labels in JavsScript

Hi all,

The .tpl scheme allows translation labels like {t(#Order payment#)} within .tpl files.
I was wondering what's the best way to retrieve similar translation labels through JavaScript.

For example I would like to add a Title to a part of the page but I would like that title to change languages with different translations that I add through the translation labels system.


Any help would be appreciated, Thanks!
-Niboon
__________________
Niboon -- Pathos Engineering

X-Cart 5.1.10 & 5.2beta
Reply With Quote
  #2  
Old 08-28-2016, 08:00 PM
  cherie's Avatar 
cherie cherie is offline
 

X-Wizard
  
Join Date: May 2003
Location: USA
Posts: 1,534
 

Default Re: Using Translation Labels in JavsScript

Untested:
Code:
<script> var myVar = '{t(#Order payment#)}' </script>
__________________
redlimeweb.com
custom mods and design integration
4.7 linux
Reply With Quote
  #3  
Old 08-31-2016, 01:22 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Using Translation Labels in JavsScript

Here is how you can do it:
Code:
var myLabel; // This should be located inside a function that is run after the JS code is loaded by XC5 if (!myLabel) { myLabel = core.t('My text'); } // Now myLabel contains the translation retrieved through an asynchronous request

However, the code above should be called after XC5 loads its JS core (otherwise the "core" will be unassigned). For example, you can put this code into your JS object extended from AController, that you load via core.autoload() function (see the source code for examples).
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #4  
Old 08-31-2016, 01:33 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Using Translation Labels in JavsScript

Here is another way that does not require asynchronous calls:
1. In your widget class you add getCommentedData() method and make it return an array of parameters and their values. Add your translation there:
PHP Code:
protected function getCommentedData()
{
    return array(
        
'lbl_welcome' => static::t('My label'),
    );

2. In the widget template you render these parameters:
Code:
<div id="myWidget"> {displayCommentedData(getCommendedData())} <!-- some other code --> </div>
3. In your widget class you add a custom JS file:
PHP Code:
public function getJSFiles()
{
    
$list parent::getJSFiles();
    
$list[] = 'modules/MyDevId/MyModuleId/controller.js';

    return 
$list;

4. In that JS file you can retrieve these parameters:
Code:
core.microhandlers.add( 'myCustomMicrohandlerName', '#myWidget', function () { var lbl_welcome = core.getCommentedData(this, 'lbl_welcome'); // ...other code } );
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions

Last edited by qualiteam : 08-31-2016 at 01:44 AM.
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 08:17 AM.

   

 
X-Cart forums © 2001-2020