Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
server: dynamic language directionality
Browse files Browse the repository at this point in the history
Use https://www.npmjs.com/package/rtl-detect to determine language
script directionality.

Bug: T209288
  • Loading branch information
wiese committed Dec 14, 2018
1 parent d60a6eb commit 894571a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -20,6 +20,7 @@
"http-status-codes": "^1.3.0",
"module-alias": "^2.1.0",
"mwbot": "^1.0.10",
"rtl-detect": "^1.0.2",
"ts-deferred": "^1.0.4",
"validate.js": "^0.12.0",
"vue": "^2.5.17",
Expand Down
6 changes: 5 additions & 1 deletion src/server/data-access/ContentLanguagesLanguageRepo.ts
Expand Up @@ -2,6 +2,7 @@ import LanguageRepository from '@/common/data-access/LanguageRepository';
import LanguageCollection from '@/datamodel/LanguageCollection';
import Language from '@/datamodel/Language';
import WikibaseContentLanguagesRepo, { WikibaseApiContentLanguages } from './WikibaseContentLanguagesRepo';
import RtlDetectLib from 'rtl-detect';

export default class ContentLanguagesLanguageRepo implements LanguageRepository {
private languagesRepo: WikibaseContentLanguagesRepo;
Expand All @@ -17,7 +18,10 @@ export default class ContentLanguagesLanguageRepo implements LanguageRepository
Object.entries( contentLanguages ).forEach( ( [ languageCode, language ] ) => {
languages[ languageCode ] = {
code: languageCode,
directionality: 'ltr',
// this does not do full justice to the directionality question
// * languages do not have directionality - scripts do
// * the configured wikibase instance may have more/different languages
directionality: RtlDetectLib.getLangDir( languageCode ),
} as Language;
} );

Expand Down
5 changes: 5 additions & 0 deletions src/types/server/rtl-detect.d.ts
@@ -0,0 +1,5 @@
declare module 'rtl-detect' {
export default class RtlDetectLib {
public static getLangDir( code: string ): string;
}
}
42 changes: 35 additions & 7 deletions tests/unit/server/data-access/ContentLanguagesLanguageRepo.spec.ts
Expand Up @@ -3,6 +3,7 @@ import MwBotWikibaseContentLanguagesRepo from '@/server/data-access/MwBotWikibas
import mwbot from 'mwbot';
import ContentLanguagesLanguageRepo from '@/server/data-access/ContentLanguagesLanguageRepo';
import { WikibaseApiContentLanguages } from '@/server/data-access/WikibaseContentLanguagesRepo';
import RtlDetectLib from 'rtl-detect';

function newWikibaseContentLanguagesRepository( contentLanguagesRepo: any ) {
return new ContentLanguagesLanguageRepo(
Expand All @@ -23,11 +24,11 @@ describe( 'ContentLanguagesLanguageRepo', () => {
getContentLanguages.mockResolvedValue( {
en: {
code: 'en',
name: 'Englisch',
name: 'English',
},
de: {
code: 'de',
name: 'Deutsch',
ar: {
code: 'ar',
name: 'Arabic',
},
} as WikibaseApiContentLanguages );
const contentLanguagesRepo = {
Expand All @@ -42,15 +43,42 @@ describe( 'ContentLanguagesLanguageRepo', () => {
code: 'en',
directionality: 'ltr',
},
de: {
code: 'de',
directionality: 'ltr',
ar: {
code: 'ar',
directionality: 'rtl',
},
} as LanguageCollection );
done();
} );
} );

it( 'loads language directionality from rtl-detect', ( done ) => {
const spyGetLangDir = jest.spyOn( RtlDetectLib, 'getLangDir' );

const getContentLanguages = jest.fn();
getContentLanguages.mockResolvedValue( {
en: {
code: 'en',
name: 'English',
},
ar: {
code: 'ar',
name: 'Arabic',
},
} as WikibaseApiContentLanguages );
const contentLanguagesRepo = {
getContentLanguages,
};
const repo = newWikibaseContentLanguagesRepository( contentLanguagesRepo );

repo.getLanguages().then( ( languages: LanguageCollection ) => {
expect( spyGetLangDir ).toBeCalledTimes( 2 );
expect( spyGetLangDir ).toBeCalledWith( 'en' );
expect( spyGetLangDir ).toBeCalledWith( 'ar' );
done();
} );
} );

} );

} );

0 comments on commit 894571a

Please sign in to comment.