Skip to content

Commit c9bb9a4

Browse files
authored
Add Project::getLocaleTool method (#828)
* Save info about tools and supported locales in JSON file * Add Project::getLocaleTool
1 parent 301cc02 commit c9bb9a4

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

app/classes/Transvision/Project.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,28 @@ function ($row) {
226226

227227
return array_unique($reference_components);
228228
}
229+
230+
/**
231+
* Return the name of the tool the requested locale is working in.
232+
*
233+
* @return string Name of the tool, empty if not available
234+
*/
235+
public static function getLocaleTool($locale)
236+
{
237+
// Read list of tools and their supported locales from local sources
238+
$file_name = APP_SOURCES . 'tools.json';
239+
if (file_exists($file_name)) {
240+
$json_tools = (new Json($file_name))->fetchContent();
241+
} else {
242+
die("ERROR: run app/scripts/setup.sh or app/scripts/dev-setup.sh to generate sources.");
243+
}
244+
245+
foreach ($json_tools as $tool => $supported_locales) {
246+
if (in_array($locale, $supported_locales)) {
247+
return $tool;
248+
}
249+
}
250+
251+
return '';
252+
}
229253
}

app/scripts/generate_sources

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ ksort($json_repositories);
5050
echo "* Saving JSON record of all supported repositories\n";
5151
$file_name = "{$config_folder}/sources/supported_repositories.json";
5252
file_put_contents($file_name, json_encode($json_repositories));
53+
54+
// Create list of locales working in tools like Pootle and Pontoon
55+
$uri = $server_config['l10nwebservice'] . '/?tool=all';
56+
if (! $file_content = file_get_contents($uri)) {
57+
error_log('JSON source is not reachable.');
58+
exit(1);
59+
}
60+
echo "* Saving JSON record of tools and their supported locales\n";
61+
$file_name = "{$config_folder}/sources/tools.json";
62+
file_put_contents($file_name, $file_content);

tests/testfiles/config/tools.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"locamotion": ["ach", "ar", "tr"],
3+
"pontoon": ["sr", "te"]
4+
}

tests/units/Transvision/Project.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,25 @@ public function testGetLocaleInContext($a, $b, $c)
155155
->string($obj->getLocaleInContext($a, $b))
156156
->isEqualTo($c);
157157
}
158+
159+
public function getLocaleToolDP()
160+
{
161+
return [
162+
['ar', 'locamotion'],
163+
['fr', ''],
164+
['sr', 'pontoon'],
165+
['te', 'pontoon'],
166+
];
167+
}
168+
169+
/**
170+
* @dataProvider getLocaleToolDP
171+
*/
172+
public function testGetLocaleTool($a, $b)
173+
{
174+
$obj = new _Project();
175+
$this
176+
->string($obj->getLocaleTool($a))
177+
->isEqualTo($b);
178+
}
158179
}

0 commit comments

Comments
 (0)