add custom api loader symfony

I’m trying to create a loader to load the translations received from an api and even though I’ve implemented it, it seems there is still something that I have to do, and do not know what.

How I load it:

$translator = new Symfony_Component_Translation_EmsTranslator($lang, new MessageFormatter());
$translator->addLoader('custom', new CustomLoader());

CustomLoader class:

class CustomLoader extends FileLoader {
/**
 * {@inheritdoc}
 */
protected function loadResource($resource)
{
    $messages = [];
    $translations = new ApiClient();
    $data = $translations->getTranslations();

    $messages = json_decode($data, true);

    if (0 < $errorCode = json_last_error()) {
       throw new InvalidResourceException('Error parsing JSON: ' . $this->getJSONErrorMessage($errorCode));
    }

    return $messages;
}

But my function loadResource is not even called. So not sure what’s the best thing to do here. I understand that basically my CustomLoader(which retrives data from an api) basically will not use a file, but still. Any ideas?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *