扫描目录中的gettext字符串,并使用zerospam/laraavel-gettext包将它们添加到.po文件中



有没有一种方法可以扫描目录中的gettext字符串,并以编程方式将它们添加到.po文件中,这意味着不必通过Poedit来实现这一点?

我完成了文档中列出的正常安装配置步骤,但当执行php artisan gettext:createphp artisan gettext:update命令时,它似乎只生成只包含标头的空.po文件。

laravel-gettext.php :

<?php

return [
/**
* Translation handlers, options are:
*
* - symfony: (recommended) uses the symfony translations component. Incompatible with php-gettext
* you must uninstall the php-gettext module before use this handler.
*
* - gettext: requires the php-gettext module installed. This handler has well-known cache issues
*/
'handler' => 'symfony',
/**
* Session identifier: Key under which the current locale will be stored.
*/
'session-identifier' => 'laravel-gettext-locale',
/**
* Default locale: this will be the default for your application.
* Is to be supposed that all strings are written in this language.
*/
'locale' => 'en_US',
/**
* Supported locales: An array containing all allowed languages
*/
'supported-locales' => [
'en_US',
],
/**
* Default charset encoding.
*/
'encoding' => 'UTF-8',
/**
* -----------------------------------------------------------------------
* All standard configuration ends here. The following values
* are only for special cases.
* -----------------------------------------------------------------------
**/
/**
* Locale categories to set
*/
'categories' => [
'LC_ALL',
],
/**
* Base translation directory path (don't use trailing slash)
*/
'translations-path' => '../resources/lang',
/**
* Relative path to the app folder: is used on .po header files
*/
'relative-path' => '../../../../../app',
/**
* Fallback locale: When default locale is not available
*/
'fallback-locale' => 'en_US',
/**
* Default domain used for translations: It is the file name for .po and .mo files
*/
'domain' => 'messages',
/**
* Project name: is used on .po header files
*/
'project' => 'MultilanguageLaravelApplication',
/**
* Translator contact data (used on .po headers too)
*/
'translator' => 'James Translator <james@translations.colm>',
/**
* Paths where Poedit will search recursively for strings to translate.
* All paths are relative to app/ (don't use trailing slash).
*
* Remember to call artisan gettext:update after change this.
*/
'source-paths' => [
'Http',
'../resources/views',
'Console',
],
/**
* Multi-domain directory paths. If you want the translations in
* different files, just wrap your paths into a domain name.
* for example:
*/
/*
'source-paths' => [
// 'frontend' domain
'frontend' => [
'controllers',
'views/frontend',
],
// 'backend' domain
'backend' => [
'views/backend',
],
// 'messages' domain (matches default domain)
'storage/views',
],
*/
/**
* Sync laravel: A flag that determines if the laravel built-in locale must
* be changed when you call LaravelGettext::setLocale.
*/
'sync-laravel' => true,
/**
* The adapter used to sync the laravel built-in locale
*/
'adapter' => XinaxLaravelGettextAdaptersLaravelAdapter::class,
/**
* Where to store the current locale/domain
*
* By default, in the session.
* Can be changed for only memory or your own storage mechanism
*
* @see XinaxLaravelGettextStoragesStorage
*/
'storage' => XinaxLaravelGettextStoragesSessionStorage::class,
/**
* Use custom locale that is not supported by the system
*/
'custom-locale' => false,
/**
* The keywords list used by poedit to search the strings to be translated
*
* The "_", "__" and "gettext" are singular translation functions
* The "_n" and "ngettext" are plural translation functions
* The "dgettext" function allows a translation domain to be explicitly specified
*
* "__" and "_n" and "_i" and "_s" are helpers functions @see XinaxLaravelGettextSupporthelpers.php
*/
'keywords-list' => ['_', '__', '_i', '_s', 'gettext', '_n:1,2', 'ngettext:1,2', 'dgettext:2'],
];

messages.po :

msgid ""
msgstr ""
"Project-Id-Version: MultilanguageLaravelApplicationn"
"POT-Creation-Date: 2022-09-16 11:59+0200n"
"PO-Revision-Date: 2022-09-16 11:59+0200n"
"Last-Translator: James Translator <james@translations.colm>n"
"Language-Team: James Translator <james@translations.colm>n"
"Language: en_USn"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"X-Generator: Poedit 1.5.4n"
"X-Poedit-KeywordsList: _;__;_i;_s;gettext;_n:1,2;ngettext:1,2;dgettext:2n"
"X-Poedit-Basepath: ../../../../../appn"
"X-Poedit-SourceCharset: UTF-8n"
"X-Poedit-SearchPath-0: Httpn"
"X-Poedit-SearchPath-1: ../resources/viewsn"
"X-Poedit-SearchPath-2: Consolen"
"X-Poedit-SearchPath-3: ../storage/framework/messages/n"

更新现有.po文件的正常过程是用xgettext提取可翻译的行,生成.pot文件,然后用msgmerge将其与现有.po合并。

为了简化流程,您可以使用POedit工具,该工具将在GUI中完成以上所有操作。

请参阅:GNU gettext手册,特别是制作PO模板文件和更新现有PO文件章节

最新更新