定制 struktal/struktal-translator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

struktal/struktal-translator

最新稳定版本:1.1.0

Composer 安装命令:

composer require struktal/struktal-translator

包简介

PHP library for translating texts in Struktal applications

关键字:

README 文档

README

This is a PHP library for translating texts in Struktal applications.

Installation

To install this library, include it in your project using Composer:

composer require struktal/struktal-translator

Usage

Translations directory

In your project, you have to create a specific directory for translations. This directory then contains more subdirectories—one for each language or locale. Then, the subdirectories contain the translation files—one for each message domain:

📁 .../translations
├── 📁 en_US
│   ├── 📄 messages.json
│   └── 📄 emails.json
├── 📁 en_GB
│   ├── 📄 messages.json
│   └── 📄 emails.json
├── 📁 de_DE
│   ├── 📄 messages.json
│   └── 📄 emails.json
└── 📁 de_AT
    ├── 📄 messages.json
    └── 📄 emails.json

A translation file is a JSON file that contains key-value pairs for the translations. For example, the messages.json file for the en_US locale might look like this:

{
    "welcome_message": "Welcome to our application!",
    "goodbye_message": "Thank you for using our application!"
}

You can use placeholders in the translations, which will be replaced at runtime:

{
    "welcome_message": "Welcome to our application, $$username$$!",
    "goodbye_message": "Thank you for using our application, $$username$$!"
}

Setup

Before you can use this library, you need to customize a few parameters. You can do this in the startup of your application:

\struktal\Translator\Translator::setTranslationsDirectory("path/to/your/translations/root");
\struktal\Translator\Translator::setDomain("messages"); // Defines the file to read from the translations directory
\struktal\Translator\Translator::setLocale(
    \struktal\Translator\LanguageUtil::getPreferredLocale()
);

Then, you can use the library's features in your code.

Translate texts

To translate texts, you can use the Translator::translate() method:

\struktal\Translator\Translator::translate("welcome_message", [
    "username" => "John Doe"
]);

License

This software is licensed under the MIT license. See the LICENSE file for more information.

统计信息

  • 总下载量: 713
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-22