scafera/translate
Composer 安装命令:
composer require scafera/translate
包简介
Translation for the Scafera framework
关键字:
README 文档
README
Translation for the Scafera framework. UI string translation with locale management, RTL support, and Twig integration.
Internally adopts symfony/translation. Userland code never imports Symfony Translation types — boundary enforcement blocks it at compile time.
Provides: UI string translation for Scafera —
Translatorfor lookup,LocaleManagerfor locale switching (with RTL detection). Flat JSON translation files with{name}parameter syntax (not Symfony's%name%). Whenscafera/frontendis installed, a companion bundle registers{{ t() }}and{{ locale_direction() }}Twig functions automatically.Depends on: A Scafera host project whose architecture package defines a translations directory via
getTranslationsDir()(e.g.resources/translations/inscafera/layered). Twig integration activates only whenscafera/frontendis installed.Extension points: None of its own —
TranslatorandLocaleManagerare consumed as services. New locales are picked up by adding{locale}.jsonfiles to the translations directory. Locale behavior is tuned via two parameters inconfig/config.yaml:scafera.translate.default_localeandscafera.translate.rtl_locales.Not responsible for: Non-JSON translation formats (no YAML, no XLIFF — deliberate) · multi-level fallback chains (single fallback to default locale only) · choosing the translations directory (architecture package owns
getTranslationsDir()) · direct use ofSymfony\Component\Translationtypes in userland (blocked byTranslateBoundaryPassandTranslateBoundaryValidator).
This is a capability package. It adds optional translation to a Scafera project. It does not define folder structure or architectural rules — those belong to architecture packages.
What it provides
Translator— get translated strings with{name}parameter replacementLocaleManager— locale switching, available locales, RTL detectionJsonFileLoader— loads flat JSON translation files (internal)- Twig integration —
{{ t() }}and{{ locale_direction() }}(whenscafera/frontendis installed) - Companion bundle auto-discovery via
extra.scafera-bundlesin composer.json
Design decisions
{name}parameter syntax, not%name%— translation files use{name}for parameter placeholders. This is engine-independent (Symfony uses%name%) and human-readable. Replacement is done after Symfony'strans()call via simplestrtr().- Translations directory is architecture-owned — defined by
getTranslationsDir()on the architecture package, not hardcoded. Forscafera/layered, this isresources/translations/. - JSON only — no YAML, no XLIFF. Flat key-value format. Keeps translation files simple and parseable by any tooling.
- Companion bundle for Twig — the
{{ t() }}function is registered viaextra.scafera-bundlescompanion discovery (ADR-056), not by requiringscafera/frontendas a dependency. The translate package works without Twig. - Missing keys return the raw key —
get('MISSING')returns'MISSING', not an error. Fallback to default locale is automatic for keys that exist in another locale.
Installation
composer require scafera/translate
Requirements
- PHP >= 8.4
- scafera/kernel
Translation files
JSON files in the architecture-defined translations directory (e.g., resources/translations/{locale}.json):
{
"WELCOME": "Welcome to the app!",
"GREETING": "Hello, {name}!"
}
Flat key-value format. No nesting, no YAML, no XLIFF. Parameters use {name} syntax — not Symfony's %name% format. This keeps translation files engine-independent and human-readable.
Usage
use Scafera\Translate\Translator; $translator->get('WELCOME'); // 'Welcome to the app!' $translator->get('GREETING', ['name' => 'Alice']); // 'Hello, Alice!' $translator->has('WELCOME'); // true $translator->getLocale(); // 'en' $translator->getDirection(); // 'ltr'
Missing keys return the raw key string. Fallback to the default locale is automatic.
Locale management
use Scafera\Translate\LocaleManager; $localeManager->setLocale('ar'); $localeManager->getLocale(); // 'ar' $localeManager->getDirection(); // 'rtl' $localeManager->getDirection('en'); // 'ltr' $localeManager->getAvailableLocales(); // ['ar', 'en']
Available locales are discovered from *.json files in the translations directory.
Twig integration
When scafera/frontend is installed, a companion bundle registers automatically:
<html dir="{{ locale_direction() }}"> <h1>{{ t('WELCOME') }}</h1> <p>{{ t('GREETING', {name: user.name}) }}</p> </html>
Configuration
All configuration is optional. The package works out of the box with sensible defaults.
| Parameter | Default | Description |
|---|---|---|
scafera.translate.default_locale |
en |
Default locale used when no locale is set. Also used as the fallback locale when a key is missing in the current locale. |
scafera.translate.rtl_locales |
[ar, fa, ur] |
Locale codes that use right-to-left text direction. LocaleManager::getDirection() returns 'rtl' for these. |
Not configurable via parameters:
- Translation files directory — defined by the architecture package via
getTranslationsDir(). Forscafera/layered, this isresources/translations/. - File format — JSON only. No YAML, no XLIFF.
- Fallback chain — always falls back to the default locale. No multi-level fallback.
To override defaults, add to config/config.yaml:
# config/config.yaml parameters: scafera.translate.default_locale: fr scafera.translate.rtl_locales: [ar, fa, ur]
If you are using en as your default locale and the standard RTL set, no configuration is needed.
Boundary enforcement
| Blocked | Use instead |
|---|---|
Symfony\Component\Translation\* |
Scafera\Translate\Translator |
Enforced via compiler pass (build time) and validator (scafera validate). Detects use, new, and extends patterns.
License
MIT
scafera/translate 适用场景与选型建议
scafera/translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「locale」 「translate」 「scafera」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 scafera/translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 scafera/translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 scafera/translate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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
An Astrotomic Translatable extension for Laravel Nova.
腾讯翻译君翻译、百度翻译 、谷歌翻译(Google 翻译)
DeepL integration for Craft CMS
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-14