northcreationagency / silverstripe-lingo
在 SilverStripe 管理界面内翻译和处理 yml 文件中的键。
Requires
- silverstripe/admin: ^1.5
- silverstripe/framework: ^4.2
Requires (Dev)
- phpunit/phpunit: ^5.7
README
Lingo 允许开发者以与常规 lang 文件相同的样式在 yml 文件中定义键,但可以在 SilverStripe 管理界面中查看和翻译。
需求
- SilverStripe 4 (测试版本为 4.12 和 PHP 8.1.0)
安装
composer require northcreationagency/silverstripe-lingo
如何使用
在项目的 yml 配置文件中设置文件/文件夹的位置,用于处理文本,例如: app/_config/mysite.yml
moduleCatalog 是 textCatalog 所在的目录(在“第一级”)。
textCatalog 是将包含要由模块读取的 yml 文件的目录。
示例:app/lingotext
其中 app 是“moduleCatalog”,而 lingotext 是“textCatalog”。
NorthCreationAgency\SilverStripeLingo\Lingo:
moduleCatalog: app
textCatalog: lingotext
在 textCatalog 目录中放置一个或多个 yml 文件(每个语言一个)以处理您的文本。文件应遵循与 SilverStripes yml lang 文件相同的结构。示例
en:
List:
Header: 'This is a list header'
Company:
Header: This is a company header
同步文本到数据库
要将 yml 文件中的文本同步到数据库,请在构建 URL 中添加一个 URL 变量,如下所示:dev/build?synclingo=1
然后当您运行 dev/build?synclingo=1 时,yml 文件中的文本将被读取并存储在数据库中,并且可以从管理界面中进行编辑。构建时也会清除 Lingo 缓存。
您还可以添加一个额外的配置变量,如果希望在构建时始终同步 lingo 文本。如果您这样做配置,lingo 文本将在每次 dev/build 时读取并同步到数据库,无需使用 URL 变量。
NorthCreationAgency\SilverStripeLingo\Lingo:
moduleCatalog: app
textCatalog: lingotext
syncOnBuild: true
还有一个任务可以用于将文本同步到数据库。您可以通过 dev/tasks/SyncLingoTask 运行它。
用法
像使用 Silverstripe 翻译函数一样使用 SilverStripe 翻译函数。
它首先在常规 yml 文件中搜索实体。如果不存在,它将检查数据库中是否存在 Lingo 翻译实体。如果存在,该值将被缓存并返回。
在 PHP 函数中使用
// Simple string translation
_t('LeftAndMain.FILESIMAGES','Files & Images');
// Using injection to add variables into the translated strings.
_t('CMSMain.RESTORED',
"Restored {value} successfully",
['value' => $itemRestored]
);
在模板中使用
//with string
<%t Foo.BAR 'Bar' %>
//with variable
<%t Member.WELCOME 'Welcome {name} to {site}' name=$Member.Name site="Foobar.com" %>
