定制 davidlienhard/i18n 二次开发

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

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

davidlienhard/i18n

最新稳定版本:2.1.1

Composer 安装命令:

composer require davidlienhard/i18n

包简介

🐘 php library to use for internationalization

README 文档

README

🐘 php library to use for internationalization

Latest Stable Version Source Code Software License Minimum PHP Version CI Status

Setup

You can install through composer with:

composer require davidlienhard/i18n:^2

Note: davidlienhard/i18n requires PHP 8.0

How to use

1. Create language files

Create at least one language file. Supported filetypes are json, yaml, yml or ini.

./lang/en.yml (English)

save: Save
greeting: Hi %1

./lang/de.yml (German)

save: Speichern
greeting: Hallo %1

2. Load the class

Use composer autoloader if possible or include the files in the src folders manually

3. Create the object

<?php declare(strict_types=1);

use DavidLienhard\i18n\i18n;

$i18n = new i18n;

4. Set the options

you can either set some options right through the constructor or via the set methods

$i18n = new i18n(
    filePath: "./lang/{LANGUAGE}.yml",
    cachePath: "./cache/",
    fallbackLang: "en",
    prefix: "L"
);

$i18n->setNamespace("YourApp\Translations");

The following setter methods are available:

  • setFilePath(string $filePath): Sets the path to the language files
  • setCachePath(string $cachePath): Sets the path to the cache directory
  • setFallbackLang(string $fallbackLang): sets a fallback language
  • setMergeFallback(bool $mergeFallback): whether or not to merge the fallback language
  • setPrefix(string $prefix): Sets the prefix/name of the class to contain the translations
  • setForcedLang(string $forcedLang): a language that is forced to be used
  • setSectionSeparator(string $sectionSeparator): the character to use to concatenate sections

5. Initialize the class / create cache-files

$i18n->init();

Thss will then create the cache file if required and load the new translation data with the given namespace & prefix/class-name.

6. Use the translation data

use YourApp\Translations\L;

echo L::save;                       // Save / Speichern
echo L::get("save");                // Save / Speichern
echo L::greeting("David");          // Hi David // Hallo David
echo L::get("greeting", "David");   // Hi David // Hallo David

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-14