Skip to content

Commit

Permalink
Lowercase language code from item statement
Browse files Browse the repository at this point in the history
We now use the property “IETF language tag” for the automatic language
code on Wikidata; the subtag of these tags is case-insensitive, but some
capitalization is recommended [1][2]. For MediaWiki or Wikidata, on the
other hand, the subtags generally need to be all-lowercase (except our
special mis-x-Qid codes, see T317863). Reconcile this by lowercasing all
language codes from items in the store action.

[1]: https://en.wikipedia.org/wiki/IETF_language_tag#Syntax_of_language_tags
[2]: https://en.wikipedia.org/w/index.php?title=IETF_language_tag&oldid=1181524280#Syntax_of_language_tags

Bug: T349652
  • Loading branch information
lucaswerkmeister committed Oct 30, 2023
1 parent a02b386 commit 9187db5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export default function createActions(
{ commit }: RootContext,
languageCode: string | undefined | null | false,
) {
if ( typeof languageCode === 'string' ) {
languageCode = languageCode.toLowerCase(); // T349652
}
if ( typeof languageCode === 'string' && !languageCodesProvider.isValid( languageCode ) ) {
languageCode = false;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/store/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ describe( 'HANDLE_LANGUAGE_CHANGE', () => {
} );

it( 'validates and sets to state if valid lang code was returned', async () => {
const getLanguageCodeFromItemMock = jest.fn().mockResolvedValue( 'de' );
// T349652: en-GB from item (preferred IETF lanugage tag format) is lowercased to en-gb (MediaWiki code)

Check warning on line 358 in tests/unit/store/actions.test.ts

View workflow job for this annotation

GitHub Actions / code-quality

This line has a length of 112. Maximum allowed is 100
const getLanguageCodeFromItemMock = jest.fn().mockResolvedValue( 'en-GB' );
const isValidMock = jest.fn().mockReturnValue( true );
const actions = createActions(
unusedLexemeCreator,
Expand Down Expand Up @@ -387,9 +388,9 @@ describe( 'HANDLE_LANGUAGE_CHANGE', () => {

expect( store.state.language ).toStrictEqual( { id: 'Q456' } );
expect( store.state.spellingVariant ).toBe( '' );
expect( store.state.languageCodeFromLanguageItem ).toBe( 'de' );
expect( store.state.languageCodeFromLanguageItem ).toBe( 'en-gb' );
expect( getLanguageCodeFromItemMock ).toHaveBeenCalledWith( 'Q456' );
expect( isValidMock ).toHaveBeenCalledWith( 'de' );
expect( isValidMock ).toHaveBeenCalledWith( 'en-gb' );
} );

it( 'validates and sets false state if invalid lang code was returned', async () => {
Expand Down

0 comments on commit 9187db5

Please sign in to comment.