Jump to content

Hooks translations in multi-language addons


Eduardo G.

Recommended Posts

Hi all.

I have created several multi-language addons using "lang" folder inside the addon folder:
https://developers.whmcs.com/addon-modules/multi-language/

And it's ok as long I use those translations from _output and _sidebar.

But then my hooks cannot take that advantage.
They just can't see _LANG variables
Also method Lang::trans() is not valid because it doesnt take into account those custom addon's language files.

Of course I cannot ask the customers to edit their global lang overrides, also that wouldn't be correct.

So question is: how could I use custom addon own lang files and translation from inside this adddon's hook file?

TIA.

Link to comment
Share on other sites

There could be a better approach that I am not aware of but that's how I do it. When I started coding in WHMCS (2009) there were no Lang::trans() and autoloader so I had to find a way myself.

In my module hooks.php file I require my autoloader (it can also be a normal php file) where the constructor perfoms various things including the loading of my language files depending on current user/visitor language. I also take into account overrides directory if present in modules/{MyModule}/lang/overrides. This way customers can override $_ADDONLANG like they already do for $_LANG.

<?php

require 'my_module_autoloader.php';

add_hook('AnnouncementAdd', 1, function($vars) {

    $hook = Hook::getInstance($vars);

    die($hook->addonlang->welcome_message);
});

Over the years I used new(), singleton, and dependency injection approach. They all work.

Edited by Kian
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated