All files / ext.wikilambda.app/store/stores languages.js

92.39% Statements 85/92
76.92% Branches 10/13
100% Functions 7/7
92.39% Lines 85/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 266x 266x 266x 266x         266x 103x 103x 103x 103x 103x 103x 103x 103x 190x 190x 190x 190x       190x 103x 103x 103x 103x 103x 103x 103x 103x 313x 103x 103x 103x 103x 103x 103x 103x 103x 253x 253x 253x 103x 103x 103x 103x 103x 103x 103x 103x 305x 305x 305x 305x 305x 305x 305x 19x 19x 19x 19x 305x 305x 305x 103x 103x 103x 103x  
/*!
 * WikiLambda Vue editor: Pinia store for language-related state, actions, mutations and getters
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' );
const LabelData = require( '../classes/LabelData.js' );
 
module.exports = {
	state: {},
 
	getters: {
		/**
		 * Get user preferred language code, from config variable zlang
		 *
		 * @return {string}
		 */
		getUserLangCode: function () {
			// If wikilambda config is set up, return zlang
			if ( this.getWikilambdaConfig.zlang ) {
				return this.getWikilambdaConfig.zlang;
			}
			// Else return userLang only if it's a valid language code
			// or default to English if it's not:
			const userLang = mw.config.get( 'wgUserLanguage' );
			const contentLang = mw.config.get( 'wgPageContentLanguage' );
			return $.uls.data.languages[ userLang ] ? userLang : contentLang;
		},
 
		/**
		 * Get user preferred language zid, from config variable zlangZid
		 *
		 * @return {string}
		 */
		getUserLangZid: function () {
			// If wikilambda config is set up, return zlangZid
			if ( this.getWikilambdaConfig.zlangZid ) {
				return this.getWikilambdaConfig.zlangZid;
			}
			// Else return the Zid for getUserLangCode if it has been fetched
			// or default to English if it hasn't:
			const langZid = this.getLanguageZidOfCode( this.getUserLangCode );
			return langZid || Constants.Z_NATURAL_LANGUAGE_ENGLISH;
		},
 
		/**
		 * Return user requested lang, which might not be a valid WF language.
		 *
		 * @return {string}
		 */
		getUserRequestedLang: function () {
			return mw.language.getFallbackLanguageChain()[ 0 ];
		},
 
		/**
		 * Return the list of fallback languages in their Zid representations.
		 *
		 * @return {Array}
		 */
		getFallbackLanguageZids: function () {
			return mw.language.getFallbackLanguageChain()
				.map( ( code ) => this.getLanguageZidOfCode( code ) )
				.filter( ( zid ) => !!zid );
		},
 
		/**
		 * Get `LabelData` object for a given language code.
		 *
		 * @return {Function} A function that accepts a language code and returns its `LabelData`.
		 */
		getLabelDataForLangCode: function () {
			/**
			 * Build a `LabelData` object for the specified language code.
			 *
			 * @param {string} langCode The language code to retrieve the `LabelData` for.
			 * @return {LabelData} The `LabelData` object containing label, language code, and directionality.
			 */
			const findLabelDataForLangCode = ( langCode ) => new LabelData(
				null,
				null,
				langCode,
				this.getLanguageIsoCodeOfZLang( langCode )
			);
			return findLabelDataForLangCode;
		}
	},
 
	actions: {}
};