Jump to content

Translation in module Controller ?


Recommended Posts

Hi

 

In my module, I have an AdminController inside which I would like to write some texts and make its translatable.

In Controller construct function, I've created an instance :

$module_instance = new mymodule;

Then, in Controller functions, I use this, for example :

$module_instance->l('Delete selected');

I can retrieve "Delete selected" in Prestashop back-office / translations.

But It never translates when I show my module controller.

 

How must I do ?

Thanks

Link to comment
Share on other sites

 

as tu essayé

require_once('../mymodule.php');
$module_instance = new MyModule();

Oui, j'ai bien le require_once de mon module.

Tu veux dire qu'il faut que je Capitalize le nom du module ?

En réalité il s'appelle edelweiss_revenues.

J'ai essayé $module_instance = Edelweiss_Revenues();

sans succès.

 

 

Hi,

 

In controller it should be:

$this->module->l('Translatable string');

on frontend:

$this->module->l('Translatable string', 'controller_name');

$this->module is the name of my module.

In controller construct :

$this->module = 'edelweiss_revenues';

of course, if I try 

$this->module->l('Translatable string');

I have an error Call to a member function l() on a non-object

Link to comment
Share on other sites

dans ton module tu as

class MonModule extends Module

il faut reprendre la casse MonModule et il faut éviter les _ dans les nom de modules, je ne sais pas si ça peut influer sur les traductions

 

we must take the break MyModule and should be avoided _ in the name of modules, I do not know if it can affect translations

Link to comment
Share on other sites

yes, in my module I have

class edelweiss_revenues extends Module

and in my controller : 

require_once _PS_MODULE_DIR_ . 'edelweiss_revenues/edelweiss_revenues.php';

class AdminRevenuesStatsController extends AdminController
{
    public function __construct()
    {
        $this->revenues = new edelweiss_revenues;
...

$this->revenues->l('text to translate'); -> it doesn't work.

I will try to remove the underscore in my module name.

Link to comment
Share on other sites

sinon essaye 
 
 

require_once('../mymodule.php');
$module_instance = new MyModule();

et dans mymodule.php, dans n'importe quelle fonction, rajoute la ligne

$this->l('Delete selected');

et ensuite utilise la fonction de traduction des modules dans le back office pour traduire 'Delete selected'

Link to comment
Share on other sites

coeos.pro - please do not use French, here on English forum.

And secondly, your solution is wrong. Totally wrong.

 

Problem must be somewhere else.

 

ChDUP - are you sure that you're have everything ok witihn your __construct method? parent::construct at the end of constructor in your class which extends ModuleAdminController?

 

Also, just to make sure, you're trying to translate it in Localization -> Translations -> Installed modules -> Core (no theme), right?

Link to comment
Share on other sites

Here is My admin Controller :

<?php
require_once _PS_MODULE_DIR_ . 'edelweiss_revenues/edelweiss_revenues.php';

class AdminRevenuesStatsController extends ModuleAdminController
{
    public function __construct()
    {
        $this->name = 'revenues';
        $this->lang = false;
        $this->context = Context::getContext();
        $this->id_lang = (int)$this->context->language->id;
        $this->bootstrap = true;

        parent::__construct();
    }

    public function postProcess()
    {
        echo($this->module->l('Text to translate'));
        die();

I've translated the 'Text to translate' in Localization -> Translations -> Installed modules -> Core and also in Localization -> Translations -> Installed modules -> MyTheme

 

no success.

Link to comment
Share on other sites

ok

just have

echo($this->l('Text translate'));
die();

in my postProcess function.

no PHP cache extension

and I see my translation changes in my module (in module / configuration) but still not in my controller.

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 months later...

UP

 

need help please

 

I don't know exactly what your code does look like, but in my case, I have an controller override where I need to obtain some translation labels from the global lang theme directory.

 

I build and translate override function which behaves like the Translate.php getModuleTranslation() function.

 

See my attached php Translate file.

 

Usage:

 

$txt = Translate::getControllerTranslation('product', 'full');

 

where 'product' is the translation view from the ProductController in PS BO translation page and 'full' is the desired translated label. Via this code you access the $_LANG gloabl variable (and not the $_MODULE like in previous examples)

Edited by prestowicz (see edit history)
Link to comment
Share on other sites

  • 2 years later...

Hello

The solution I found to this problem was to use single quote inside l() function and add controller name to l() function like the following:

$this->module->l('Your text.', 'Example')

You should not pass controller class name, just use controller name.
My controller class name is MymoduleExampleModuleFrontController but I have to use the key word Example because it is actual name of controller.

Edited by ELEGANTAL (see edit history)
Link to comment
Share on other sites

  • 1 year later...

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
×
×
  • Create New...