Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F50317274
T231755 script (php2json – add CldrNames and LocalNames to i18n JSON files (v2)
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
Lucas_Werkmeister_WMDE
May 5 2024, 11:19 AM
2024-05-05 11:19:11 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
T231755 script (php2json – add CldrNames and LocalNames to i18n JSON files (v2)
View Options
<?php
declare
(
strict_types
=
1
);
function
addToJson
(
string
$code
,
array
$messages
):
void
{
$jsonFileName
=
__DIR__
.
'/i18n/'
.
$code
.
'.json'
;
$jsonText
=
@
file_get_contents
(
$jsonFileName
);
if
(
$jsonText
===
false
)
{
// empty stub file
$jsonText
=
'{ "@metadata": { "authors": [] } }'
;
}
$json
=
json_decode
(
$jsonText
,
true
);
if
(
$json
===
null
)
{
throw
new
RuntimeException
(
'Unable to decode file: '
.
$jsonFileName
);
}
$json
+=
$messages
;
ksort
(
$json
);
$jsonText
=
json_encode
(
$json
,
JSON_UNESCAPED_SLASHES
|
JSON_UNESCAPED_UNICODE
|
JSON_PRETTY_PRINT
);
if
(
$jsonText
===
false
)
{
throw
new
RuntimeException
(
'Unable to encode data for file: '
.
$jsonFileName
);
}
// fix indentation (regex taken from FormatJson::encode())
$jsonText
=
preg_replace
(
'/ {4}|.*+
\n\K
{4}/A'
,
"
\t
"
,
$jsonText
);
$success
=
file_put_contents
(
$jsonFileName
,
$jsonText
);
if
(
$success
===
false
)
{
throw
new
RuntimeException
(
'Unable to write file: '
.
$jsonFileName
);
}
}
/** Inverse {@link \MediaWiki\Languages\LanguageNameUtils::getFileName()}. */
function
fileNamePartToCode
(
string
$fileNamePart
):
string
{
return
str_replace
(
'_'
,
'-'
,
lcfirst
(
$fileNamePart
)
);
}
function
loadCldrNames
():
array
{
$cldrLanguageNames
=
[];
foreach
(
scandir
(
__DIR__
.
'/CldrNames'
)
as
$entry
)
{
$path
=
__DIR__
.
'/CldrNames/'
.
$entry
;
if
(
!
is_file
(
$path
)
)
{
continue
;
}
$languageNames
=
false
;
require
$path
;
if
(
is_array
(
$languageNames
)
)
{
$code
=
fileNamePartToCode
(
substr
(
$entry
,
9
,
// strlen( 'CldrNames' )
-
4
// strlen( '.php' )
)
);
$cldrLanguageNames
[
$code
]
=
$languageNames
;
}
}
return
$cldrLanguageNames
;
}
function
loadLocalNames
():
array
{
$localLanguageNames
=
[];
foreach
(
scandir
(
__DIR__
.
'/LocalNames'
)
as
$entry
)
{
$path
=
__DIR__
.
'/LocalNames/'
.
$entry
;
if
(
!
is_file
(
$path
)
)
{
continue
;
}
$languageNames
=
false
;
require
$path
;
if
(
is_array
(
$languageNames
)
)
{
$code
=
fileNamePartToCode
(
substr
(
$entry
,
10
,
// strlen( 'LocalNames' )
-
4
// strlen( '.php' )
)
);
$localLanguageNames
[
$code
]
=
$languageNames
;
}
}
return
$localLanguageNames
;
}
$allCldrNames
=
loadCldrNames
();
$allLocalNames
=
loadLocalNames
();
$allMessages
=
[];
foreach
(
$allCldrNames
as
$code
=>
$cldrNames
)
{
foreach
(
$cldrNames
as
$code2
=>
$cldrName
)
{
$allMessages
[
$code
][
"cldr-language-name-$code2"
]
=
$cldrName
;
}
}
foreach
(
$allLocalNames
as
$code
=>
$localNames
)
{
foreach
(
$localNames
as
$code2
=>
$localName
)
{
$allMessages
[
$code
][
"cldr-language-name-$code2"
]
=
$localName
;
}
}
foreach
(
$allCldrNames
as
$code
=>
$cldrNames
)
{
foreach
(
$cldrNames
as
$code2
=>
$cldrName
)
{
if
(
!
array_key_exists
(
$code2
,
$allCldrNames
[
'en'
]
)
)
{
fwrite
(
STDERR
,
"In CLDR, language code '$code2' has a name in '$code' but not in English!"
.
PHP_EOL
);
}
}
}
foreach
(
$allLocalNames
as
$code
=>
$localNames
)
{
foreach
(
$localNames
as
$code2
=>
$localName
)
{
if
(
!
array_key_exists
(
$code2
,
$allLocalNames
[
'en'
]
)
)
{
fwrite
(
STDERR
,
"In LocalNames, language code '$code2' has a name in '$code' but not in English!"
.
PHP_EOL
);
}
}
}
foreach
(
$allMessages
as
$code
=>
$messages
)
{
foreach
(
$messages
as
$key
=>
$message
)
{
$code2
=
substr
(
$key
,
19
);
// strlen( 'cldr-language-name-' )
$allMessages
[
'qqq'
][
$key
]
=
"Language name of the [https://hub.toolforge.org/P305:$code2 '$code2'] language code."
;
// TODO improve qqq
if
(
!
array_key_exists
(
$key
,
$allMessages
[
'en'
]
)
)
{
fwrite
(
STDERR
,
"Overall, language code '$code2' has a name in '$code' but not in English!"
.
PHP_EOL
);;
}
}
}
foreach
(
$allMessages
as
$code
=>
$messages
)
{
addToJson
(
$code
,
$messages
);
}
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
16793891
Default Alt Text
T231755 script (php2json – add CldrNames and LocalNames to i18n JSON files (v2) (3 KB)
Attached To
Mode
P61867 T231755 script (php2json – add CldrNames and LocalNames to i18n JSON files (v2)
Attached
Detach File
Event Timeline
Log In to Comment