bnjmnhssnn/babelfisch

此包的最新版本(dev-master)没有可用的许可证信息。

在此处输入描述

dev-master 2021-01-26 18:53 UTC

This package is auto-updated.

Last update: 2024-09-27 03:06:55 UTC


README

网站国际化语言管理器

  • 按树状结构组织您的语言文件,例如文件系统中的文件夹或数据库中的父子层次结构
  • 支持类似mustache语法的嵌套语言文件
  • 动态注入附加数据

PHP Composer

您的语言文件夹的示例结构

语言文件必须以下一步使用的语言标识符结尾

languages/
├── button/
│   ├── buy_EN.txt
│   ├── buy_DE.txt
│   └── buy_NL.txt
├── greeting/
│    ├── welcome_EN.txt
│    ├── welcome_DE.txt
│    └── welcome_NL.txt
└── home/
     ├── main_paragraph_EN.txt
     ├── main_paragraph_DE.txt
     └── main_paragraph_NL.txt

无论您使用文件系统存储还是数据库,您都可以自由地组织您的语言文件。如果您选择层次结构,树状结构,您可以通过冒号分隔的标识符访问每个条目(参见 基本用法

获取Babelfisch实例

use \bnjmnhssnn\Babelfisch;
use \bnjmnhssnn\Babelfisch\StorageAdapter\FilesystemAdapter;

$bf = new Babelfisch(
    new FilesystemAdapter(__DIR__ . '/languages'),
    'EN', 'DE', 'NL' // Pass the active language first, then optional fallback languages
);

当找不到活动语言的相应语言文件时,Babelfisch将按照指定的顺序尝试加载回退语言。

基本用法

$buy_button = '<button>' . $bf->output('button:buy') . '</button>';

注入动态数据

languages/greetings/welcome_EN 的内容

Hello {{name}}, welcome to my awesome website!
echo $bf->output('greeting:welcome', ['name' => 'Ted']); // Hello Ted, welcome to my awesome website!

嵌套语言文件 & 动态数据

languages/home/main_paragraph_EN 的内容

{{greeting:welcome}}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.

首先,Babelfisch将使用语言文件 languages/greeting/welcome_EN.txt 中的内容解析 {{greeting:welcome}},然后使用第二个参数中的动态数据解析 languages/greeting/welcome_EN.txt 中的 {{name}}

echo $bf->output('home:main_paragraph', ['name' => 'Ted']);

当动态数据条目与语言文件具有相同的标识符时,动态数据将具有优先权

echo $bf->output(
    'home:main_paragraph', 
    [
        'name' => 'Ted',
        'greeting:welcome' => 'Hi {{name}},' // Will overwrite the content of languages/greeting/welcome
    ]
);

使用缓存

在大型且嵌套较深的语言文件的情况下,使用缓存模块可能更高效。

use \bnjmnhssnn\Babelfisch;
use \bnjmnhssnn\Babelfisch\StorageAdapter\FilesystemAdapter;
use \bnjmnhssnn\Babelfisch\Cache\FilesystemCache;

$bf = new Babelfisch(new FilesystemAdapter(__DIR__ . '/languages'), 'EN', 'DE', 'NL');
$bf->setCache(new FilesystemCache(__DIR__ . '/cache'));

当您将动态数据传递给输出方法时,缓存模块将为每个动态数据集生成 一个缓存文件,这在许多情况下是不希望的。尽管如此,您必须通过第三个布尔参数显式激活缓存,或者使用方便的方法 outputWithCache

echo $bf->output('very_large_paragraph', [], true);
// ... or with the convenient method "outputWithCache"
echo $bf->outputWithCache('very_large_paragraph');

缺失语言文件的策略

当语言文件及其所有回退文件都缺失时,Babelfisch默认会抛出异常。您可以设置4种不同的策略来自定义此行为。

// Throw an exception (default)
$bf->setNotFoundAction(Babelfisch::NOT_FOUND_ACTION_EXCEPTION);

// Output the requested ID for the missing content
$bf->setNotFoundAction(Babelfisch::NOT_FOUND_ACTION_SHOW_ID);

// Output an empty string for the missing content
$bf->setNotFoundAction(Babelfisch::NOT_FOUND_ACTION_EMPTY_STRING);

// Provide any callable that takes the ID as input (must return a string)
$bf->setNotFoundAction(
     function($id) {
          return "<strong style="background-color: red">{$id}</strong>";
     }
);