PHP code example of bluepsyduck / factorio-translator

1. Go to this page and download the library: Download bluepsyduck/factorio-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/ */

    

bluepsyduck / factorio-translator example snippets


use BluePsyduck\FactorioTranslator\Translator;

$translator = new Translator();

use BluePsyduck\FactorioTranslator\Loader\ModArchiveLoader;
use BluePsyduck\FactorioTranslator\Loader\ModDirectoryLoader;

$translator->addLoader(new ModDirectoryLoader())
           ->addLoader(new ModArchiveLoader());


$translator->loadMod('/path/to/factorio/data/core')
           ->loadMod('/path/to/factorio/data/base')
           ->loadMod('/path/to/factorio/mods/my-fancy-mod_1.33.7.zip');

use BluePsyduck\FactorioTranslator\Processor\Placeholder\PositionPlaceholderProcessor;
use BluePsyduck\FactorioTranslator\Processor\Placeholder\EntityPlaceholderProcessor;
use BluePsyduck\FactorioTranslator\Processor\Placeholder\ItemPlaceholderProcessor;
use BluePsyduck\FactorioTranslator\Processor\Placeholder\PluralPlaceholderProcessor;

$translator->addProcessor(new PositionPlaceholderProcessor()) // Handles e.g. __1__
           ->addProcessor(new EntityPlaceholderProcessor())   // Handles e.g. __ENTITY__iron-ore__
           ->addProcessor(new ItemPlaceholderProcessor())     // Handles e.g. __ITEM_electronic-circuit__
           ->addProcessor(new PluralPlaceholderProcessor());  // Handles e.g. __plural_for_parameter_1_{...}__

use BluePsyduck\FactorioTranslator\Processor\Placeholder\AbstractControlPlaceholderProcessor;
use BluePsyduck\FactorioTranslator\TranslatorAwareInterface;
use BluePsyduck\FactorioTranslator\TranslatorAwareTrait;

$translator->addProcessor(
    new class extends AbstractControlPlaceholderProcessor implements TranslatorAwareInterface {
        use TranslatorAwareTrait;

        protected function processControl(string $locale, string $controlName, int $version): ?string
        {
            // Use the translated name of the control (as it appears in the options menu), 
            // and put it into square brackets. 
            $control = $this->translator->translateWithFallback($locale, ["controls.{$controlName}"]);
            return "[{$control}]"; 
        }
    }
);

echo $translator->translate('en', ['item-name.electronic-circuit']); // Electronic circuit
echo $translator->translate('de', ['item-name.electronic-circuit']); // Elektronischer Schaltkreis