承接 lantosbro/cli-localizer 相关项目开发

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

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

lantosbro/cli-localizer

最新稳定版本:1.0.0

Composer 安装命令:

composer require lantosbro/cli-localizer

包简介

A library for localization in CLI applications

README 文档

README

CliLocalizer is a PHP library for easy localization in CLI applications with support for multiple translation file formats (JSON, YAML, PHP(Array) and MO).

Installation

You can install the package via composer:

composer require lantosbro/cli-localizer

Requirements

  • PHP 7.4 or higher
  • PHP YAML extension
  • PHP Gettext extension

To install the YAML extension:

pecl install yaml

To install the Gettext extension:

pecl install gettext

Then add extension=yaml.so and extension=gettext.so to your php.ini file.

Usage

use CliLocalizer\Translator;
use CliLocalizer\CliOutput;

// Initialize the Translator with the required translations path, format, and optional locale and fallback locale
$translator = new Translator('/path/to/your/translations', 'mo', 'ru_RU', 'en_US');

// Create a CliOutput instance
$output = new CliOutput($translator);

// Use the output
$output->writeln('welcome_message', ['name' => 'User']);
$output->writeln('processing_files', ['count' => 5]);

Configuration

You must specify the path to your translations directory and the format of your translation files when initializing the Translator. Supported formats are:

  • JSON ('json')
  • YAML ('yaml')
  • PHP ('php')
  • MO ('mo')

Create translation files in your specified translations directory using the chosen format.

Example translation files:

JSON (en_US.json):

{
    "welcome_message": "Welcome, :name!",
    "processing_files": "Processing :count files..."
}

YAML (en_US.yaml):

welcome_message: "Welcome, :name!"
processing_files: "Processing :count files..."

PHP (en_US.php):

<?php
return [
    'welcome_message' => 'Welcome, :name!',
    'processing_files' => 'Processing :count files...',
];

For MO files, you need to compile them from PO files using tools like msgfmt. The structure should be similar to other formats, with keys being the original strings and values being the translations.

The Translator will attempt to load translations in the following order:

  1. The specified locale (e.g., 'ru_RU')
  2. The language code of the specified locale (e.g., 'ru')
  3. The fallback locale (default is 'en_US')

Ensure that you have at least a translation file for your fallback locale.

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-14