An issue with my custom component I am developing from scratch with 4.0 new ways of loading classes. I am following how com_acrticle, com_banner, and com_contact was created and how it loads classes via the Service folder. So, I created my own libraries of classes and it is loading automatically just fine using name classes anywhere I call it until I noticed Text class does not work anymore in my custom component classes unless my custom class extends Joomla's classes like HTMLView. This issue started when I first experienced and reported an issue with frontpage not loading a class #21177.
Prior to that, Text is working just fine. I guess this is related to the new class autoloader.
Labels |
Added:
?
|
Status | New | ⇒ | Information Required |
Category | ⇒ | Code style |
@mbabker Yes, I actually did verify that the language .ini file is loaded by enabling the language debug and the language translate text exists and loaded and there's no reported error on my ini file. Strange thing is, Text only work fin in HtmlView where it extends Joomla\CMS\MVC\View\HtmlView.
Labels |
Added:
J4 Issue
|
Text only work fin in HtmlView where it extends Joomla\CMS\MVC\View\HtmlView
Nothing in the view classes is language aware (as in loading language files, having a property based on the language API, etc.). Without seeing some very explicit code examples and knowing your expectations, there's nothing really for us to debug.
Title |
|
Hmmm... well as a work around, what I did is load the component language in my custom class __contruct. I don't know the proper way of doing it in J4, I just thought it is auto loaded. I guess I have to wait for the documentation. The core components does not really explain much why and how things are working which is my pattern. I'm no PhP expert and still trying to learn. Thanks!
Again, without knowing exactly what it is you're trying to accomplish it's hard to say what your specific issue is.
In the context of a normal request cycle, component language files are loaded when a component dispatcher is created in 4.0, in 3.x it was when ComponentHelper::renderComponent()
was called. So language files aren't loaded if you're randomly calling a class in your extension package from anywhere in the system (say a module or plugin using a helper class in your component), and we don't have a system that automatically loads in language files.
So your "workaround" might be the right solution for your specific "issue", or there very well may be some kind of bug somewhere. Again, it's hard to say because we really don't know what it is you're doing. If your component is the active component of the request cycle, its language files will be loaded (but if you're trying to use a class before that happens, say in a plugin subscribed to onAfterInitialise
or onAfterRoute
, at that point the language files won't be loaded because Joomla hasn't reached the point to load them yet). If it's not the active component for a request and you're trying to use a class, then the language files need to be manually loaded.
@mbabker Thank you for your explanation. Quick question, if I am currently on my custom component and I have created a Service class and I know it is loaded since I can see the class if I look at the service container.
On that service class, the Text object should be working properly, right?
I enabled the language debug and I know that my custom component language ini file is loaded and without error. But yet, Text is not translating the language.
I am not in a plugin or a module but rather I am in a component view which is calling a service class.
Is the language file loaded before or after the use in the service class? Debug might say it was loaded, but if it was loaded after you tried using it, that wouldn't help much.
The Text
class is just a fancy static wrapper around the global Language
object. It doesn't behave any differently based on where in the API you use it. Its only requirement is that Factory::getLanguage()
gives you a Language
object. So with that established, what is the state of the global Language
object when you're trying to use your language string(s)? Has the component language file actually been loaded at the time of use? The debug output is going to tell you what's loaded over the course of the request cycle, it's not telling you when that load happens.
This is only very loosely updated to not have a broken display with the 4.0 UI changes, but install it and it'll give you some extra debug data about the loaded language files in the debug console (@laoneo @wilsonge we really need a factory for Language class instantiation so this plugs in a bit more easily and some kind of early system hook that can hook the container even before we start executing the application, because if you look how this plugin actually runs right now it's re-instantiating the Language class because we lack a factory service or early system hook to overload a factory service).
@mbabker Yes! you're right, my custom component language is not loaded on the service class at the time Text is called.
I was assuming that language is magically loaded by Joomla framework anywhere in the component. So, I tested it in com_content service class (icon) and it's language ini is not loaded as well at the time icon object calls Text. So I guess it is by design, unless that was an unforeseen behavior. Quick question, where or how does HtmlView class magically load a component language? I can't find it!
Anyways, I tried loading the language in the service provider class and that seems to work. Now I don't have to load the component language in every service object when I need to use Text.
Thank you for the language debug plugin, it is a helpful tool indeed. It should be added to the core IMO.
@frogydiak if the Issue is solved please close it, thanks.
Status | Information Required | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-08-10 18:39:52 |
Closed_By | ⇒ | frogydiak |
Quick question, where or how does HtmlView class magically load a component language? I can't find it!
When you boot the component initially - it's not in the view at all :) Specifically in the components' dispatcher https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Dispatcher/LegacyDispatcher.php#L88
Have you verified the language file(s) in question are actually loaded?
There is nothing in the language API autoloading anything but overrides and
the default language file, extension files still require manual loading.
On Sat, Jul 28, 2018 at 6:13 PM FrogyDiak notifications@github.com wrote:
--