定制 nixphp/i18n 二次开发

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

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

nixphp/i18n

最新稳定版本:v0.1.4

Composer 安装命令:

composer require nixphp/i18n

包简介

NixPHP Internationalization Plugin

README 文档

README

Logo

NixPHP I18n Plugin

← Back to NixPHP

nixphp/i18n

Simple JSON-based translations for your NixPHP application.

This plugin provides a lightweight translation system for multilingual apps. It reads language files from disk, supports variable replacements, and falls back gracefully — all with minimal overhead.

🧩 Part of the official NixPHP plugin collection. Install it if you want clean, flexible localization without external libraries.

📦 Features

  • Loads language files from app/Resources/lang/
  • Configure translation file path via config key app:translationPath (use language to set the language)
  • Supports t('key') with a fallback mechanism
  • Replaces variables via :name, :count, etc.
  • Language codes follow ISO 639-1 (e.g. en, de, fr)
  • JSON-based – easy to edit, export, and manage

📥 Installation

composer require nixphp/i18n

The plugin auto-registers and makes a t() as well as a translator() helper available globally. The t() function accepts a key and an optional array of replacements, while translator() is a shortcut to access the translator directly.

🚀 Usage

If you don't configure a language, the default language is English (en).

🔍 Translate

echo t('welcome');

Assuming app/Resources/lang/en.json contains:

{
  "welcome": "Welcome to our site!"
}

You’ll see: Welcome to our site!

✨ With replacements

echo t('greeting', ['name' => 'John']);

With this JSON entry:

{
  "greeting": "Hello, :name!"
}

Result: Hello, John!

🌍 Switch language

use NixPHP\I18n\Support\Language;

t()->setLanguage(Language::DE);

Make sure app/Resources/lang/de.json exists.

Through query parameter

/index.php?lang=de

An event listener will set the language based on the query parameter within a cookie.

🔄 Fallback

If a key is missing, the key itself is returned:

translator()->get('unknown_key');
// → "unknown_key"

This helps you spot missing translations during development.

📁 File structure

app/
└── Resources/
    └── lang/
        ├── en.json
        ├── de.json
        └── fr.json

Each file should be a flat key-value map using UTF-8 encoded JSON.

✅ Requirements

  • nixphp/framework >= 0.1.0
  • PHP >= 8.1

📄 License

MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固