问题
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