12 private $extensionDir;
16 private $excludedMsgs;
20 private $coreDataCache;
23 parent::__construct();
24 $this->addArg(
'extension',
'The extension name' );
25 $this->addOption(
'outdir',
26 'The output directory, default $IP/languages/i18n',
false,
true );
32 $this->outDir = $this->getOption(
'outdir', MW_INSTALL_PATH .
'/languages/i18n' );
33 if ( !file_exists( $this->outDir ) ) {
34 mkdir( $this->outDir, 0777,
true );
37 $this->extName = $this->getArg();
38 $extJsonPath = $this->extensionDir .
"/{$this->extName}/extension.json";
39 $extJson = file_get_contents( $extJsonPath );
40 if ( $extJson ===
false ) {
41 $this->fatalError(
"Unable to open \"$extJsonPath\"" );
43 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
45 $this->excludedMsgs = [];
46 foreach ( [
'namemsg',
'descriptionmsg' ] as $key ) {
47 if ( isset( $extData[$key] ) ) {
48 $this->excludedMsgs[] = $extData[$key];
52 foreach ( $this->getMessagesDirs( $extData ) as $dir ) {
53 $this->processDir( $dir );
57 private function init() {
58 $services = $this->getServiceContainer();
59 $config = $services->getMainConfig();
60 $this->extensionDir = $config->get( MainConfigNames::ExtensionDirectory );
63 private function getMessagesDirs( $extData ) {
64 if ( isset( $extData[
'MessagesDirs'] ) ) {
66 foreach ( $extData[
'MessagesDirs'] as $dirs ) {
67 if ( is_array( $dirs ) ) {
68 foreach ( $dirs as $dir ) {
69 $messagesDirs[] = $dir;
72 $messagesDirs[] = $dirs;
76 $messagesDirs = [
'i18n' ];
81 private function processDir( $dir ) {
82 $path = $this->extensionDir .
"/{$this->extName}/$dir";
84 foreach (
new DirectoryIterator(
$path ) as $file ) {
85 if ( !$file->isDot() && str_ends_with( $file->getFilename(),
'.json' ) ) {
87 substr( $file->getFilename(), 0, -5 ),
94 private function processFile( $lang, $extI18nPath ) {
95 $extJson = file_get_contents( $extI18nPath );
96 if ( $extJson ===
false ) {
97 $this->error(
"Unable to read i18n file \"$extI18nPath\"" );
100 $extData = json_decode( $extJson, JSON_THROW_ON_ERROR );
101 $coreData = $this->getCoreData( $lang );
103 if ( isset( $extData[
'@metadata'][
'authors'] ) ) {
104 $authors = array_unique( array_merge(
105 $coreData[
'@metadata'][
'authors'] ?? [],
106 $extData[
'@metadata'][
'authors']
109 foreach ( $authors as &$author ) {
110 $author = (string)$author;
113 $coreData[
'@metadata'][
'authors'] = $authors;
116 foreach ( $extData as $name => $value ) {
117 if ( str_starts_with( $name,
'@' ) ) {
120 if ( in_array( $name, $this->excludedMsgs ) ) {
123 $coreData[$name] = $value;
126 $this->setCoreData( $lang, $coreData );
129 private function getCoreData( $lang ) {
130 if ( !isset( $this->coreDataCache[$lang] ) ) {
131 $corePath = MW_INSTALL_PATH .
"/languages/i18n/$lang.json";
133 $coreJson = @file_get_contents( $corePath );
134 if ( $coreJson ===
false ) {
135 $this->error(
"Warning: discarding extension localisation " .
136 "for language \"$lang\" not present in core" );
140 $this->coreDataCache[$lang] = json_decode( $coreJson, JSON_THROW_ON_ERROR );
142 return $this->coreDataCache[$lang];
145 private function setCoreData( $lang, $data ) {
146 if ( !isset( $this->coreDataCache[$lang] ) ) {
151 $this->coreDataCache[$lang] = $data;
152 $outPath =
"{$this->outDir}/$lang.json";
153 if ( !file_put_contents(
155 FormatJson::encode( $data,
"\t", FormatJson::ALL_OK ) .
"\n"
157 $this->error(
"Unable to write core i18n file \"$outPath\"" );