nabeghe/light-localization
Composer 安装命令:
composer require nabeghe/light-localization
包简介
A light weight, key-value & path-based PHP localization library that translations are loaded up when needed.
README 文档
README
A lightweight, key-value & path-based PHP localization library.
Translation groups are loaded lazily — only when first accessed.
Requirements
- PHP >= 7.4
- nabeghe/atlin
Installation
composer require nabeghe/light-localization
Directory Structure
Organize your translations like this:
/langs/
/en/
main.php
messages.php
extra.atlin
/fa/
main.php
extra.atlin
- Each sub-directory represents a locale code.
- Each file inside is a translation group (identified by filename without extension).
- Files can be
.php(returning an array) or.atlinformat. .atlinfiles take precedence over.phpwhen both exist for the same group name.
Quick Start
use Nabeghe\LightLocalization\Localizer; $en = new Localizer(__DIR__ . '/langs', 'en'); $fa = new Localizer(__DIR__ . '/langs', 'fa', $en); // $en is the fallback // Reads from main.php (default group) echo $fa->get('title'); // Reads from messages.php echo $fa->get('success', 'messages'); // Returns a random item when the value is an array echo $fa->rnd('greet');
Constructor
new Localizer( string $path, string $locale = 'en', Localizer|string|null $fallback = null, ?string $defaultTranslationText = null, ?Atlin $atlin = null );
| Parameter | Description |
|---|---|
$path |
Absolute path to the translations root directory |
$locale |
Active locale code |
$fallback |
Another Localizer to chain, or a plain string used as default |
$defaultTranslationText |
Returned when no translation is found anywhere in the chain |
$atlin |
Custom Atlin instance; created automatically if null |
Translation Files
PHP
Return a plain associative array. Values can be strings or arrays of strings (for rnd()).
<?php // langs/en/main.php return [ 'title' => 'Hello World', 'greet' => ['Hi!', 'Hey!', 'Hello!'], ];
Atlin
Standard key-value .atlin format:
@title
Hello Atlin World
@description
A lightweight localization library
API Reference
Locale
| Method | Description |
|---|---|
getLocale(): string |
Returns the current locale |
setLocale(string $locale): void |
Changes the locale and clears all loaded groups |
getLastLocale(): ?string |
Locale used in the most recent successful lookup |
Lookup
| Method | Description |
|---|---|
get(string $key, string $group = 'main', bool $deep = true): ?string |
Returns the translation for a key |
rnd(string $key, string $group = 'main', bool $deep = true): ?string |
Like get(), but picks a random item when the value is an array |
has(string $key, string $group = 'main', bool $deep = true): bool |
Checks whether a key exists |
Group Management
| Method | Description |
|---|---|
load(string $group): bool |
Loads a translation group from disk |
refresh(): void |
Reloads all currently loaded groups |
reset(): void |
Clears all loaded groups from memory |
resetTranslation(string $group): bool |
Removes a specific group from memory |
hasTranslation(string $group): bool |
Checks whether a group is loaded in memory |
hasTranslationFile(string $group): bool |
Checks whether a group file exists on disk |
Fallback & Default Text
| Method | Description |
|---|---|
getFallback() |
Returns the current fallback (Localizer or string) |
setFallback(Localizer|string|null $fallback): void |
Sets a new fallback |
getDefaultTranslationText(): ?string |
Returns the static default text |
setDefaultTranslationText(?string $text): void |
Sets the static default text |
Fallback Chain
When a key is not found in the active locale, the library resolves it in this order:
- Current locale group (lazy-loaded from disk)
- Fallback
Localizer(if provided), recursively - Fallback string (if provided instead of a Localizer)
defaultTranslationText(constructor parameter / setter)null
Running Tests
composer test
License
Licensed under the MIT License.
nabeghe/light-localization 适用场景与选型建议
nabeghe/light-localization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 109 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「language」 「l10n」 「translator」 「localization」 「translate」 「lang」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nabeghe/light-localization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nabeghe/light-localization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nabeghe/light-localization 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
Easy to use internationalization functions for Laravel
Set Links with a specific language parameter
统计信息
- 总下载量: 109
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-19