定制 alnaggar/php-translation-files 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alnaggar/php-translation-files

Composer 安装命令:

composer require alnaggar/php-translation-files

包简介

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.

README 文档

README

I Stand With Palestine Badge

I Stand With Palestine Banner

Latest Stable Version Total Downloads License

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:

Table of Contents

Requirements

  • PHP 7.3+
  • ext-json PHP extension
  • ext-pcre PHP extension

Installation

Install via Composer:

composer require alnaggar/php-translation-files

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:

use Alnaggar\PhpTranslationFiles\Formats\Json\JsonFileLoader;

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

Dump:

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.
);

MO

Load:

use Alnaggar\PhpTranslationFiles\Formats\Mo\MoFileLoader;

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

Dump:

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 include additional information about the translation file, such as language, authorship, or pluralization rules.
);

PHP

Load:

use Alnaggar\PhpTranslationFiles\Formats\Php\PhpFileLoader;

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

Dump:

use Alnaggar\PhpTranslationFiles\Formats\Php\PhpFileDumper;

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

PO

Load:

use Alnaggar\PhpTranslationFiles\Formats\Po\PoFileLoader;

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

Dump:

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 include additional information about the translation file, such as language, authorship, or pluralization rules.
);

XLIFF

Load:

use Alnaggar\PhpTranslationFiles\Formats\Xliff\XliffFileLoader;

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

Dump:

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.
);

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:

use Alnaggar\PhpTranslationFiles\Formats\Yaml\YamlFileLoader;

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

Dump:

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.
);

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 $path of the translations file holding the key

The callback function is responsible for determining and returning the appropriate value.

Example:

$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('...');

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 file
  • Alnaggar\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 created
  • Alnaggar\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

License

PhpTranslationFiles is open-sourced software licensed under the MIT license.

alnaggar/php-translation-files 适用场景与选型建议

alnaggar/php-translation-files 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「translation」 「gettext」 「localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 alnaggar/php-translation-files 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alnaggar/php-translation-files 我们能提供哪些服务?
定制开发 / 二次开发

基于 alnaggar/php-translation-files 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-13