承接 danil-kashin/localization 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

danil-kashin/localization

最新稳定版本:v1.0.0

Composer 安装命令:

composer require danil-kashin/localization

包简介

A PHP localization package for managing translations

README 文档

README

A lightweight PHP localization package for managing translations. Instantiate a Translator with a language and a path to your JSON translation files, and wire it up however your application requires.

Requirements

  • PHP 8.5+

Installation

composer require danil-kashin/localization

Quick start

Create a JSON translation file for each language you support:

translations/
    ru.json
    de.json
{
    "Hello": "Привет",
    "Goodbye": "До свидания"
}

Instantiate a Translator and call translate():

use DanilKashin\Localization\Translator;
use DanilKashin\Localization\Language;

$translator = new Translator(
    language: 'ru',
    translationsPath: __DIR__ . '/translations',
);

echo $translator->translate('Hello');   // Привет
echo $translator->translate('Goodbye'); // До свидания
echo $translator->translate('Missing'); // Missing  (falls back to original text)

Translations are loaded lazily on the first call to translate().

Languages

The Language class contains constants for all 184 ISO 639-1 language codes:

use DanilKashin\Localization\Language;

new Translator(Language::RU, __DIR__ . '/translations');
new Translator(Language::DE, __DIR__ . '/translations');
new Translator(Language::ZH, __DIR__ . '/translations');

The default language is Language::EN. If you need codes beyond ISO 639-1, extend the class:

class MyLanguage extends Language
{
    const string ZH_TW = 'zh-TW'; // Traditional Chinese
}

Normalizing language codes

Language::fromCode() normalizes arbitrary codes (e.g. from Accept-Language headers or Telegram's language_code) to a 2-character lowercase string:

Language::fromCode('en-US'); // 'en'
Language::fromCode('RU');    // 'ru'

Default language

The default language is Language::EN ('en'). Translating in the default language always returns the original text unchanged — no file is loaded and no missing-key tracking occurs.

You can change the default:

$translator = new Translator(
    language: 'de',
    translationsPath: __DIR__ . '/translations',
    defaultLanguage: 'de',
);

$translator->isDefaultLanguage(); // true
$translator->translate('Hello');  // 'Hello' (no translation attempted)

Tracking missing translations

Pass a MissingTranslationHandlerInterface to be notified whenever a key has no translation. The built-in JsonFileMissingTranslationHandler appends missing keys to a JSON file:

use DanilKashin\Localization\JsonFileMissingTranslationHandler;

$handler = new JsonFileMissingTranslationHandler(__DIR__ . '/missing.json');

$translator = new Translator(
    language: 'ru',
    translationsPath: __DIR__ . '/translations',
    missingHandler: $handler,
);

$translator->translate('An untranslated string');
// missing.json → { "ru": ["An untranslated string"] }

The file is created if it does not exist. Duplicate keys are never written twice.

Custom handler

Implement the interface to handle missing keys your own way (logging, alerting, etc.):

use DanilKashin\Localization\MissingTranslationHandlerInterface;

class LoggingHandler implements MissingTranslationHandlerInterface
{
    public function handle(string $language, string $text): void
    {
        // log or alert
    }
}

Dependency injection

Translator implements TranslatorInterface, so you can type-hint against the interface in your own code:

use DanilKashin\Localization\TranslatorInterface;

class MyService
{
    public function __construct(private readonly TranslatorInterface $translator) {}
}

License

BSD-3-Clause. See LICENSE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-04-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固