Skip to content

Commit

Permalink
MERGE: Merge branch '3.3' into 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kdambekalns committed Dec 21, 2016
2 parents 345c9ea + 078cef0 commit e5d3108
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 147 deletions.
18 changes: 9 additions & 9 deletions Crowdin/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

require(__DIR__ . '/Helpers.php');

if ($useBundling === TRUE) {
$projectPath = realpath(__DIR__ . '/../../../');
echo 'Working on project bundle' . PHP_EOL;
executeDownload($projectPath);
if ($useBundling === true) {
$projectPath = realpath(__DIR__ . '/../../../');
echo 'Working on project bundle' . PHP_EOL;
executeDownload($projectPath, $branch);
} else {
foreach ($projects as $identifier => $projectData) {
$projectPath = realpath(__DIR__ . '/../../../' . $projectData['path']);
echo 'Working on ' . $projectPath . PHP_EOL;
executeDownload($projectPath);
}
foreach ($projects as $identifier => $projectData) {
$projectPath = realpath(__DIR__ . '/../../../' . $projectData['path']);
echo 'Working on ' . $projectPath . PHP_EOL;
executeDownload($projectPath, $branch);
}
}
98 changes: 60 additions & 38 deletions Crowdin/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<?php

if (!isset($argv[1])) {
echo 'The configuration file must be given as parameter.' . PHP_EOL;
exit(1);
echo 'The configuration file must be given as parameter.' . PHP_EOL;
exit(1);
}

if (!file_exists($argv[1])) {
echo 'The configuration file ' . $argv[1] . ' does not exist.' . PHP_EOL;
exit(1);
echo 'The configuration file ' . $argv[1] . ' does not exist.' . PHP_EOL;
exit(1);
}

/** @var array $projects */
$projects = json_decode(file_get_contents($argv[1]), TRUE);
$projects = json_decode(file_get_contents($argv[1]), true);

if (array_search('--bundle', $argv) !== FALSE) {
$useBundling = TRUE;
if (array_search('--bundle', $argv) !== false) {
$useBundling = true;
} else {
$useBundling = FALSE;
if (isset($projects['__bundle'])) {
unset($projects['__bundle']);
}
$useBundling = false;
if (isset($projects['__bundle'])) {
unset($projects['__bundle']);
}
}
$uploadTranslations = (array_search('--translations', $argv) !== FALSE);
$uploadTranslations = (array_search('--translations', $argv) !== false);

$branch = isset($projects['branch']) ? $projects['branch'] : null;
unset($projects['branch']);

/**
* Check if the given project exists on Crowdin.
Expand All @@ -30,46 +33,65 @@
* @param string $apiKey
* @return boolean
*/
function projectExists($identifier, $apiKey) {
$projectInfo = json_decode(file_get_contents(sprintf('http://api.crowdin.net/api/project/%s/info?json&key=%s', $identifier, $apiKey)), TRUE);
function projectExists($identifier, $apiKey)
{
$projectInfo = @json_decode(file_get_contents(sprintf('http://api.crowdin.net/api/project/%s/info?json&key=%s', $identifier, $apiKey)), true);

// check if project exists
if (isset($projectInfo['success']) && $projectInfo['success'] === FALSE) {
return FALSE;
} else {
return TRUE;
}
// check if project exists
if (!is_array($projectInfo) || (isset($projectInfo['success']) && $projectInfo['success'] === false)) {
return false;
} else {
return true;
}
}

/**
*
* @param string $projectPath
* @param boolean $uploadTranslations
* @param string $branch
* @return void
*/
function executeUpload($projectPath, $uploadTranslations) {
if (file_exists($projectPath . '/crowdin.yaml')) {
echo sprintf('cd %s; crowdin-cli upload sources', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload sources', escapeshellarg($projectPath)));
if ($uploadTranslations === TRUE) {
echo sprintf('cd %s; crowdin-cli upload translations', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload translations', escapeshellarg($projectPath)));
}
} else {
echo 'Project is not configured.' . PHP_EOL;
}
function executeUpload($projectPath, $uploadTranslations, $branch = null)
{
if (file_exists($projectPath . '/crowdin.yaml')) {
if ($branch === null) {
echo sprintf('cd %s; crowdin-cli upload sources', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload sources', escapeshellarg($projectPath)));
if ($uploadTranslations === true) {
echo sprintf('cd %s; crowdin-cli upload translations', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload translations', escapeshellarg($projectPath)));
}
} else {
echo sprintf('cd %s; crowdin-cli upload sources -b %s', escapeshellarg($projectPath), escapeshellarg($branch)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload sources -b %s', escapeshellarg($projectPath), escapeshellarg($branch)));
if ($uploadTranslations === true) {
echo sprintf('cd %s; crowdin-cli upload translations -b %s', escapeshellarg($projectPath), escapeshellarg($branch)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli upload translations -b %s', escapeshellarg($projectPath), escapeshellarg($branch)));
}
}
} else {
echo 'Project is not configured.' . PHP_EOL;
}
}

/**
*
* @param string $projectPath
* @param string $branch
* @return void
*/
function executeDownload($projectPath) {
if (file_exists($projectPath . '/crowdin.yaml')) {
echo sprintf('cd %s; crowdin-cli download', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli download', escapeshellarg($projectPath)));
} else {
echo 'Project is not configured.' . PHP_EOL;
}
function executeDownload($projectPath, $branch = null)
{
if (file_exists($projectPath . '/crowdin.yaml')) {
if ($branch === null) {
echo sprintf('cd %s; crowdin-cli download', escapeshellarg($projectPath)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli download', escapeshellarg($projectPath)));
} else {
echo sprintf('cd %s; crowdin-cli download -b %s', escapeshellarg($projectPath), escapeshellarg($branch)) . PHP_EOL;
passthru(sprintf('cd %s; crowdin-cli download -b %s', escapeshellarg($projectPath), escapeshellarg($branch)));
}
} else {
echo 'Project is not configured.' . PHP_EOL;
}
}
74 changes: 42 additions & 32 deletions Crowdin/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,95 @@ You need to have the crowdin-cli tool installed, see https://crowdin.net/page/cl

Now create a JSON file in your project root, e.g. named ``crowdin.json``::

{
"acme-foo": {
"name": "Acme Foo",
"path": "Packages/Application/Acme.Foo",
"apiKey": "<project api key>"
},
"acme-bar": {
"name": "Acme Bar",
"path": "Packages/Application/Acme.Bar",
"apiKey": "<project api key>"
}
}
{
"acme-foo": {
"name": "Acme Foo",
"path": "Packages/Application/Acme.Foo",
"apiKey": "<project api key>"
},
"acme-bar": {
"name": "Acme Bar",
"path": "Packages/Application/Acme.Bar",
"apiKey": "<project api key>"
}
}

.. note:: You should never commit this to Git, as it contains your secret keys.
You can also use the option of specifying the key(s) via an environment variable.

Now you can run::

php Build/BuildEssentials/Setup.php `pwd`/crowdin.json
php Build/BuildEssentials/Crowdin/Setup.php `pwd`/crowdin.json

This will create ``crowdin.yaml`` in every project configured in ``crowdin.json``.

.. note:: You should exclude ``crowdin.yaml`` from Git, so it cannot be committed by
accident (as it contains your secret keys)
accident (as it contains your secret keys) or use the option of specifying the
key(s) via an environment variable.

Using an environment variable for the API key
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To use an environment variable for the API key, you must specify the name of the
variable in the ``crowdin.json``file, **wrapped in percent signs**. Then, when
actually calling the scripts to setup, upload or download, define the variable
as usual.
Bundling projects
^^^^^^^^^^^^^^^^^
Instead of using a single Crowdin project per package, you ca also use bundling to have all
Instead of using a single Crowdin project per package, you can also use bundling to have all
XLIFF files (sources and translations) be handled in a single Crowdin project. This allows
for better overview, uses a single translation memory and glossary and thus makes the life
of translators easier.
To use bundling, set up ``crowdin.json`` like above but include a special ``__bundle`` entry::

{
"__bundle": {
"projectIdentifier": "<project identifier>",
"apiKey": "<project api key>"
},
"acme-foo": {
}
}
{
"__bundle": {
"projectIdentifier": "<project identifier>",
"apiKey": "<project api key>"
},
"acme-foo": {
}
}

The individual project entries can still have ``apiKey`` entries, they will be ignored.
Keeping them allows to reuse the same ``crowdin.json`` file for bundled and non-bundled
use (should that ever be needed).

Then call the setup script like this::

php Build/BuildEssentials/Setup.php `pwd`/crowdin.json --bundle
php Build/BuildEssentials/Crowdin/Setup.php `pwd`/crowdin.json --bundle

Usage
-----

Now you can run::

php Build/BuildEssentials/Upload.php `pwd`/crowdin.json
php Build/BuildEssentials/Crowdin/Upload.php `pwd`/crowdin.json

This will upload the source XLIFF files to Crowdin. Using::

php Build/BuildEssentials/Upload.php `pwd`/crowdin.json --translations
php Build/BuildEssentials/Crowdin/Upload.php `pwd`/crowdin.json --translations

sources and existing translations will be uploaded (synchronized) in one go.

Running this will download the translations from Crowdin::

php Build/BuildEssentials/Download.php `pwd`/crowdin.json
php Build/BuildEssentials/Crowdin/Download.php `pwd`/crowdin.json

This updates the XLIFF files with translations from Crowdin; review and commit as
you like.

All of the above commands can be called with ``--bundle`` to use project bundling::

php Build/BuildEssentials/Upload.php `pwd`/crowdin.json --bundle
php Build/BuildEssentials/Upload.php `pwd`/crowdin.json --bundle --translations
php Build/BuildEssentials/Download.php `pwd`/crowdin.json --bundle
php Build/BuildEssentials/Crowdin/Upload.php `pwd`/crowdin.json --bundle
php Build/BuildEssentials/Crowdin/Upload.php `pwd`/crowdin.json --bundle --translations
php Build/BuildEssentials/Crowdin/Download.php `pwd`/crowdin.json --bundle

To remove all generated ``crowdin.yaml`` files again use::

php Build/BuildEssentials/Teardown.php `pwd`/crowdin.json
php Build/BuildEssentials/Crowdin/Teardown.php `pwd`/crowdin.json

98 changes: 55 additions & 43 deletions Crowdin/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,61 @@
$configurationFileTemplate = file_get_contents(__DIR__ . '/template-crowdin.yaml');
$filesEntryTemplate = file_get_contents(__DIR__ . '/template-filesentry.yaml');

if ($useBundling === TRUE) {
if (!isset($projects['__bundle'])) {
echo 'No __bundle configuration found in configuration file.' . PHP_EOL;
exit(1);
}

$bundleProjectIdentifier = $projects['__bundle']['projectIdentifier'];
$bundleProjectApiKey = $projects['__bundle']['apiKey'];
unset($projects['__bundle']);

if (projectExists($bundleProjectIdentifier, $bundleProjectApiKey) === FALSE) {
echo 'Project ' . $bundleProjectIdentifier . ' does not exist, please create it in Crowdin.' . PHP_EOL;
exit(1);
}

$filesEntryContent = '';
foreach ($projects as $identifier => $projectData) {
$projectPath = '/' . $projectData['path'];
$filesEntryContent .= sprintf($filesEntryTemplate, $projectPath, $projectPath);
}
$configurationFileContent = sprintf($configurationFileTemplate, $bundleProjectIdentifier, $bundleProjectApiKey, $filesEntryContent);

$configurationfilePathAndFilename = realpath(__DIR__ . '/../../../') . '/crowdin.yaml';
file_put_contents($configurationfilePathAndFilename, $configurationFileContent);
echo 'Wrote ' . $configurationfilePathAndFilename . PHP_EOL;
if ($useBundling === true) {
if (!isset($projects['__bundle'])) {
echo 'No __bundle configuration found in configuration file.' . PHP_EOL;
exit(1);
}

$bundleProjectIdentifier = $projects['__bundle']['projectIdentifier'];
$apiKey = $projects['__bundle']['apiKey'];
$apiKeyEnvironmentVariable = false;
if (preg_match('/^%.+%$/', $apiKey) === 1) {
$apiKeyEnvironmentVariable = trim($apiKey, '%');
$apiKey = getenv($apiKeyEnvironmentVariable);
}
unset($projects['__bundle']);

if (projectExists($bundleProjectIdentifier, $apiKey) === false) {
echo 'Project ' . $bundleProjectIdentifier . ' does not exist, please create it in Crowdin.' . PHP_EOL;
exit(1);
}

$filesEntryContent = '';
foreach ($projects as $identifier => $projectData) {
$projectPath = '/' . $projectData['path'];
$filesEntryContent .= sprintf($filesEntryTemplate, $projectPath, $projectPath);
}
$configurationFileContent = sprintf($configurationFileTemplate, $bundleProjectIdentifier, $apiKeyEnvironmentVariable ? '_env' : '', $apiKeyEnvironmentVariable ?: $apiKey, $filesEntryContent);

$configurationfilePathAndFilename = realpath(__DIR__ . '/../../../') . '/crowdin.yaml';
file_put_contents($configurationfilePathAndFilename, $configurationFileContent);
echo 'Wrote ' . $configurationfilePathAndFilename . PHP_EOL;
} else {
foreach ($projects as $identifier => $projectData) {
if (!isset($projectData['apiKey'])) {
echo 'Project ' . $projectData['name'] . ' has no apiKey set.' . PHP_EOL;
continue;
}

if (projectExists($identifier, $projectData['apiKey']) === FALSE) {
echo 'Project ' . $projectData['name'] . ' does not exist, please create it in Crowdin.' . PHP_EOL;
continue;
}

$filesEntryContent = sprintf($filesEntryTemplate, '', '');
$configurationFileContent = sprintf($configurationFileTemplate, $identifier, $projectData['apiKey'], $filesEntryContent);

$configurationfilePathAndFilename = realpath(__DIR__ . '/../../../' . $projectData['path']) . '/crowdin.yaml';
file_put_contents($configurationfilePathAndFilename, $configurationFileContent);
echo 'Wrote ' . $configurationfilePathAndFilename . PHP_EOL;
}
foreach ($projects as $identifier => $projectData) {
if (!isset($projectData['apiKey'])) {
echo 'Project ' . $projectData['name'] . ' has no apiKey set.' . PHP_EOL;
continue;
}

$apiKey = $projectData['apiKey'];
$apiKeyEnvironmentVariable = false;
if (preg_match('/^%.+%$/', $apiKey) === 1) {
$apiKeyEnvironmentVariable = trim($apiKey, '%');
$apiKey = getenv($apiKeyEnvironmentVariable);
}

if (projectExists($identifier, $apiKey) === false) {
echo 'Project ' . $projectData['name'] . ' does not exist, please create it in Crowdin.' . PHP_EOL;
continue;
}

$filesEntryContent = sprintf($filesEntryTemplate, '', '');
$configurationFileContent = sprintf($configurationFileTemplate, $identifier, $apiKeyEnvironmentVariable ? '_env' : '', $apiKeyEnvironmentVariable ?: $apiKey, $filesEntryContent);

$configurationfilePathAndFilename = realpath(__DIR__ . '/../../../' . $projectData['path']) . '/crowdin.yaml';
file_put_contents($configurationfilePathAndFilename, $configurationFileContent);
echo 'Wrote ' . $configurationfilePathAndFilename . PHP_EOL;
}
}

Loading

0 comments on commit e5d3108

Please sign in to comment.