PHP code example of vanengers / catalog-translator

1. Go to this page and download the library: Download vanengers/catalog-translator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vanengers / catalog-translator example snippets


$translator = new Translator(
    MessageCatalogue $extracted,
    array $translateTo => [],
    ?ITranslateClient $external = null,
    string $translations_config_file = ''
);

$extracted = new \Symfony\Component\Translation\MessageCatalogue('en-GB',[
    'domain' => [
        'This is a base language key' => 'This is a base language key'
    ]
]);

$translateTo = ['nl-NL', 'de-DE'];

$external = new \Vanengers\CatalogTranslator\Client\DeeplClient([
    'api_key' => 'your-deepl-api-key'
]);

$translations_config_file = __DIR__ . '/translations.json';
 
class CustomTranslator implements ITranslateClient {}

$external = new \Vanengers\CatalogTranslator\Client\DeeplClient([
    'api_key' => 'your-deepl-api-key'
]);

$languages = $external->getSourceLanguages();

public function translate(string $text, string $source, string $target): string
{
    $source = $this->fromLocaleToIso($source);
    $target = $this->fromLocaleToIso($source, true);
    return $this->api->translateText($text, $source, $target);
}
 
public function getSourceLanguages(): array
{
    return array_map(function(Language $language) {
        return new Lang($language->code, $language->name, $this->fromIsoToLocale($language->code, false));
    }, $this->api->getSourceLanguages());
}