leaseweb/gettext-translation-bundle

易于使用且本地的(更快)gettext翻译支持

v1.1.1 2018-02-12 21:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 01:55:29 UTC


README

LswGettextTranslationBundle将gettext翻译支持添加到您的Symfony4应用程序中。它旨在比Symfony4内置的翻译支持更快、更易于使用。

需求

  • PHP带gettext支持
  • Symfony 4+

安装

安装分为以下步骤

  1. 使用composer下载LswGettextTranslationBundle
  2. 启用Bundle
  3. 安装所需的区域设置
  4. 在应用程序中设置语言

步骤1:使用composer下载LswGettextTranslationBundle

在composer.json中添加LswGettextTranslationBundle

{
    "require": {
        "leaseweb/gettext-translation-bundle": "*",
        ...
    }
}

现在运行以下命令让composer下载bundle

$ php composer.phar update leaseweb/gettext-translation-bundle

Composer会将bundle安装到项目的vendor/leaseweb目录中。

步骤2:启用bundle

在kernel中启用bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Lsw\GettextTranslationBundle\LswGettextTranslationBundle(),
    );
}

步骤3:安装所需的区域设置

这里所述,您可以使用命令'locale -a'列出您系统上安装的区域设置。如果您想支持荷兰语、英语和德语,应执行以下命令

sudo locale-gen nl_NL.UTF-8
sudo locale-gen en_US.UTF-8
sudo locale-gen de_DE.UTF-8

更多语言代码可以在这里找到

步骤4:在应用程序中设置语言

使用标准$request->setLocale('en');来设置应用程序中的区域设置。

编辑以下文件以定义区域设置的2个字母快捷键(这建议这么做)

Lsw/GettextTranslationBundle/Resources/config/config.yml

用法

用法分为以下步骤

  1. 在代码中使用gettext(便捷)函数
  2. 使用gettext从bundle中提取需要翻译的字符串(.pot文件)
  3. (首次使用)在bundle中初始化您要支持的语言(.po文件)
  4. (跳过首次使用)使用gettext模板(.pot)文件更新语言(.po)文件
  5. 使用优秀的Poedit应用程序翻译语言文件
  6. 将所有翻译合并到一个文件(.mo文件)

步骤1:在代码中使用gettext(便捷)函数

您可以使用以下函数

  • _($text)gettext 的快捷方式
  • _n($textSingular,$textPlural,$n)ngettext 的快捷方式
  • __($format,$args,...)sprintf(_($format),$args,...)) 的快捷方式
  • __n($formatSingular,$formatPlural,$n,$args,...)sprintf(_n($formatSingular,$formatPlural,$n),$args,...)) 的快捷方式

步骤2:从bundle中提取字符串

使用./app/console gettext:bundle:extract命令搜索bundle中的翻译字符串,并将其存储到gettext模板(.pot)文件中。

步骤3:(首次使用)初始化您要支持的语言

使用./app/console gettext:bundle:initialize命令将gettext模板(.pot)文件复制到特定语言的(.po)文件中。

步骤4:(跳过首次使用)使用模板更新gettext语言(.po)文件

使用 Poedit 应用程序加载 gettext 语言 (.po) 文件。选择“从模板更新”选项,并将 Poedit 指向生成的 gettext 模板 (.pot) 文件。查看并确认更改。

步骤 5:使用卓越的 Poedit 应用程序进行语言翻译

使用 Poedit 应用程序加载 gettext 语言 (.po) 文件。翻译所有缺失的字符串(以蓝色显示)并检查和纠正所有模糊翻译的字符串(以黄色显示)。

步骤 6:合并所有翻译

使用 ./app/console gettext:combine 命令将所有 gettext 语言 (.po) 文件合并成一个编译后的 gettext (.mo) 文件。