Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ The basic usage is like this:
languageData.getAutonym( 'en');
// Returns version of the language data or UNKNOWN if not declared
languageData.getVersion();
// Returns an object mapping all territory codes to their language arrays
// e.g. { US: ['en', 'es', ...], DE: ['de'], ... }
languageData.getTerritoriesWithLanguages();
// Returns the languages spoken in a specific territory
languageData.getLanguagesInTerritory( 'US' );

The exposed methods are similar to the methods present in the PHP `LanguageUtil <api/languagedata/languageutil.html>`_ class.

Expand Down
11 changes: 11 additions & 0 deletions src/LanguageUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Wikimedia\LanguageData;

use stdClass;

/**
* A singleton utility class to query the language data.
*/
Expand Down Expand Up @@ -365,6 +367,15 @@ public function getDir( string $languageCode ) {
return false;
}

/**
* Returns all territories and the languages spoken in each.
* @return stdClass An object whose keys are territory codes and whose values
* are arrays of language codes spoken in that territory.
*/
public function getTerritoriesWithLanguages(): stdClass {
return $this->data->territories;
}

/**
* Returns the languages spoken in a territory
* @param string $territory Territory code
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ function getDir( language ) {
return isRtl( language ) ? 'rtl' : 'ltr';
}

/**
* Returns all territories and the languages spoken in each.
*
* @return {Object} An object whose keys are territory codes and whose values
* are arrays of language codes spoken in that territory.
*/
function getTerritoriesWithLanguages() {
return languageData.territories;
}

/**
* Returns the languages spoken in a territory.
*
Expand Down Expand Up @@ -328,6 +338,7 @@ module.exports = {
getRegionGroups,
getScript,
getScriptGroupOfLanguage,
getTerritoriesWithLanguages,
isKnown,
isRedirect,
isRtl,
Expand Down
8 changes: 8 additions & 0 deletions tests/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ describe( 'languagedata', () => {
'An invalid country has no languages and returns an empty array'
);

const territories = languageData.getTerritoriesWithLanguages();
assert.ok( typeof territories === 'object' && territories !== null, 'getTerritoriesWithLanguages() returns an object' );
assert.ok( Array.isArray( territories[ 'US' ] ), 'US territory entry is an array' );
assert.ok( territories[ 'US' ].includes( 'en' ), 'English is listed for the US territory' );
assert.ok( territories[ 'RU' ].includes( 'sah' ), 'Sakha is listed for the RU territory via getTerritoriesWithLanguages()' );
assert.ok( Object.keys( territories ).length > 0, 'getTerritoriesWithLanguages() returns a non-empty set of territories' );
assert.strictEqual( territories[ 'no-such-country' ], undefined, 'An invalid territory code is not present in getTerritoriesWithLanguages()' );

const languagesAM = [ 'atj', 'chr', 'chy', 'cr', 'en', 'es', 'fr', 'gn', 'haw', 'ike-cans', 'ik', 'kl', 'nl', 'pt', 'qu', 'srn', 'yi' ];
assert.deepEqual(
languageData.sortByScriptGroup( languagesAM.sort( languageData.sortByAutonym ) ),
Expand Down
22 changes: 22 additions & 0 deletions tests/php/LanguageUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ public function testGetLanguagesInTerritory() {
$this->assertNotContains( 'de', $actualsAFG );
}

public function testGetTerritoriesWithLanguages() {
$territories = $this->languageUtil->getTerritoriesWithLanguages();

$this->assertObjectHasProperty( 'US', $territories, 'US is a known territory' );
$this->assertContains( 'en', $territories->US, 'English is listed for the US territory' );

$this->assertObjectHasProperty( 'AT', $territories, 'AT is a known territory' );
$this->assertContains( 'de', $territories->AT, 'German is listed for the AT territory' );

$this->assertObjectNotHasProperty(
'no-such-country',
$territories,
'An invalid territory code is not present in getTerritoriesWithLanguages()'
);

$this->assertGreaterThan(
0,
count( (array)$territories ),
'getTerritoriesWithLanguages() returns a non-empty set of territories'
);
}

public function testAddLanguage() {
$this->assertFalse( $this->languageUtil->isKnown( 'xyz' ) );
$this->assertNotContains(
Expand Down
Loading