[ticket/10824] Make languages use composer.json

PHPBB3-10824
This commit is contained in:
Joas Schilling 2015-09-05 19:20:15 +02:00 committed by Marc Alexander
parent a200446646
commit 8244aff9cb
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
4 changed files with 88 additions and 25 deletions

View file

@ -265,20 +265,21 @@ class acp_language
$lang_iso = $request->variable('iso', ''); $lang_iso = $request->variable('iso', '');
$lang_iso = basename($lang_iso); $lang_iso = basename($lang_iso);
if (!$lang_iso || !file_exists("{$phpbb_root_path}language/$lang_iso/iso.txt")) if (!$lang_iso || !file_exists("{$phpbb_root_path}language/$lang_iso/composer.json"))
{ {
trigger_error($user->lang['LANGUAGE_PACK_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING); trigger_error($user->lang['LANGUAGE_PACK_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
} }
$file = file("{$phpbb_root_path}language/$lang_iso/iso.txt"); /** @var \phpbb\language\language_file_helper $language_helper */
$language_helper = $phpbb_container->get('language.helper.language_file');
$lang_pack = array( try
'iso' => $lang_iso, {
'name' => trim(htmlspecialchars($file[0], ENT_COMPAT)), $lang_pack = $language_helper->get_language_data_from_composer_file("{$phpbb_root_path}language/$lang_iso/composer.json");
'local_name'=> trim(htmlspecialchars($file[1], ENT_COMPAT, 'UTF-8')), }
'author' => trim(htmlspecialchars($file[2], ENT_COMPAT, 'UTF-8')) catch (\DomainException $e)
); {
unset($file); trigger_error($user->lang['LANGUAGE_PACK_NOT_EXIST'] . adm_back_link($this->u_action), E_USER_WARNING);
}
$sql = 'SELECT lang_iso $sql = 'SELECT lang_iso
FROM ' . LANG_TABLE . " FROM ' . LANG_TABLE . "

27
phpBB/language/en/composer.json Executable file
View file

@ -0,0 +1,27 @@
{
"name": "en",
"description": "phpBB Forum Software default style",
"type": "phpbb-language",
"version": "3.2.0-a1-dev",
"homepage": "https://www.phpbb.com",
"license": "GPL-2.0",
"authors": [
{
"name": "phpBB Limited",
"email": "operations@phpbb.com",
"homepage": "https://www.phpbb.com/go/authors"
}
],
"support": {
"issues": "https://tracker.phpbb.com",
"forum": "https://www.phpbb.com/community/",
"wiki": "https://wiki.phpbb.com",
"irc": "irc://irc.freenode.org/phpbb"
},
"extra": {
"language-iso": "en",
"english-name": "British English",
"local-name": "British English",
"phpbb-version": "3.2.0-a1-dev"
}
}

View file

@ -1,3 +0,0 @@
British English
British English
phpBB Limited

View file

@ -45,7 +45,7 @@ class language_file_helper
// Find available language packages // Find available language packages
$finder = new Finder(); $finder = new Finder();
$finder->files() $finder->files()
->name('iso.txt') ->name('composer.json')
->depth('== 1') ->depth('== 1')
->followLinks() ->followLinks()
->in($this->phpbb_root_path . 'language'); ->in($this->phpbb_root_path . 'language');
@ -53,20 +53,58 @@ class language_file_helper
$available_languages = array(); $available_languages = array();
foreach ($finder as $file) foreach ($finder as $file)
{ {
$path = $file->getRelativePath(); $json = $file->getContents();
$info = explode("\n", $file->getContents()); $data = json_decode($json, true);
$available_languages[] = array( $available_languages[] = $this->get_language_data_from_json($data);
// Get the name of the directory containing iso.txt
'iso' => $path,
// Recover data from file
'name' => trim($info[0]),
'local_name' => trim($info[1]),
'author' => trim($info[2])
);
} }
return $available_languages; return $available_languages;
} }
/**
* Collect some data from the composer.json file
*
* @param string $path
* @return array
*/
public function get_language_data_from_composer_file($path)
{
$json_data = file_get_contents($path);
return $this->get_language_data_from_json(json_decode($json_data, true));
}
/**
* Collect some data from the composer.json data
*
* @param array $data
* @return array
*/
protected function get_language_data_from_json(array $data)
{
if (!isset($data['extra']['language-iso']) || !isset($data['extra']['english-name']) || !isset($data['extra']['local-name']))
{
throw new \DomainException('INVALID_LANGUAGE_PACK');
}
$authors = array();
if (isset($data['authors']))
{
foreach ($data['authors'] as $author)
{
if (isset($author['name']) && $author['name'] !== '')
{
$authors[] = $author['name'];
}
}
}
return array(
'iso' => $data['extra']['language-iso'],
'name' => $data['extra']['english-name'],
'local_name' => $data['extra']['local-name'],
'author' => implode(', ', $authors),
);
}
} }