Download the PHP package alnaggar/php-translation-files without Composer
On this page you can find all versions of the php package alnaggar/php-translation-files. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alnaggar/php-translation-files
More information about alnaggar/php-translation-files
Files in alnaggar/php-translation-files
Package php-translation-files
Short Description A php package for simple, robust loaders and dumpers for common translation file formats. It aims to make conversion and manipulation of translation resources easy for CLI scripts, build steps, or server-side tooling.
License MIT
Informations about the package php-translation-files
PhpTranslationFiles
PhpTranslationFiles provides simple, robust loaders and dumpers for common translation file formats. It aims to make conversion and manipulation of translation resources easy for CLI scripts, build steps, or server-side tooling.
Supported formats:
- JSON
- MO (Machine Object)
- PHP
- PO (Portable Object)
- XLIFF (XML Localization Interchange File Format)
- YAML
Table of Contents
- Requirements
- Installation
- Usage
- Handling Missing Translation Values
- Exceptions & Error Handling
- Contributing
- Credits
- License
Requirements
- PHP 7.3+
ext-jsonPHP extensionext-pcrePHP extension
Installation
Install via Composer:
Usage
Each format has a Loader and a Dumper. Loaders implement load(string $path): array and return an array of translations. Dumpers implement dump(array $translations, string $path, ...$options): static which writes the file and returns the dumper instance.
JSON
Load:
Dump:
MO
Load:
Dump:
PHP
Load:
Dump:
PO
Load:
Dump:
XLIFF
Load:
Dump:
YAML
Both the YAML loader and dumper support only simple YAML structures, including mappings, nested mappings, and scalar values. Keys and scalar values may be double-quoted, single-quoted, or unquoted (as long as they do not contain special YAML characters).
Load:
Dump:
Handling Missing Translation Values
All loaders inherit a method to set a callback for handling missing translation values. If a value is '' or null, the loader will use the translation key as a fallback, so missing translations are visibly flagged in the UI for correction.
To override this behavior, pass a callback to setMissingTranslationValueCallback() which receives:
- The missing translation
$key - The
$pathof the translations file holding the key
The callback function is responsible for determining and returning the appropriate value.
Example:
Exceptions & Error Handling
Loaders and dumpers throw specific exceptions to help you handle errors properly:
Alnaggar\PhpTranslationFiles\Exceptions\FileNotFoundException— file not found (invalid path)Alnaggar\PhpTranslationFiles\Exceptions\FileLoadException— failed to open/read fileAlnaggar\PhpTranslationFiles\Exceptions\FileParsingException— file content could not be parsed (invalid JSON/YAML/PO/etc.)Alnaggar\PhpTranslationFiles\Exceptions\InvalidFileException— file structure is invalid (e.g., non-UTF8 YAML, invalid MO header)Alnaggar\PhpTranslationFiles\Exceptions\DirectoryNotFoundException— target directory does not exist and cannot be createdAlnaggar\PhpTranslationFiles\Exceptions\FileDumpException— failed to write output file
Contributing
If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on the GitHub repository.
Credits
- Palestine banner and badge by Safouene1.
License
PhpTranslationFiles is open-sourced software licensed under the MIT license.
All versions of php-translation-files with dependencies
ext-json Version *
ext-pcre Version *