Yurik has uploaded a new change for review. https://gerrit.wikimedia.org/r/318663
Change subject: WIP: Add language switcher ...................................................................... WIP: Add language switcher Change-Id: Ib026d5068af017cd725ac658d11eeca540b31463 --- M includes/JCDataContent.php M includes/JCTabularContent.php 2 files changed, 52 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig refs/changes/63/318663/1 diff --git a/includes/JCDataContent.php b/includes/JCDataContent.php index 8850277..206b8f4 100644 --- a/includes/JCDataContent.php +++ b/includes/JCDataContent.php @@ -103,4 +103,27 @@ return $html; } + + /** + * Get a list of language codes declared in any of the localizable data + * @return string[]|null + */ + public function getSupportedLanguages() { + if ( !$this->isValid() ) { + return null; + } + $result = []; + $this->addSupportedLanguages( $result ); + $result = array_keys( $result ); + ksort( $result ); + return $result; + } + + /** + * Add any language codes declared by any localizable data as keys to the result array + * @param array $result + */ + protected function addSupportedLanguages( & $result ) { + $result += $this->getData()->info; + } } diff --git a/includes/JCTabularContent.php b/includes/JCTabularContent.php index bcddf3a..8122386 100644 --- a/includes/JCTabularContent.php +++ b/includes/JCTabularContent.php @@ -122,17 +122,11 @@ $result->headers = $data->headers; $result->types = $data->types; $result->titles = array_map( $localize, $data->titles ); - if ( !in_array( 'localized', $data->types ) ) { + $isLocalized = $this->getLocalizedColumns(); + if ( !$isLocalized ) { // There are no localized strings in the data, optimize $result->rows = $data->rows; } else { - // Make a list of all columns that need to be localized - $isLocalized = []; - foreach ( $data->types as $ind => $type ) { - if ( $type === 'localized' ) { - $isLocalized[] = $ind; - } - } $result->rows = array_map( function ( $row ) use ( $localize, $isLocalized ) { foreach ( $isLocalized as $ind ) { $row[$ind] = $localize( $row[$ind] ); @@ -141,4 +135,31 @@ }, $data->rows ); } } + + /** + * Make a list of indexes of all localized columns + */ + private function getLocalizedColumns() { + $data = $this->getData(); + $isLocalized = []; + foreach ( $data->types as $ind => $type ) { + if ( $type === 'localized' ) { + $isLocalized[] = $ind; + } + } + return $isLocalized; + } + + protected function addSupportedLanguages( & $result ) { + $data = $this->getData(); + $result += $data->titles; + $isLocalized = $this->getLocalizedColumns(); + if ( $isLocalized ) { + foreach ( $data->rows as $row ) { + foreach ( $isLocalized as $idx ) { + $result += $row[$idx]; + } + } + } + } } -- To view, visit https://gerrit.wikimedia.org/r/318663 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib026d5068af017cd725ac658d11eeca540b31463 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/JsonConfig Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits