X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Using Translation Labels in JavsScript (https://forum.x-cart.com/showthread.php?t=74343)

Niboon 08-22-2016 09:31 AM

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

cherie 08-28-2016 08:00 PM

Re: Using Translation Labels in JavsScript
 
Untested:
Code:

<script>
var myVar = '{t(#Order payment#)}'
</script>


qualiteam 08-31-2016 01:22 AM

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).

qualiteam 08-31-2016 01:33 AM

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
  }
);



All times are GMT -8. The time now is 11:11 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.