如何从 PrestaShop 中的其他模块获取翻译? [英] How to get translation from other module in PrestaShop?

查看:41
本文介绍了如何从 PrestaShop 中的其他模块获取翻译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个baseModule"PrestaShop 模块和一堆baseExtensionModule[n]"模块.

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

所以,为了避免冗余,我想在其他人中重用来自baseModule"的一些翻译.

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

我已经检查了 Translate::getModuleTranslation() 并且看起来 ModuleCore::l() 没有提供传递模块名称并将其转发给第一个的可能性.

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?

我想获取baseModule"模块实例将是另一种方法 - 使用它的 l() 方法而不是当前的 $this->l.如何获取另一个模块的实例?

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?

推荐答案

这是我在我的 ajax 文件中使用的一个小片段:

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

现在你可以使用 Module::getInstanceByName('mymodule')->l('string'),但我几乎可以肯定它会起作用.

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

这是因为翻译控制器使用正则表达式扫描模块文件夹中的 $this->l\((.*)\) 并将可翻译字符串添加到文件中.由于 Module::getInstanceByName('modulename')->l('') 与此模式不匹配,因此甚至无法识别字符串.

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.

如果您希望通过 Module::getInstanceByName('modulename')->l('string1') 访问字符串,$this->l('string1') 必须存在于基本模块文件中,它将被扫描并添加到翻译中.

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

然后使用Module::getInstanceByName获取实例并访问静态变量.

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.

这篇关于如何从 PrestaShop 中的其他模块获取翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