13 private $extensionDir;
17 private $excludedMsgs;
21 private $coreDataCache;
24 parent::__construct();
25 $this->
addArg(
'extension',
'The extension name' );
27 'The output directory, default $IP/languages/i18n',
false,
true );
33 $this->outDir = $this->
getOption(
'outdir', MW_INSTALL_PATH .
'/languages/i18n' );
34 if ( !file_exists( $this->outDir ) ) {
35 mkdir( $this->outDir, 0777,
true );
38 $this->extName = $this->
getArg();
39 $extJsonPath = $this->extensionDir .
"/{$this->extName}/extension.json";
40 $extJson = file_get_contents( $extJsonPath );
41 if ( $extJson ===
false ) {
42 $this->
fatalError(
"Unable to open \"$extJsonPath\"" );
44 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
46 $this->excludedMsgs = [];
47 foreach ( [
'namemsg',
'descriptionmsg' ] as $key ) {
48 if ( isset( $extData[$key] ) ) {
49 $this->excludedMsgs[] = $extData[$key];
53 foreach ( $this->getMessagesDirs( $extData ) as $dir ) {
54 $this->processDir( $dir );
58 private function init() {
60 $config = $services->getMainConfig();
61 $this->extensionDir = $config->get( MainConfigNames::ExtensionDirectory );
64 private function getMessagesDirs( array $extData ): array {
65 if ( isset( $extData[
'MessagesDirs'] ) ) {
67 foreach ( $extData[
'MessagesDirs'] as $dirs ) {
68 if ( is_array( $dirs ) ) {
69 foreach ( $dirs as $dir ) {
70 $messagesDirs[] = $dir;
73 $messagesDirs[] = $dirs;
77 $messagesDirs = [
'i18n' ];
82 private function processDir(
string $dir ) {
83 $path = $this->extensionDir .
"/{$this->extName}/$dir";
85 foreach (
new DirectoryIterator(
$path ) as $file ) {
86 if ( !$file->isDot() && str_ends_with( $file->getFilename(),
'.json' ) ) {
88 substr( $file->getFilename(), 0, -5 ),
95 private function processFile(
string $lang,
string $extI18nPath ) {
96 $extJson = file_get_contents( $extI18nPath );
97 if ( $extJson ===
false ) {
98 $this->error(
"Unable to read i18n file \"$extI18nPath\"" );
101 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
102 $coreData = $this->getCoreData( $lang );
104 if ( isset( $extData[
'@metadata'][
'authors'] ) ) {
105 $authors = array_unique( array_merge(
106 $coreData[
'@metadata'][
'authors'] ?? [],
107 $extData[
'@metadata'][
'authors']
110 foreach ( $authors as &$author ) {
111 $author = (string)$author;
114 $coreData[
'@metadata'][
'authors'] = $authors;
117 foreach ( $extData as $name => $value ) {
118 if ( str_starts_with( $name,
'@' ) ) {
121 if ( in_array( $name, $this->excludedMsgs ) ) {
124 $coreData[$name] = $value;
127 $this->setCoreData( $lang, $coreData );
131 private function getCoreData(
string $lang ) {
132 if ( !isset( $this->coreDataCache[$lang] ) ) {
133 $corePath = MW_INSTALL_PATH .
"/languages/i18n/$lang.json";
135 $coreJson = @file_get_contents( $corePath );
136 if ( $coreJson ===
false ) {
137 $this->error(
"Warning: discarding extension localisation " .
138 "for language \"$lang\" not present in core" );
142 $this->coreDataCache[$lang] = json_decode( $coreJson, JSON_THROW_ON_ERROR );
144 return $this->coreDataCache[$lang];
151 private function setCoreData(
string $lang, $data ) {
152 if ( !isset( $this->coreDataCache[$lang] ) ) {
157 $this->coreDataCache[$lang] = $data;
158 $outPath =
"{$this->outDir}/$lang.json";
159 if ( !file_put_contents(
161 FormatJson::encode( $data,
"\t", FormatJson::ALL_OK ) .
"\n"
163 $this->error(
"Unable to write core i18n file \"$outPath\"" );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.