26b / i18n-midoru
Handle translation operation with ease (download and upload).
Installs: 2 502
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 17
pkg:composer/26b/i18n-midoru
Requires
- php: >=8.2
- guzzlehttp/guzzle: ^7.2
- symfony/console: ^7.3
Requires (Dev)
- phpunit/phpunit: ^9.0
README
This library has some helper functions/classes for dealing with exporting and importing translations based on a config file. Right now we're only supporting Localise as the source for translations.
⚠️ This library is in active development so the API may suffer changes.
How to use?
There are essentially three interfaces, they only differ on how they ingest the options to the Translations system.
-
Use a JSON file with all the configuration.
{ "project_name": { "export": { "ext": "mo", "format": "gettext" } } }use TwentySixB\Translations\Translations; use TwentySixB\Translations\Input\File; // Make POT $translations = new Translations( new File( 'path_to_file.json' ) ); $translations->make_pots(); $translations->upload();
-
Use PHP as your interface and have a PHP data structure with the configuration.
use TwentySixB\Translations\Translations; use TwentySixB\Translations\Input\Dataset; // Make POT $translations = new Translations( new Dataset( [ 'project_name' => [ 'export' => [ 'ext' => 'mo', 'format' => 'gettext', ] ] ] ) ); $translations->make_pots(); $translations->upload();
-
Use a custom command and fetch config from the options passed to it.
Create your two command files in PHP.
<?php // upload.php use TwentySixB\Translations\Translations; use TwentySixB\Translations\Input\CLI; $translations = new Translations( new CLI() ); $translations->make_pots(); $translations->upload();
<?php // download.php use TwentySixB\Translations\Translations; use TwentySixB\Translations\Input\CLI; $translations = new Translations( new CLI() ); $translations->download();
Now you can run them from the command line like this:
# Make POTs and upload files. $ php run upload.php --name="project_name" --ext="mo" --format="gettext" # Download translations form the system for two locales. $ php run download.php --name="project_name" --ext="mo" --format="jed" --locale="pt_PT" --locale="en"
Options
Regardless of the method to pass information to the Translations class, you can use any of the following options to configure what you want to happen.
Export (export)
localestring|array- Short code(s) for the language(s) to export. See Locale Export API.
extstring- The extensions accepted by the Localise API. See Locale Export API.
formatstring- The format accepted by the Localise API. See Locale Export API.
domainstring|optional- Domain for the export. Appended in the beginning of the filename, before the locale. See below.
filenamestring|optional- Filename for the export. Takes precedence over domain. See below.
js-handlestring|optional- To be append at the end of the filename with a '-' preceding it, before the extension. See below.
pathstring|optional|default:./- Path to the directory where the file will be saved.
wrap-jedbool|optional|default:true if format is JED- Specifies if the content in the exported JSON files should be wrapped by an array with key 'locale_data'.
namestring|requiredif using CLI- Name of the project.
The file path will be comprised of:
path ?(domain|filename-) locale ?(-js-handle) .ext
?(.*)indicates optional values.
Import (import)
localestring|array- Short code(s) for the language(s) to import. See Locale Import API.
extstring- The extensions accepted by the Localise API. See Locale Import API.
domainstring|optional- Domain for the import. Appended in the beginning of the filename, before the locale. See below.
filenamestring|optional- Filename for the import. Takes precedence over domain. See below.
js-handlestring|optional- To be append at the end of the filename with a '-' preceding it, before the extension. See below.
pathstring|optional|default:./- Path to the directory where the file will be saved.
namestring|requiredif using CLI- Name of the project.
The file path will be comprised of:
path ?(domain|filename-) locale ?(-js-handle) .ext
?(.*)indicates optional values.
Make pots (make_pots)
domainstring- Domain for the
wp i18n make-potscommand.
sourcestring- Source path for the
wp i18n make-potscommand. Directory where the strings to be translated will be extracted from.
destinationstring- Destination path for the
wp i18n make-potscommand. Where the pot file will be saved.
filenamestring- Destination file name for the
wp i18n make-potscommand. To be placed in thedestinationfolder..
skip-js:bool|optional|default:true- Whether the option
--skip-jswill be passed to thewp i18n make-potscommand. Makes it so strings to be translated inside JS code will not be considered.
TODO
- Maybe rest of api params.
- Join all arguments and indicate which are not usable in some situations.