compositephp/localization 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

compositephp/localization

最新稳定版本:v0.1.0

Composer 安装命令:

composer require compositephp/localization

包简介

Easy to use Localization and Pluralization, based on files

README 文档

README

Simple translation and pluralization php library based on files.

Overview:

Requirements

  • PHP 8.1+
  • PDO Extension with desired database drivers

Installation

Install package via composer:

$ composer require compositephp/localization

Localization

First, you need to create new class and extend from Composite\Localization\AbstractLocalization

use Composite\Localization\AbstractLocalization;
use Composite\Localization\Language;

class Localization extends AbstractLocalization
{
    public function __construct(Language $language)
    {
        parent::__construct(
            language: $language,
            sourcePath: '/directory/to/your/locale/files',
        );
    }

    public function general(string $text, array $placeholders = []): string
    {
        return $this->get('general', $text, $placeholders);
    }

    public function profile(string $text, array $placeholders = []): string
    {
        return $this->get('profile', $text, $placeholders);
    }
    //other categories
    //...
}

Usage:


Second, create category files in your localization source path /directory/to/your/locale/files:

- general.php
- profile.php

Example content of general.php

use Composite\Localization\Language;

return [
    'Hello World!' => [
        Language::FR->value => 'Bonjour le monde!'
    ],
    'hello_world' => [
        Language::EN->value => 'Hello World!',
        Language::FR->value => 'Bonjour le monde!',
    ],
    'Hello {{first_name}} {{last_name}}!' => [
        Language::FR->value => 'Bonjour {{first_name}} {{last_name}}!',
    ],
    'hello_fullname' => [
        Language::EN->value => 'Hello {{first_name}} {{last_name}}!',
        Language::FR->value => 'Bonjour {{first_name}} {{last_name}}!',
    ],
    'Hello {{name}}, you have {{num}} [[new_comment:num]]' => [
        Language::FR->value => 'Bonjour {{name}}, vous avez {{num}} [[new_comment:num]]',
    ],
];

Usage:

use Composite\Localization\Language;

$localization = new Localization(Language::FR);

$localization->general('Hello World!'); //Bonjour le monde!

$localization->general('hello_world'); //Bonjour le monde!

$localization->general(
    'Hello {{first_name}} {{last_name}}!', 
    ['first_name' => 'John', 'last_name' => 'Smith']
); //Bonjour John Smith!

$localization->general(
    'hello_fullname', 
    ['first_name' => 'John', 'last_name' => 'Smith']
); //Bonjour John Smith!

//if translation not exists localization instance will still output it as is

$localization->general('Bye World!'); //Bye World!

$localization->general('bye_world'); //bye_world

$localization->general(
    'Bye {{first_name}} {{last_name}}!', 
    ['first_name' => 'John', 'last_name' => 'Smith']
); //Bye John Smith!

Pluralization

Create a file _plural.php inside folder with translation categories /directory/to/your/locale/files:

use Composite\Localization\Language;
use Composite\Localization\Plurals\EnglishPlural;
use Composite\Localization\Plurals\FrenchPlural;

return [
    'new_comment' => [
        Language::EN->value => new EnglishPlural('new comment', 'new comments'),
        Language::FR->value => new FrenchPlural('nouveau commentaire', 'nouveaux commentaires'),
    ],
];

Usage:

$localization->general(
    'Hello {{name}}, you have {{num}} [[new_comment:num]]', 
    ['name' => 'John', 'num' => 1]
); //Bonjour John, vous avez 1 nouveau commentaire

$localization->general(
    'Hello {{name}}, you have {{num}} [[new_comment:num]]', 
    ['name' => 'John', 'num' => 2]
); //Bonjour John, vous avez 2 nouveaux commentaires

License:

MIT License (MIT). Please see LICENSE for more information. Maintained by Composite PHP.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固