Page MenuHomePhabricator
Paste P61836

T231755 script (php2json – add CldrNames and LocalNames to i18n JSON files
ActivePublic

Authored by Lucas_Werkmeister_WMDE on May 3 2024, 12:06 PM.
<?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;
}
$allMessages = [];
foreach ( loadCldrNames() as $code => $cldrNames ) {
foreach ( $cldrNames as $code2 => $cldrName ) {
$allMessages[$code]["cldr-languagename-$code2"] = $cldrName;
$allMessages['qqq']["cldr-languagename-$code2"] =
"CLDR language name of the '$code2' language code. " .
'This language name is translated in the CLDR database. ' .
'Any changes you make here will be overwritten the next time the CLDR data is updated; ' .
'please also submit your changes to the CLDR database.'; // TODO link documentation
}
}
foreach ( loadLocalNames() as $code => $localNames ) {
foreach ( $localNames as $code2 => $localName ) {
$allMessages[$code]["cldr-languagename-$code2-override"] = $localName;
$allMessages['qqq']["cldr-languagename-$code2-override"] =
"MediaWiki override of the '$code2' language code. ";
if ( array_key_exists( "cldr-languagename-$code2", $allMessages['qqq'] ) ) {
$allMessages['qqq']["cldr-languagename-$code2-override"] .=
"This replaces {{msg-mw|cldr-languagename-$code2}}. ";
}
$allMessages['qqq']["cldr-languagename-$code2-override"] .=
'We override this language name because TODO.';
}
}
foreach ( $allMessages as $code => $messages ) {
addToJson( $code, $messages );
}