Skip to content

Commit 9868ea2

Browse files
committed
Add support for Firefox for iOS
1 parent a05a5eb commit 9868ea2

File tree

14 files changed

+351
-10
lines changed

14 files changed

+351
-10
lines changed

app/classes/Transvision/AnalyseStrings.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public static function differences($tmx_source, $tmx_target, $repo, $ignored_str
4444

4545
if (Strings::startsWith($repo, 'gaia')) {
4646
$patterns = [
47-
'l10njs' => '/\{\{\s*([a-z0-9_]+)\s*\}\}/iu', // {{foobar2}}
47+
'l10njs' => '/\{\{\s*([a-z0-9_]+)\s*\}\}/iu', // {{foobar2}}
48+
];
49+
} elseif ($repo == 'firefox_ios') {
50+
$patterns = [
51+
'ios' => '/(%(?:[0-9]+\$){0,1}@)/i', // %@, but also %1$@, %2$@, etc.
4852
];
4953
} else {
5054
$patterns = [

app/classes/Transvision/Project.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public static function getGaiaRepositories()
138138
public static function getDesktopRepositories()
139139
{
140140
return array_diff(
141-
array_diff(self::getRepositories(), ['mozilla_org']),
141+
self::getRepositories(),
142+
['mozilla_org', 'firefox_ios'],
142143
self::getGaiaRepositories()
143144
);
144145
}
@@ -224,14 +225,17 @@ public static function getLocaleInContext($locale, $context)
224225
'sr' => 'sr-Cyrl',
225226
];
226227

227-
// Other contexts. At the moment, identical to Bugzilla list
228-
$locale_mappings['other'] = $locale_mappings['bugzilla'];
229-
230228
// Use Gaia mapping for all Gaia repositories
231229
if (Strings::startsWith($context, 'gaia')) {
232230
$context = 'gaia';
233231
}
234232

233+
// Firefox for iOS: no mapping
234+
$locale_mappings['firefox_ios'] = [];
235+
236+
// For other contexts use the same as Bugzilla
237+
$locale_mappings['other'] = $locale_mappings['bugzilla'];
238+
235239
// Fallback to 'other' if context doesn't exist in $locale_mappings
236240
$context = array_key_exists($context, $locale_mappings)
237241
? $context

app/classes/Transvision/ShowResults.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
285285
if ($search_options['repo'] == 'mozilla_org') {
286286
$locale1_path = VersionControl::gitPath($locale1, $search_options['repo'], $key);
287287
$locale2_path = VersionControl::gitPath($locale2, $search_options['repo'], $key);
288+
} elseif ($search_options['repo'] == 'firefox_ios') {
289+
$locale1_path = VersionControl::svnPath($locale1, $search_options['repo'], $key);
290+
$locale2_path = VersionControl::svnPath($locale2, $search_options['repo'], $key);
288291
} else {
289292
$locale1_path = VersionControl::hgPath($locale1, $search_options['repo'], $key);
290293
$locale2_path = VersionControl::hgPath($locale2, $search_options['repo'], $key);
@@ -332,6 +335,8 @@ public static function resultsTable($search_results, $recherche, $locale1, $loca
332335
if (isset($search_options["extra_locale"])) {
333336
if ($search_options['repo'] == 'mozilla_org') {
334337
$locale3_path = VersionControl::gitPath($locale3, $search_options['repo'], $key);
338+
} elseif ($search_options['repo'] == 'firefox_ios') {
339+
$locale3_path = VersionControl::svnPath($locale3, $search_options['repo'], $key);
335340
} else {
336341
$locale3_path = VersionControl::hgPath($locale3, $search_options['repo'], $key);
337342
}

app/classes/Transvision/VersionControl.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class VersionControl
1919
public static function getVCS($repo)
2020
{
2121
$vcs = [
22-
'hg' => [],
2322
'git' => ['mozilla_org'],
23+
'hg' => [],
24+
'svn' => ['firefox_ios'],
2425
];
2526
$vcs['hg'] = array_merge(
2627
Project::getDesktopRepositories(),
@@ -197,9 +198,11 @@ public static function svnPath($locale, $repo, $path)
197198
if ($repo == 'mozilla_org') {
198199
$file_path = 'projects/mozilla.com/trunk/locales/'
199200
. $locale . '/' . self::extractFilePath($path);
201+
} elseif ($repo == 'firefox_ios') {
202+
$file_path = "projects/l10n-misc/trunk/firefox-ios/{$locale}/firefox-ios.xliff";
200203
}
201204

202-
return 'http://viewvc.svn.mozilla.org/vc/'
205+
return 'https://viewvc.svn.mozilla.org/vc/'
203206
. $file_path . '?view=markup';
204207
}
205208

app/classes/Transvision/Xliff.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace Transvision;
3+
4+
/**
5+
* Xliff class
6+
*
7+
* This class is used to manipulate translation files in XLIFF format
8+
* used in Firefox for iOS.
9+
*
10+
* @package Transvision
11+
*/
12+
class Xliff
13+
{
14+
/**
15+
*
16+
* Loads strings from a .xliff file
17+
*
18+
* @param string $xliff_path Path to the .xliff to load
19+
* @param string $project_name The project this string belongs to
20+
*
21+
* @return array Array of strings as [string_id => translation]
22+
*/
23+
public static function getStrings($xliff_path, $project_name)
24+
{
25+
$strings = [];
26+
if ($xml = simplexml_load_file($xliff_path)) {
27+
$namespaces = $xml->getDocNamespaces();
28+
$xml->registerXPathNamespace('x', $namespaces['']);
29+
/* Get all trans-units, which include both reference (source) and
30+
/* translation (target)
31+
*/
32+
$trans_units = $xml->xpath('//x:trans-unit');
33+
foreach ($trans_units as $trans_unit) {
34+
/* File's name is 2 levels above in the hierarchy, stored as
35+
* 'original' attribute of the <file> element.
36+
*/
37+
$file_node = $trans_unit->xpath('../..');
38+
$file_name = $file_node[0]['original'];
39+
40+
$string_id = self::generateStringID($project_name, $file_name, $trans_unit['id']);
41+
$translation = str_replace("'", "\\'", $trans_unit->target);
42+
43+
$strings[$string_id] = $translation;
44+
}
45+
}
46+
47+
return $strings;
48+
}
49+
50+
/**
51+
* Generate a unique id for a string to store in Transvision.
52+
* String ID can be identical to the source string in iOS, so it's more
53+
* reliable to generate a unique ID from it.
54+
*
55+
* @param string $project_name The project this string belongs to
56+
* @param string $file_name 'original' attribute of the <file> element
57+
* @param string $string_id 'id' attribute of the <trans-unit> element
58+
*
59+
* @return string unique ID such as firefox_ios/Client/Intro.strings:1cd1dc4e
60+
*/
61+
public static function generateStringID($project_name, $file_name, $string_id)
62+
{
63+
return "{$project_name}/{$file_name}:" . hash('crc32', $string_id);
64+
}
65+
}

app/scripts/bash_variables.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ done
4646
mozilla_org=$local_git/mozilla_org/
4747
folders+=( $mozilla_org )
4848

49+
# Firefox for iOS (XLIFF)
50+
firefox_ios=$local_svn/firefox_ios/
51+
folders+=( $firefox_ios )
52+
4953
# l20n test repo
5054
l20n_test=$local_git/L20N_TEST
5155
l20n_test_locales=${path_sources}/l20n_test.txt

app/scripts/glossaire.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,21 @@ function updateFromGitHub() {
282282
fi
283283
}
284284

285+
function updateFirefoxiOS() {
286+
if $checkrepo
287+
then
288+
cd $firefox_ios
289+
echogreen "Update subversion repository"
290+
svn up
291+
fi
292+
if $createTMX
293+
then
294+
echogreen "Extract strings for Firefox for iOS"
295+
cd $install
296+
nice -20 app/scripts/tmx_xliff firefox_ios
297+
fi
298+
}
299+
285300
# Update repos without branches first (their TMX is created in updateStandardRepo)
286301
updateNoBranchRepo "chatzilla"
287302
updateNoBranchRepo "venkman"
@@ -296,8 +311,8 @@ do
296311
updateGaiaRepo ${gaia_version}
297312
done
298313

299-
# mozilla.org has its own extraction script
300314
updateFromGitHub
315+
updateFirefoxiOS
301316

302317
# Generate productization data
303318
cd $install

app/scripts/setup.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ then
295295
git clone https://github.com/mozilla-l10n/www.mozilla.org .
296296
fi
297297

298+
echogreen "Firefox for iOS repo being checked out from subversion"
299+
cd $firefox_ios
300+
if [ ! -d $firefox_ios/.svn ]
301+
then
302+
echogreen "Checking out Firefox for iOS repo"
303+
svn co https://svn.mozilla.org/projects/l10n-misc/trunk/firefox-ios/ .
304+
fi
305+
298306
# We now deal with L20n test repo as a specific case
299307
echogreen "L20n test repo initialization"
300308
cd $l20n_test

app/scripts/tmx_xliff

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env php
2+
<?php
3+
namespace Transvision;
4+
5+
// Script should not be called from the Web
6+
if (php_sapi_name() != 'cli') {
7+
die("This command can only be used in CLI mode.\n");
8+
}
9+
10+
if (count($argv) < 2) {
11+
die("This command needs more parameters, please check tmx_xliff --help.\n");
12+
}
13+
if (isset($argv[1])) {
14+
if (in_array($argv[1], ['-h', '--help'])) {
15+
$command_help = "tmx_xliff - Create TMX from XLIFF file\n" .
16+
"Usage: tmx_xliff [project_name]\n\n" .
17+
"Example: tmx_xliff firefox_ios\n";
18+
die($command_help);
19+
}
20+
}
21+
22+
include __DIR__ . '/../inc/init.php';
23+
24+
$project_name = $argv[1];
25+
26+
$supported_projects = [
27+
'firefox_ios' => [
28+
'file_name' => 'firefox-ios.xliff',
29+
'base_path' => SVN . 'firefox_ios',
30+
],
31+
];
32+
33+
if (! isset($supported_projects[$project_name])) {
34+
die("Unknown project: {$project_name}.\n");
35+
}
36+
error_log('Extraction of strings from XLIFF file');
37+
38+
$base_path = $supported_projects[$project_name]['base_path'];
39+
foreach (Files::getFilenamesInFolder($base_path, ['templates', 'README']) as $locale) {
40+
$out_translation = '';
41+
$total_strings = 0;
42+
43+
$xliff_path = "{$base_path}/{$locale}/{$supported_projects[$project_name]['file_name']}";
44+
if (file_exists($xliff_path)) {
45+
$strings = Xliff::getStrings($xliff_path, $project_name);
46+
$total_strings = count($strings);
47+
foreach ($strings as $string_id => $translation) {
48+
$out_translation .= "'{$string_id}' => '{$translation}', \n";
49+
}
50+
} else {
51+
error_log("{$locale}: file is missing");
52+
}
53+
$out_translation = "<?php\n\$tmx = [\n" . $out_translation . "];\n";
54+
55+
Files::fileForceContents(TMX . "{$locale}/cache_{$locale}_{$project_name}.php", $out_translation);
56+
error_log("{$locale}: {$total_strings} strings");
57+
}

app/views/results_entities.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
if ($check['repo'] == 'mozilla_org') {
1919
$path_locale1 = VersionControl::gitPath($source_locale, $check['repo'], $entity);
2020
$path_locale2 = VersionControl::gitPath($locale, $check['repo'], $entity);
21+
} elseif ($check['repo'] == 'firefox_ios') {
22+
$path_locale1 = VersionControl::svnPath($source_locale, $check['repo'], $entity);
23+
$path_locale2 = VersionControl::svnPath($locale, $check['repo'], $entity);
2124
} else {
2225
$path_locale1 = VersionControl::hgPath($source_locale, $check['repo'], $entity);
2326
$path_locale2 = VersionControl::hgPath($locale, $check['repo'], $entity);
@@ -42,6 +45,8 @@
4245

4346
if ($check['repo'] == 'mozilla_org') {
4447
$path_locale3 = VersionControl::gitPath($locale2, $check['repo'], $entity);
48+
} elseif ($check['repo'] == 'firefox_ios') {
49+
$path_locale3 = VersionControl::svnPath($locale2, $check['repo'], $entity);
4550
} else {
4651
$path_locale3 = VersionControl::hgPath($locale2, $check['repo'], $entity);
4752
}

0 commit comments

Comments
 (0)