PHP code example of alnaggar / php-translation-files

1. Go to this page and download the library: Download alnaggar/php-translation-files 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/ */

    

alnaggar / php-translation-files example snippets


use Alnaggar\PhpTranslationFiles\Formats\Json\JsonFileLoader;

$loader = new JsonFileLoader();
$translations = $loader->load('path/to/translations/file.json');

use Alnaggar\PhpTranslationFiles\Formats\Json\JsonFileDumper;

$translations = [
    'welcome' => 'Welcome to our website!',
    'farewell' => 'Wishing you success on your new journey. Farewell!'
];

$dumper = new JsonFileDumper();
$dumper->dump(
    translations: $translations,
    path: 'path/to/translations/file.json', 
    flags: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT // A bitmask that controls the behavior of the JSON encoding process.
);

use Alnaggar\PhpTranslationFiles\Formats\Mo\MoFileLoader;

$loader = new MoFileLoader(
    contextDelimiter: '::',
    pluralDelimiter: '|'
);
$translations = $loader->load('path/to/translations/file.mo');

use Alnaggar\PhpTranslationFiles\Formats\Mo\MoFileDumper;

$metadata = [
    'Language' => 'en',
    'Project-Id-Version' => '1.0'
];

$dumper = new MoFileDumper(
    contextDelimiter: '::',
    pluralDelimiter: '|'
);

$dumper->dump(
    translations: $translations, 
    path: 'path/to/translations/file.mo',
    metadata: $metadata // An associative array to 

use Alnaggar\PhpTranslationFiles\Formats\Php\PhpFileLoader;

$loader = new PhpFileLoader();
$translations = $loader->load('path/to/translations/file.php');

use Alnaggar\PhpTranslationFiles\Formats\Php\PhpFileDumper;

$dumper = new PhpFileDumper();
$dumper->dump(
    translations: $translations,
    path: 'path/to/translations/file.php'
);

use Alnaggar\PhpTranslationFiles\Formats\Po\PoFileLoader;

$loader = new PoFileLoader(
    contextDelimiter: '::',
    pluralDelimiter: '|'
);
$translations = $loader->load('path/to/translations/file.po');

use Alnaggar\PhpTranslationFiles\Formats\Po\PoFileDumper;

$metadata = [
    'Language' => 'en',
    'Project-Id-Version' => '1.0'
];

$dumper = new PoFileDumper(
    contextDelimiter: '::',
    pluralDelimiter: '|'
);

$dumper->dump(
    translations: $translations, 
    path: 'path/to/translations/file.po',
    metadata: $metadata // An associative array to 

use Alnaggar\PhpTranslationFiles\Formats\Xliff\XliffFileLoader;

$loader = new XliffFileLoader();
$translations = $loader->load('path/to/translations/file.xliff');

use Alnaggar\PhpTranslationFiles\Formats\Xliff\XliffFileDumper;

$dumper = new XliffFileDumper();
$dumper->dump(
    translations: $translations, 
    path: 'path/to/translations/file.xliff',
    sourceLocale: 'en', // Specifies the source language of the translations.
    targetLocale: 'en', // Specifies the target language of the translations.
    legacy: false, // Determines whether to confrom to XLIFF 1.2 (`true`) or XLIFF 2.0 (`false`).
    fileId: 'en' // When `$legacy` is set to `false` (indicating XLIFF 2.0), this parameter is used to specify the `id` attribute for the `<file>` node in the XLIFF 2.0 structure.
);

use Alnaggar\PhpTranslationFiles\Formats\Yaml\YamlFileLoader;

$loader = new YamlFileLoader();
$translations = $loader->load('path/to/translations/file.yaml');

use Alnaggar\PhpTranslationFiles\Formats\Yaml\YamlFileDumper;

$dumper = new YamlFileDumper();
$dumper->dump(
    translations: $translations, 
    path: 'path/to/translations/file.yaml',
    dry: true // Determines whether to generate anchors and aliases for similar **mappings** in the YAML structure.
);

$loader->setMissingTranslationValueCallback(static function (string $key, string $path): string {
    error_log("Found an untranslated key: [{$key}] while loading the translations at [{$path}]");

    // Return a fallback value for missing keys.
    return strtoupper($key);
});

$translations = $loader->load('...');
bash
composer