Last active
October 28, 2019 19:37
-
-
Save Bakual/7a750e4f3f89c72bfd3c to your computer and use it in GitHub Desktop.
Crowdin Update Source or Translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Crowdin Upload Translation | |
* Usage: | |
* - Upload (latvian) translation: php crowdin.php --key=abcdefg --lang_joomla=lv-LV --lang_crowdin=lv | |
* The argument `--approved=1` will upload the source or translation as approved | |
* The argument `--branch=4.0-dev will upload the translations for Joomla 4.0. By default it will upload to staging. | |
* | |
* @package Joomla.Cli | |
* | |
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
// We are a valid entry point. | |
const _JEXEC = 1; | |
// Load system defines | |
if (file_exists(dirname(__DIR__) . '/defines.php')) | |
{ | |
require_once dirname(__DIR__) . '/defines.php'; | |
} | |
if (!defined('_JDEFINES')) | |
{ | |
define('JPATH_BASE', dirname(__DIR__)); | |
require_once JPATH_BASE . '/includes/defines.php'; | |
} | |
// Get the framework. | |
require_once JPATH_LIBRARIES . '/import.legacy.php'; | |
// Bootstrap the CMS libraries. | |
require_once JPATH_LIBRARIES . '/cms.php'; | |
// Configure error reporting to maximum for CLI output. | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
// Load Library language | |
$lang = JFactory::getLanguage(); | |
// Try the files_joomla file in the current language (without allowing the loading of the file in the default language) | |
$lang->load('files_joomla.sys', JPATH_SITE, null, false, false) | |
// Fallback to the files_joomla file in the default language | |
|| $lang->load('files_joomla.sys', JPATH_SITE, null, true); | |
/** | |
* A command line cron job to update source language files in Crowdin. | |
* | |
* @since 3.0 | |
*/ | |
class CrowdinCli extends JApplicationCli | |
{ | |
/** | |
* Entry point for CLI script | |
* | |
* @return void | |
* | |
* @since 3.0 | |
*/ | |
public function doExecute() | |
{ | |
JLoader::register('JFolder', JPATH_LIBRARIES . '/joomla/filesystem/folder.php'); | |
JLoader::register('JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php'); | |
$project = 'joomla-cms'; | |
$apiKey = $this->input->get('key'); | |
if (!$apiKey) | |
{ | |
$this->out('No API Key provided'); | |
return; | |
} | |
$languageCodeCrowdin = $this->input->get('lang_crowdin', 'en-GB'); | |
$languageCodeJoomla = $this->input->get('lang_joomla', 'en-GB'); | |
$approved = $this->input->get('approved'); | |
$branch = $this->input->get('branch', 'staging'); | |
if (($languageCodeCrowdin == 'en-GB' && $languageCodeJoomla != 'en-GB') | |
|| ($languageCodeCrowdin != 'en-GB' && $languageCodeJoomla == 'en-GB')) | |
{ | |
$this->out('Wrong language codes provided'); | |
return; | |
} | |
$languageDirs = array( | |
'/administrator/language/en-GB' => '/administrator/language/' . $languageCodeJoomla, | |
'/installation/language/en-GB' => '/installation/language/' . $languageCodeJoomla, | |
'/language/en-GB' => '/language/' . $languageCodeJoomla, | |
); | |
foreach ($languageDirs as $crowdinDir => $languageDir) | |
{ | |
$files = JFolder::files(JPATH_ROOT . $languageDir); | |
$this->out('>> Folder: ' . $languageDir . ' <<'); | |
foreach ($files as $file) | |
{ | |
// Skip PHP files (localise.php). Crowdin can't handle PHP files properly | |
// TODO: would have to upload it as txt file | |
if (JFile::getExt($file) == 'php') | |
{ | |
continue; | |
} | |
$localFile = JPATH_ROOT . $languageDir . '/' . $file; | |
$title = str_replace($languageCodeJoomla, 'xx-XX', $file); | |
$crowdinFile = str_replace($languageCodeJoomla, 'en-GB', $file); | |
$crowdinFile = $crowdinDir . '/' . $crowdinFile; | |
$post = array(); | |
$post['files[' . $crowdinFile . ']'] = curl_file_create($localFile); | |
$post['titles[' . $crowdinFile . ']'] = $title; | |
$post['language'] = $languageCodeCrowdin; | |
$post['import_eq_suggestions'] = 0; | |
$post['import_duplicates'] = 0; | |
$post['branch'] = $branch; | |
if ($approved) | |
{ | |
$post['auto_approve_imported'] = 1; | |
} | |
$url = 'https://api.crowdin.com/api/project/' . $project . '/upload-translation?key=' . $apiKey; | |
$jhttp = JHttpFactory::getHttp(null, 'curl'); | |
$postString = $post; | |
$headers = array('Content-Type' => 'multipart/form-data'); | |
$response = $jhttp->post($url, $postString, $headers); | |
$this->out($file . ' => ', false); | |
if ($body = $response->body) | |
{ | |
$xml = simplexml_load_string($body); | |
if (isset($xml->stats)) | |
{ | |
// $xml->stats->file['name'] would show the name used in Crowdin | |
$this->out($xml->stats->file['status']); | |
} | |
elseif (isset($xml->code)) | |
{ | |
$this->out($xml->message . ' (ErrorCode: ' . $xml->code . ')'); | |
} | |
} | |
} | |
} | |
} | |
} | |
// Instantiate the application object, passing the class name to JCli::getInstance | |
// and use chaining to execute the application. | |
JApplicationCli::getInstance('CrowdinCli')->execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment