joylab / tarjim-php-client
用于使用 tarjim 翻译功能的 PHP 客户端
1.1.15
2024-09-12 08:24 UTC
README
设置
- 使用 composer require joylab/tarjim-php-client
- 在您的项目中创建包含以下内容的 php tarjim 配置文件
<?php
## Required
$project_id = '';
$cache_dir = full path to tarjim cache dir;
$logs_dir = full path to tarjim logs dir;
$apikey = '';
$default_namespace = '';
## Optional
$additional_namespaces = [];
## curl timeout for update cache api calls
$update_cache_timeout = 30;
## key_case
## defaults to 'lower'
## 'lower' => converts all keys from tarjim and keys passed to _T() functions to lowercase
## 'original' => preserves the keys' cases
$key_case = 'lower';
- 在上面的目录中创建 tarjim 缓存和日志文件
cd CACHE_DIR; touch translations.json translations_backup.json sanitized_html.json;
cd LOGS_DIR; touch errors.log update_cache.log;
- 为缓存、日志和配置文件设置权限
chmod -R 777 CACHE_DIR;
chmod -R 777 LOGS_DIR;
chmod 777 CONFIG_FILE;
使用方法
初始化
use Joylab\TarjimPhpClient\TarjimClient;
$TarjimClient = new TarjimClient(FULL_PATH_TO_CONFIG_FILE.CONFIG_FILE_NAME_WITH_EXTENSION);
$language = 'en';
$TarjimClient->setTranslations($language);
_T()
- 对于页面标题,添加配置 = ['is_page_title' => true]; 例如
<title><?=_T($title_for_layout, ['is_page_title' => true])?> | Panda7</title>
如果标题在控制器中已设置,则从控制器中删除对 _T() 的调用
- 对于占位符、下拉/选择选项、电子邮件主题和 swal,传递配置 skip_assign_tid = true
<input placeholder=<?=_T('placeholder', ["skip_assign_tid" => true])?> />
skip_assign_tid 也可以用于页面标题
在翻译值中使用变量
- 在 tarjim.io 中添加您想要的变量作为 %%variable_name%%
- 在视图中传递映射到配置
_T($key, [
'mappings' => [
'var1' => 'var1 value',
]
]);
使用 tarjim 处理媒体
- 调用 _TM($key, $attributes=[]) 函数
- _TI() 是 _TM() 的别名
- 使用示例
// optional
$attributes = [
class => 'img-class-name',
width => '100px'
]
<img <?=_TM($key, $attributes)?> />
renders <img src='src' class='img-class-name' width='100px' />
- 关于媒体属性的注意事项:如果 tarjim.io 收到的属性和函数调用收到的属性中存在相同的属性,则将覆盖函数调用收到的属性,因此在前面的示例中,如果此密钥具有属性:{class: 'class-from-tarjim', height:'200px'},__TM 将返回
<img src='src' class='class-from-tarjim' width='100px' height='200px'/>
注意 width 和 height 都已添加
使用 tarjim 处理数据集
- _TD($key, $config = []);
- 为键返回所有语言的值
[
'en' => 'en values,
'fr' => 'fr value'
]
- 如果 $namespace == 'all_namespaces',则配置可以 ['namespace' => $namespace] 返回所有命名空间中的值
[
'namespace 1' => [
'en' => 'en values,
'fr' => 'fr value'
],
'namespace 2' => [
'en' => 'en value',
'fr' => 'fr value'
]
]