folded/translation

为您的Web应用程序翻译术语。

v0.1.0 2020-10-16 18:35 UTC

This package is auto-updated.

Last update: 2024-09-17 03:33:33 UTC


README

为您的Web应用程序翻译术语。

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

摘要

关于

我创建此包是为了在Web应用程序中独立、简单地使用翻译。此库基于Laravel的翻译引擎。

Folded是一系列包,可以帮助您轻松设置Web应用程序,使用即插即用的包。

  • folded/action:组织您Web应用程序的控制器的方法。
  • folded/config:为您的PHP Web应用程序提供配置工具。
  • folded/crypt:为您的Web应用程序加密和解密字符串。
  • folded/exception:为您的Web应用程序抛出各种类型的异常。
  • folded/file:使用您的Web应用程序的功能操作文件。
  • folded/history:操作您的Web应用程序的浏览器历史记录。
  • folded/http:为您的Web应用程序提供HTTP工具。
  • folded/orm:为您的Web应用程序提供ORM。
  • folded/routing:为您的PHP Web应用程序提供路由函数。
  • folded/request:为您的PHP Web应用程序提供请求工具,包括请求验证器。
  • folded/session:为您的Web应用程序提供会话函数。
  • folded/view:为您的PHP Web应用程序提供视图工具。

功能

  • 可以翻译所需语言的术语
  • 可以设置默认语言以回退
  • 可以同时使用json和基于键的翻译
  • 可以使用复数翻译

要求

  • PHP版本 >= 7.4.0
  • 已安装Composer

安装

1. 安装包

在您的根目录中,运行以下命令

composer required folded/translation

2. 准备文件夹

要工作,您需要有一个包含您的翻译的文件夹。这是推荐的组织结构

.
└── lang/
    ├── en
    └── fr

在lang文件夹内部,您可以使用任何符合您需求的组织结构,从基于键的翻译到基于json的翻译。

当使用基于键的翻译时,您可能会将包含键和翻译术语的任何文件放在相应的文件夹中,取决于语言。以下是一个示例。

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

例如,messages.php文件可以为en文件夹包含以下内容

return [
  "home" => [
    "title" => "Welcome in the home page",
  ],
];

而为fr文件夹包含以下内容

return [
  "home" => [
    "title" => "Bienvenue sur la page d'accueil",
  ],
];

但是,如果您愿意,您可以使用基于json的翻译。使用JSON格式翻译文件的用例之一是当您需要使用翻译术语作为键时。基于json的翻译文件必须放在lang文件夹中,除非是之前的基于键的方法。以下是一个示例

.
└── lang/
    ├── en/
    │   └── messages.php
    ├── fr/
    │   └── messages.php
    ├── en.json
    └── fr.json

以下是en.json的示例内容

{
  "Contact us for a custom tailored quotation": "Contact us for a custom tailored quotation"
}

这里就是fr.json的内容

{
  "Contact us for a custom tailored quotation": "Contactez nous pour un devis sur-mesure"
}

3. 添加引导代码

尽早配置库

use function Folded\setDefaultTranslationLang;
use function Folded\setTranslationFolderPath;

setDefaultTranslationLang("en");
setTranslationFolderPath("path/to/folder");

示例

请记住,任何时候如果你有疑问,都可以参考官方Laravel翻译文档

1. 通过键获取翻译项

在这个例子中,我们将从一个基于键的翻译中获取翻译项。

use function Folded\getTranslation;

echo getTranslation("messages.home.title");

这意味着你有以下文件夹结构

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

你也可以从原始文本本身获取翻译项。为此,我们建议使用基于JSON的翻译。

use function Folded\getTranslation;

echo getTranslation("Contact us for a custom tailored quotation");

这意味着你有以下文件夹结构

.
└── lang/
    ├── en.json
    └── fr.json

2. 在翻译项中使用占位符

在这个例子中,我们将在一个包含占位符的翻译中放置值。

use function Folded\getTranslation;

echo getTranslation("messages.home.welcome", ["name" => "John"]);

这意味着你的翻译类似于以下内容

// lang/en/messages.php
return [
  "home" => [
    "welcome" => "Welcome, :name",
  ],
];

3. 获取复数形式的翻译项

在这个例子中,我们将获取可复数术语的翻译。

use function Folded\getVariableTranslation;

$numberOfPageViewed = 5000;

echo getVariableTranslation("messages.home.page-viewed", $numberOfPageViewed);

这意味着你有以下文件夹结构

.
└── lang/
    ├── en/
    │   └── messages.php
    └── fr/
        └── messages.php

并且你的en文件夹中的messages.php文件包含

return [
  "home" => [
    "page-viewed" => "{0} No page viewed|[1] One page viewed|[2,*] :count page viewed",
  ]
];

更多信息,请浏览Laravel复数翻译文档

4. 在获取翻译项前更改语言

在这个例子中,我们将在获取翻译项之前更改语言。

use function Folded\setTranslationLang;
use function Folded\getTranslation;

setTranslationLang("fr");

echo getTranslation("messages.home.title");

版本支持