1+ #!/usr/bin/env php
12<?php
23
4+ /*
5+ * Symfony DataTables Bundle
6+ * (c) Omines Internetbureau B.V. - https://omines.nl/
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ declare (strict_types=1 );
13+
314use Symfony \Component \Intl \Locales ;
415use Symfony \Component \Yaml \Yaml ;
516
6- require_once __DIR__ .'/autoload.php ' ;
17+ define ('RESOURCE_PATH ' , dirname (__DIR__ ) . '/src/Resources ' );
18+
19+ $ autoload = __DIR__ . '/../vendor/autoload.php ' ;
20+ if (!file_exists ($ autoload )) {
21+ echo 'You should run "composer install" in the library root before running this script. ' ;
22+ }
23+ require_once $ autoload ;
724
825$ argc = $ _SERVER ['argc ' ];
926$ argv = $ _SERVER ['argv ' ];
1027
11- if ($ argc !== 2 || in_array ($ argv [1 ], ['-h ' , '--help ' ])) {
28+ if (2 !== $ argc || in_array ($ argv [1 ], ['-h ' , '--help ' ], true )) {
1229 echo '
13- Usage: php update-translations.php <version>
30+ Usage: bin/ update-translations <version>
1431
1532Updates the translations messages data from https://github.com/DataTables/Plugins
1633
2845$ tempZipFile = downloadToTempFile ($ zipUrl );
2946
3047traverseLangFilesInZip ($ tempZipFile , function ($ filename , $ content ) {
31- $ langData = json_decode (substr ($ content , strpos ($ content , '{ ' )), true );
48+ $ langData = json_decode (mb_substr ($ content , mb_strpos ($ content , '{ ' )), true );
3249 if (!is_array ($ langData )) {
3350 printf ("JSON error '%s' in %s \n" , json_last_error_msg (), $ filename );
51+
3452 return ;
3553 }
3654
37- // change keys like sEmptyTable to emptyTable
38- $ langData = normalizeArrayKeys ($ langData );
55+ // Change keys like sEmptyTable to emptyTable
56+ try {
57+ $ langData = normalizeArrayKeys ($ langData );
3958
40- $ localeName = getLocaleNameFromLangFilename ($ filename );
41- $ locale = getLocaleByName ($ localeName );
42- if ($ locale === false ) {
43- printf ("Unsupported locale name '%s' \n" , $ localeName );
44- return ;
45- }
59+ $ localeName = getLocaleNameFromLangFilename ($ filename );
60+ $ locale = getLocaleByName ($ localeName );
61+ if (false === $ locale ) {
62+ printf ("Unsupported locale name '%s' \n" , $ localeName );
63+
64+ return ;
65+ }
4666
47- updateMessagesFileWithLangData ($ locale , $ langData );
67+ updateMessagesFileWithLangData ($ locale , $ langData );
68+ } catch (\Error $ e ) {
69+ printf ("Could not parse source file '%s' \n" , $ filename );
70+ }
4871});
4972
5073unlink ($ tempZipFile );
@@ -74,13 +97,13 @@ function downloadToTempFile(string $url): string
7497
7598function traverseLangFilesInZip (string $ zipFile , callable $ function )
7699{
77- $ zip = new ZipArchive ;
78- if ($ zip ->open ($ zipFile ) !== true ) {
100+ $ zip = new ZipArchive () ;
101+ if (true !== $ zip ->open ($ zipFile )) {
79102 throw new \RuntimeException (sprintf ('Failed opening "%s" ' , $ zipFile ));
80103 }
81104
82105 // loop to find lang files
83- for ($ i = 0 ; $ i < $ zip ->numFiles ; $ i ++ ) {
106+ for ($ i = 0 ; $ i < $ zip ->numFiles ; ++ $ i ) {
84107 $ filename = $ zip ->getNameIndex ($ i );
85108 if (preg_match ('#/i18n/.*\.lang# ' , $ filename )) {
86109 $ content = $ zip ->getFromIndex ($ i );
@@ -95,8 +118,8 @@ function normalizeArrayKeys(array $input): array
95118{
96119 $ output = [];
97120 foreach ($ input as $ key => $ value ) {
98- if (strpos ($ key , 's ' ) === 0 || strpos ($ key , 'o ' ) === 0 ) {
99- $ key = lcfirst (substr ($ key , 1 ));
121+ if (0 === mb_strpos ($ key , 's ' ) || 0 === mb_strpos ($ key , 'o ' )) {
122+ $ key = lcfirst (mb_substr ($ key , 1 ));
100123 }
101124
102125 if (is_array ($ value )) {
@@ -111,7 +134,7 @@ function normalizeArrayKeys(array $input): array
111134
112135function updateMessagesFileWithLangData (string $ locale , array $ langData ): bool
113136{
114- $ messageFile = sprintf ('%s/translations/messages.%s.yml ' , dirname ( __DIR__ ) , $ locale );
137+ $ messageFile = sprintf ('%s/translations/messages.%s.yml ' , RESOURCE_PATH , $ locale );
115138
116139 $ messages = [
117140 'datatable ' => [
@@ -129,7 +152,8 @@ function updateMessagesFileWithLangData(string $locale, array $langData): bool
129152 $ messages = array_replace_recursive ($ messages , ['datatable ' => ['datatable ' => $ langData ]]);
130153
131154 printf ("Updating messages for %s \n" , $ locale );
132- return file_put_contents ($ messageFile , Yaml::dump ($ messages , 4 , 4 , Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE ));
155+
156+ return (bool ) file_put_contents ($ messageFile , Yaml::dump ($ messages , 4 , 4 , Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE ));
133157}
134158
135159function getLocaleNameFromLangFilename ($ filename )
0 commit comments