How to get translation from other module in PrestaShop?

ぃ、小莉子 提交于 2019-12-02 16:21:24

问题


I have a "baseModule" PrestaShop module and a bunch of "baseExtensionModule[n]" modules.

So, in order to avoid redundancy, I would like to reuse some translation from "baseModule" within the others.

I have checked Translate::getModuleTranslation() and it looks like ModuleCore::l() does not offer the possibility to pass a module name and forward it to the first one.

Any workaround you may know for this?

I guess that getting the "baseModule" module instance would be another way to do it - using it's l() method instead of the current $this->l. How can I get an instance for another module?


回答1:


Here's a little snippet I use in my ajax files:

$module_name = 'mymodule';

if (Module::isInstalled($module_name) && Module::isEnabled($module_name))
{
    $mod = Module::getInstanceByName($module_name);

    if (method_exists($mod, 'doSomething'))
        $mod->doSomething();
}

Now you could use Module::getInstanceByName('mymodule')->l('string'), but I almost sure it would work.

This is because translations controller scans for $this->l\((.*)\) inside module folder using regex and adds the translatable strings to a file. Since Module::getInstanceByName('modulename')->l('') doesn't match this pattern, strings wouldn't even be recognized.

If you want the string to accessible via Module::getInstanceByName('modulename')->l('string1'), $this->l('string1') must exist inside base module file, it will scanned and added to translations.

Another way I can recommend is to use a static variable to hold your translations:

public static $l = array();

public function __construct()
{
    ...
    $this->init();
}

public function init()
{
   if (self::isInstalled($this->name)) {
      self::$l = array(
         'string1' => $this->l('string1'),
      );
   }
}

Then use Module::getInstanceByName to get the instance and access the static variable.

I actually use this way to translate model field names, because there aren't many alternatives.



来源:https://stackoverflow.com/questions/27952621/how-to-get-translation-from-other-module-in-prestashop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!