nixphp/i18n

NixPHP Internationalization Plugin

Maintainers

Details

github.com/nixphp/i18n

Source

Issues

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:nixphp-plugin

dev-main 2025-05-31 21:20 UTC

This package is auto-updated.

Last update: 2025-05-31 21:20:54 UTC


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/
  • ✅ Supports translator()->get('key') with 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 translator() helper available globally.

🚀 Usage

🔍 Get a translation

echo translator()->get('welcome');

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

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

You’ll see: Welcome to our site!

✨ With replacements

echo translator()->get('greeting', ['name' => 'Flo']);

With this JSON entry:

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

Result: Hello, Flo!

🌍 Switch language

translator()->setLocale('de');

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

🔄 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 >= 1.0
  • PHP >= 8.1

📄 License

MIT License.