nmilosavljevic/po-editor

PHP 库,提供了用于编辑或创建 po 文件的 PHP 网页界面

dev-master 2019-04-14 21:53 UTC

This package is auto-updated.

Last update: 2024-09-15 10:13:26 UTC


README

PHP 库,提供了用于编辑或创建 po 文件的 PHP 网页界面。

致谢

此库高度依赖 https://github.com/oscarotero/Gettext

许可证

MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。

安装

使用 composer

composer require nmilosavljevic/po-editor

功能

生成翻译关键词

翻译关键词可以从 PHP 代码文件twig 文件 生成,未来将支持更多格式。PHP 代码文件和 twig 文件中的翻译可以合并成单个翻译文件。每次生成后,将删除未使用的关键词,并将新的关键词添加到翻译列表中。

读取 .PO 文件

可以从 PO 文件中读取现有翻译。

提供预先制作的用户界面进行翻译

用户可以使用预先制作的翻译表单,或者您可以构建自己的 UI 进行翻译。

导出到 .PO & .MO 文件

提供文件位置,可以将创建或编辑的翻译导出到 .PO 和 .MO 文件。

使用示例

实例化

	 $editor = new \NMilosavljevic\PoEditor\PoEditor();
	 //editor class has internal reference of translations
	 // $editor->translations

从 PHP 代码文件生成翻译

	$phpTranslations = $editor->fromPHPCodeFile([
	'functions' => ['__' => 'gettext'],
	'directories' => ['App/Helpers/']]);

从 TWIG 文件生成翻译

	$twigTranslations = $editor->fromTwigFile([
	'functions' => ['__' => 'gettext'],
	'directories' => [sprintf('%s/Helper', 'App/Template/)]]);

如果 TWIG 文件使用自定义函数进行翻译

Twig

	<h3><strong>{{ __("who_are_we") }}</strong></h3>

PHP

$twigTranslations = $editor->fromTwigFile([
'parser' => 'raw', //required
'functions' => ['__' => 'gettext'], //name of the function in twig file is __
 //path to the directory which contain multiple twigfiles
'directories' => [sprintf('%s/Template/Default', $portalPath)]]);`

合并翻译

 $twigTranslations->mergeWith($phpTranslations);

从 PO 文件读取

$editor->readFromPOFile('App/Locale/en/translations.po');

保存翻译

$editor->saveTranslationFiles('App/Locale/en/',$twigTranslations,'en','messages');
//or
$editor->SaveTranslationsToPoMoFile('App/Locale/en/');
//in this case, $editor->translations are used

获取编辑器 HTML

  $editor = new \NMilosavljevic\PoEditor\PoEditor();
   $shortcode = 'it';
  $portalPath = str_replace('Dashboard', 'Portal', PROJECT_DIR);
  $poFilePath = sprintf('%s/Locale/%s/LC_MESSAGES/messages.po',$portalPath,$shortcode);
   
   try{
        $twigTranslations = $editor->fromTwigFile([
             'parser' => 'raw',
             'functions' => ['__' => 'gettext'],
             'directories' => [sprintf('%s/Template/Default', $portalPath)]]);
   
        $phpTranslations = $editor->fromPHPCodeFile([
            'functions' => ['__' => 'gettext'],
             'directories' => [sprintf('%s/Helper', $portalPath)]]);
        
        //variables $twigTranslations and $phpTranslations are not used here, they are just declared
        //to show that function could return exported translations
        
        //in this case, translations exported from twig and php files are merged into $editor->translations
        //translations read from PO file will also be merged into $editor->translations
        
        $editor->readFromPOFile($poFilePath);
        $html = $editor->getEditorHTML();
        
        //render html in framework of your choice       

使用编辑器保存翻译

从编辑器提交翻译(通过点击保存按钮)将向同一 URL 发送带有参数翻译和可选参数语言的 POST 请求。

这些参数应传递给 saveFromEditor 函数

    $editor = new \NMilosavljevic\PoEditor\PoEditor();
    $shortcode = 'it';
    $portalPath = str_replace('Dashboard', 'Portal', PROJECT_DIR);
    $directory = sprintf('%s/Locale/%s/LC_MESSAGES/',$portalPath,$shortcode);
    $success = $editor->saveFromEditor($directory,$translations,'messages',$shortcode);
    //handle sending OK response here so that page could refresh.