PHP code example of orangecat / translate-ai

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

    

orangecat / translate-ai example snippets


namespace Your\Module\Model;

use Orangecat\TranslateAi\Api\TranslatorInterface;

class Translator implements TranslatorInterface
{
    public function translate(string $text, string $sourceLocale, string $targetLocale): string
    {
        // Your translation logic here
    }
    
    public function getCode(): string
    {
        return 'your_code'; // e.g., 'openai', 'deepseek'
    }
    
    public function getName(): string
    {
        return 'Your Provider Name';
    }
    
    public function isAvailable(): bool
    {
        // Check if properly configured (e.g., API key exists)
    }
    
    public function validateConfiguration(): array
    {
        // Return array of error messages or empty array if valid
    }
}

// Get active translator from configuration
$translator = $this->translatorPool->getActiveTranslator();

// Translate text
$translatedText = $translator->translate(
    'Hello World',
    'en_US',
    'es_ES'
);
xml
<module name="Your_TranslatorModule">
    <sequence>
        <module name="Orangecat_TranslateAi"/>
    </sequence>
</module>
xml
<type name="Orangecat\TranslateAi\Model\TranslatorPool">
    <arguments>
        <argument name="translators" xsi:type="array">
            <item name="your_code" xsi:type="object">Your\Module\Model\Translator</item>
        </argument>
    </arguments>
</type>