PHP code example of firewire / fluency

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

    

firewire / fluency example snippets


// Get the current language's ISO code
$fluency->getLanguageCode(); // => en-us

// Get the ISO code for another language using it's ProcessWire ID
$fluency->getLanguageCode(1034); // 'de'

$translation = $fluency->translate(
  sourceLanguage: 'EN',                         // Language code used by the translation service
  targetLanguage: 'DE',                         // Language code used by the translation service
  content: 'How do you do fellow developers?',  // String or array of strings to translate
  options: [],                                  // Translation Engine specific options (optional)
  caching: true                                 // Default is true, false disables, overrides module config
); // => EngineTranslationData

// Results may be accessed via properties on the return object, see toArray() example below for all
// properties present in the EngineTranslationData object
$translation->translations; // => ['Wie geht es Ihnen, liebe Entwickler?']
$translation->fromCache;    // => true

// $translation->toArray(); Outputs the following:
//
// array(10) {
//   'sourceLanguage' => 'EN'
//   'targetLanguage' => 'DE'
//   'content' => [
//     'How do you do fellow developers?'
//   ],
//   'translations' => [
//     'Wie geht es Ihnen, liebe Entwickler?'
//   ],
//   'options' => [],
//   'fromCache' => true,
//   'cacheKey' => 'ff6050ab0d048f7296214bfb21f18af707954dabf4df8013f3f013bc7382a73f',
//   'retrievedAt' => '2023-09-17T17:16:25-07:00',
//   'error' => NULL,
//   'message' => NULL,
// }

// Get the total number of translated strings returned
count($translation); // 1

$translation = $fluency->translate(sourceLanguage: 'EN', targetLanguage: 'DE', content: [
  'Must it be?',
  'It must be.',
]); // => EngineTranslationData

// $translation->toArray(); Outputs the following:
//
// array(10) {
//   'sourceLanguage' => 'EN'
//   'targetLanguage' => 'DE'
//   'content' => [
//     'Must it be?',
//     'It must be.'
//   ],
//   'translations' => [
//     'Muss das sein?',
//     'Es muss sein.'
//   ],
//   'options' => [],
//   'fromCache' => true,
//   'cacheKey' => '18af707954dabf4df8013f3f013bc7382a73fff6050ab0d048f7296214bfb21f',
//   'retrievedAt' => '2023-09-17T17:16:25-07:00',
//   'error' => NULL,
//   'message' => NULL,
// }
//
// Get the total number of translated strings returned
count($translation); // 2

$translatableLanguages = $fluency->getTranslatableLanguages(); // => EngineTranslatableLanguagesData

// $translatableLanguages->toArray(); Outputs the following:
//
//array(5) {
//   ["languages"]=>
//   array(31) {
//     [0]=>
//     object(Fluency\DataTransferObjects\EngineLanguageData)#2667 (7) {
//       ["sourceName"]=>
//       string(6) "Danish"
//       ["sourceCode"]=>
//       string(2) "DA"
//       ["targetName"]=>
//       string(6) "Danish"
//       ["targetCode"]=>
//       string(2) "DA"
//       ["meta"]=>
//       array(1) {
//         ["supports_formality"]=>
//         bool(false)
//       }
//       ["error"]=>
//       NULL
//       ["message"]=>
//       NULL
//     }
//     [1]=>
//     object(Fluency\DataTransferObjects\EngineLanguageData)#2668 (7) {
//       ["sourceName"]=>
//       string(5) "Dutch"
//       ["sourceCode"]=>
//       string(2) "NL"
//       ["targetName"]=>
//       string(5) "Dutch"
//       ["targetCode"]=>
//       string(2) "NL"
//       ["meta"]=>
//       array(1) {
//         ["supports_formality"]=>
//         bool(true)
//       }
//       ["error"]=>
//       NULL
//       ["message"]=>
//       NULL
//     }
//     [2]=>
//     object(Fluency\DataTransferObjects\EngineLanguageData)#2669 (7) {
//       ["sourceName"]=>
//       string(7) "English"
//       ["sourceCode"]=>
//       string(2) "EN"
//       ["targetName"]=>
//       string(18) "English (American)"
//       ["targetCode"]=>
//       string(5) "EN-US"
//       ["meta"]=>
//       array(1) {
//         ["supports_formality"]=>
//         bool(false)
//       }
//       ["error"]=>
//       NULL
//       ["message"]=>
//       NULL
//     }
//     // ... ommitted for brevity
//   }
//   ["fromCache"]=>
//   bool(true)
//   ["retrievedAt"]=>
//   string(25) "2023-10-27T04:12:12-07:00"
//   ["error"]=>
//   NULL
//   ["message"]=>
//   NULL
// }

// Additional methods are available as well, each return their own DTO

$danish = $translatableLanguages->bySourceCode('DA');             // => EngineLanguageData for Danish
$americanEnglish = $translatableLanguages->byTargetCode('EN-US'); // => EngineLanguageData for English

// Get the total number of translatable languages
count($translatableLanguages); // => 31

$configuredLanguages = $fluency->getConfiguredLanguages(); // => AllConfiguredLanguagesData

