Skip to content

Custom Loader

Michael Spiss edited this page Aug 17, 2017 · 4 revisions

Loaders are used to parse data from any file format into an array.

Creating a custom loader is a painless, three step process:

  1. Create a new class which implements MichaelSpiss\Translation\LoaderInterface
<?php

use MichaelSpiss\Translation\LoaderInterface;

class CustomLoader implements LoaderInterface {

    public function getContent( string $file ): array { }

}
  1. Implement the getContent() method. For further information about this method see the method definition below.

  2. After that you only have to make sure that your loader is known to the Translator by calling its addLoader method after initialization:

$translator = new Translator('en', 'path/to/translations');
$translator->addLoader('yml', new YamlLoader());

getContent

public function getContent( string $file ): array

Gets translation data from a file and turns it into an array.

Arguments:

  • string $file: The file which needs to be parsed

Returns:

The parsed array. Should be of the following format:

[
    'key' => 'translation string',
    'another' = [
        'subkey' => 'another string'
    ]
]
Clone this wiki locally