// $translatableLanguages->toArray(); Outputs the following:
//
// array(1) {
//   ["languages"]=>
//   array(6) {
//     [0]=>
//     object(Fluency\DataTransferObjects\ConfiguredLanguageData)#381 (7) {
//       ["id"]=>
//       int(1017)
//       ["title"]=>
//       string(7) "English"
//       ["default"]=>
//       bool(true)
//       ["isCurrentLanguage"]=>
//       bool(true)
//       ["engineLanguage"]=>
//       object(Fluency\DataTransferObjects\EngineLanguageData)#380 (7) {
//         ["sourceName"]=>
//         string(7) "English"
//         ["sourceCode"]=>
//         string(2) "EN"
//         ["targetName"]=>
//         string(18) "English (American)"
//         ["targetCode"]=>
//         string(5) "EN-US"
//         ["meta"]=>
//         array(1) {
//           ["supports_formality"]=>
//           bool(false)
//         }
//         ["error"]=>
//         NULL
//         ["message"]=>
//         NULL
//       }
//       ["error"]=>
//       NULL
//       ["message"]=>
//       NULL
//     }
//     [1]=>
//     object(Fluency\DataTransferObjects\ConfiguredLanguageData)#384 (7) {
//       ["id"]=>
//       int(1028)
//       ["title"]=>
//       string(6) "French"
//       ["default"]=>
//       bool(false)
//       ["isCurrentLanguage"]=>
//       bool(false)
//       ["engineLanguage"]=>
//       object(Fluency\DataTransferObjects\EngineLanguageData)#383 (7) {
//         ["sourceName"]=>
//         string(6) "French"
//         ["sourceCode"]=>
//         string(2) "FR"
//         ["targetName"]=>
//         string(6) "French"
//         ["targetCode"]=>
//         string(2) "FR"
//         ["meta"]=>
//         array(1) {
//           ["supports_formality"]=>
//           bool(true)
//         }
//         ["error"]=>
//         NULL
//         ["message"]=>
//         NULL
//       }
//       ["error"]=>
//       NULL
//       ["message"]=>
//       NULL
//     }
//     // ...omitted for brevity
//   }
// }

// Additional methods are available as well, each return their own DTO. Examples based on the return value shown above:

$configuredLanguages->getDefault();                         // => ConfiguredLanguageData for English
$configuredLanguages->getCurrent();                         // => ConfiguredLanguageData for English
$configuredLanguages->getByProcessWireId(1028)              // => ConfiguredLanguageData for French
$configuredLanguages->getBySourceCode('FR')                 // => ConfiguredLanguageData for French
$configuredLanguages->getByTargetCode('EN-US')              // => ConfiguredLanguageData for English
$configuredLanguages->getBySourceName('French')             // => ConfiguredLanguageData for French
$configuredLanguages->getByTargetname('English (American)') // => ConfiguredLanguageData for English

// Since these return ConfiguredLangaugeData objects, you can access very useful information quickly.

$currentLanguage = $configuredLanguages->getCurrent(); // => ConfiguredLanguageData for English
$currentLanguage->id;                                  // => 1017
$currentLanguage->title;                               // => 'English' (Returned localized if translated in ProcessWire)
$currentLanguage->default;                             // => true

// All properties under engineLanguage are provided by the Translation Engine
// The source/target language codes can be used when calling `$fluency->translate()`;

$currentLanguage->engineLanguage->sourceCode;          // => 'EN'
$currentLanguage->engineLanguage->targetCode;          // => 'EN-US'
$currentLanguage->engineLanguage->sourceName;          // => 'English'
$currentLanguage->engineLanguage->targetName;          // => 'English (American)'

// Get the number of ProcessWire langauges configured in Fluency
count($configuredLanguages); // => 6

$usage = $fluency->getTranslationApiUsage(); // => EngineApiUsageData

// $usage->toArray(); Outputs the following:
//
// array(7) {
//   ["used"]=>
//   int(222822)
//   ["limit"]=>
//   int(500000)
//   ["remaining"]=>
//   int(277178)
//   ["percentUsed"]=>
//   string(3) "45%"
//   ["unit"]=>
//   string(9) "Character"
//   ["error"]=>
//   NULL
//   ["message"]=>
//   NULL
// }

$engineInfo = $fluency->getTranslationEngineInfo(); // => EngineInfoData

// $engineInfo->toArray() outputs the following:
//
// array(12) {
//   ["name"]=>
//   string(5) "DeepL"
//   ["version"]=>
//   string(3) "1.1"
//   ["provider"]=>
//   string(5) "DeepL"
//   ["providerApiVersion"]=>
//   string(3) "2.0"
//   ["providerApiDocs"]=>
//   string(31) "https://www.deepl.com/docs-api/"
//   ["configId"]=>
//   string(64) "f71ee845229943abd3e5863227d5706600f472b3d31b4de4768878a072b681f1"
//   ["providesUsageData"]=>
//   bool(true)
//   ["details"]=>
//   string(645) "{Engine description as shown on the Fluency config page}"
//   ["authorName"]=>
//   string(8) "FireWire"
//   ["authorUrl"]=>
//   string(51) "https://processwire.com/talk/profile/3976-firewire/"
//   ["error"]=>
//   NULL
//   ["message"]=>
//   NULL
// }

// Get the current number of cached translations:
$fluency->getCachedTranslationCount(); // Int

// Clear the translation cache:
$fluency->clearTranslationCache(); // 0 on success

// Check if the list of languages translatable by the translation engine are currently cached:
$fluency->translatableLanguagesAreCached(); // Bool

// Clear the translatable languages cache:
$fluency->clearTranslatableLanguagesCache(); // 0 on success

$endpoints = $fluency->getApiEndpoints(); // => stdClass

// object(stdClass)#376 (6) {
//   ["endpoints"]=>
//   string(19) "/processwire/fluency/api/"
//   ["usage"]=>
//   string(25) "/processwire/fluency/api/usage/"
//   ["translation"]=>
//   string(31) "/processwire/fluency/api/translation/"
//   ["languages"]=>
//   string(29) "/processwire/fluency/api/languages/"
//   ["translationCache"]=>
//   string(37) "/processwire/fluency/api/cache/translations"
//   ["translatableLanguagesCache"]=>
//   string(34) "/processwire/fluency/api/cache/languages"
// }