diff --git a/data/langdb.yaml b/data/langdb.yaml index 2d06f35..f67ffbe 100644 --- a/data/langdb.yaml +++ b/data/langdb.yaml @@ -551,6 +551,8 @@ rtlscripts: regiongroups: # Worldwide, international WW: 1 + # Special languages. To be shown near Worldwide when relevant. + SP: 1 # America AM: 2 # Europe diff --git a/i18n/en.json b/i18n/en.json index 9722941..2aa84b4 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -7,6 +7,7 @@ }, "uls-select-language": "Select Language", "uls-region-WW": "Worldwide", +"uls-region-SP": "Special", "uls-region-AM": "America", "uls-region-AF": "Africa", "uls-region-EU": "Europe", @@ -15,7 +16,7 @@ "uls-region-PA": "Pacific", "uls-no-results-found": "No results found", "uls-common-languages": "Common languages", -"uls-no-results-suggestion-title": "You may be interested in:", +"uls-no-results-suggestion-title": "You may be interested in:", "uls-search-help": "You can search by language name, script name, ISO code of language or you can browse by region:", "uls-search-placeholder": "Language search" } diff --git a/i18n/qqq.json b/i18n/qqq.json index 0e0c2f5..3db0d33 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -7,6 +7,7 @@ }, "uls-select-language": "Language Selector Title", "uls-region-WW": "Label for worldwide languages. They are languages spoken in multiple countries. Eg: English, French, Spanish etc. This label is used in the map region of ULS and as the title of section showing worldwide languages. Translation should not be descriptive.", + "uls-region-SP": "Label for the special languages section. This section may include special custom languages that can be added in run time. It's shown after the Worldwide section.", "uls-region-AM": "Label for America", "uls-region-AF": "Label for Africa", "uls-region-EU": "Label for Europe", @@ -18,4 +19,4 @@ "uls-no-results-suggestion-title": "Title for language suggestion in 'no results found' screen", "uls-search-help": "Help text for searching. After this text, there will be a set of links for regions", "uls-search-placeholder": "Placeholder text in search box" -} \ No newline at end of file +} diff --git a/src/jquery.uls.core.js b/src/jquery.uls.core.js index c736b35..09f65ec 100644 --- a/src/jquery.uls.core.js +++ b/src/jquery.uls.core.js @@ -236,7 +236,8 @@ quickList: uls.options.quickList, clickhandler: $.proxy( uls.select, uls ), lazyload: uls.options.lazyload, - source: uls.$languageFilter + source: uls.$languageFilter, + showRegions: uls.options.showRegions } ).data( 'lcd' ); uls.$languageFilter.languagefilter( { @@ -366,7 +367,8 @@ searchAPI: null, // Language search API languages: $.uls.data.getAutonyms(), // Languages to be used for ULS, default is all languages quickList: null, // Array of language codes or function that returns such - lazyload: true // Lazy load the language list when scrolled. + lazyload: true, // Lazy load the language list when scrolled. + showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA'] }; // Define a dummy i18n function, if jquery.i18n not integrated. diff --git a/src/jquery.uls.data.utils.js b/src/jquery.uls.data.utils.js index ed28ba1..ab0b3d6 100644 --- a/src/jquery.uls.data.utils.js +++ b/src/jquery.uls.data.utils.js @@ -421,4 +421,38 @@ $.uls.data.getLanguagesInTerritory = function ( territory ) { return $.uls.data.territories[territory]; }; + + /** + * Adds a language in run time and sets its options as provided. + * If the target option is provided, the language is defined as a redirect. + * Other possible options are script, regions and autonym. + * + * @param code string New language code. + * @param options Object Language properties. + * @return list of language codes + */ + $.uls.data.addLanguage = function( code, options ) { + if ( options.target ) { + $.uls.data.languages[code] = [options.target]; + } else { + $.uls.data.languages[code] = [options.script, options.regions, options.autonym]; + } + }; + + /** + * Removes a language from the langdb in run time. + * + * @param code string Language code to delete. + * @return true if the language was removed, false otherwise. + */ + $.uls.data.deleteLanguage = function( code ) { + if ( $.uls.data.languages[code] ) { + delete $.uls.data.languages[code]; + + return true; + } + + return false; + }; + } ( jQuery ) ); diff --git a/src/jquery.uls.lcd.js b/src/jquery.uls.lcd.js index eff660e..9a654b5 100644 --- a/src/jquery.uls.lcd.js +++ b/src/jquery.uls.lcd.js @@ -147,7 +147,10 @@ $rowDiv.append( $ul ); } - $divRegionCode.show(); + // Don't show the region unless it was enabled + if ( $.inArray( regionCode, this.options.showRegions ) > -1 ) { + $divRegionCode.show(); + } return $ul; }, @@ -158,6 +161,7 @@ regions = { // These are fallback text when i18n library not present WW: 'Worldwide', + SP: 'Special', AM: 'America', EU: 'Europe', ME: 'Middle East', @@ -317,6 +321,7 @@ $.fn.lcd.defaults = { languages: null, + showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA'], lazyload: true }; diff --git a/test/jquery.uls.test.js b/test/jquery.uls.test.js index 1bc3f71..a00fb0e 100644 --- a/test/jquery.uls.test.js +++ b/test/jquery.uls.test.js @@ -89,7 +89,16 @@ assert.ok( $.fn.uls, "$.fn.uls is defined" ); } ); - test( "-- $.uls.data testing", 41, function ( assert ) { + test( "-- $.uls.data testing", 45, function ( assert ) { + // Add a language in run time. + // This is done early to make sure that it doesn't break other functions. + $.uls.data.addLanguage( 'qqq', { + script: 'Latn', + regions: ['SP'], + autonym: 'Language documentation' + } ); + + assert.ok( $.uls.data.getAutonym( 'qqq' ), 'Language documentation', 'Language qqq was added with the correct autonym' ); assert.strictEqual( $.uls.data.isRedirect( 'sr-ec' ), 'sr-cyrl', "'sr-ec' is a redirect to 'sr-cyrl'" ); var autonyms = $.uls.data.getAutonyms(); @@ -166,14 +175,14 @@ ); assert.deepEqual( $.uls.data.getRegionsInGroup( 3 ), [ - "EU", "ME", "AF" - ], "regions in group 3 are selected correctly" ); + 'EU', 'ME', 'AF' + ], 'regions in group 3 are selected correctly' ); assert.deepEqual( $.uls.data.getRegionsInGroup( 2 ), [ - "AM" - ], "regions in group 2 are selected correctly" ); + 'AM' + ], 'regions in group 2 are selected correctly' ); assert.deepEqual( $.uls.data.getRegionsInGroup( 1 ), [ - "WW" - ], "regions in group 1 are selected correctly" ); + 'WW', 'SP' + ], 'regions in group 1 are selected correctly' ); var languagesByScriptInAM = $.uls.data.getLanguagesByScriptInRegion( "AM" ); assert.deepEqual( languagesByScriptInAM['Cans'], [ @@ -204,8 +213,8 @@ ], 'All languages in the Greek script found' ); assert.deepEqual( $.uls.data.getAllRegions(), [ - "WW", "AM", "EU", "ME", "AF", "AS", "PA" - ], "All regions found" ); + 'WW', 'SP', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA' + ], 'All regions found' ); // autonyms: gn: avañe'ẽ, de: deutsch, hu: magyar, fi: suomi assert.deepEqual( ['de', 'fi', 'gn', 'hu'].sort( $.uls.data.sortByAutonym ), [ @@ -219,6 +228,10 @@ assert.ok( $.inArray( "sah", $.uls.data.getLanguagesInTerritory( "RU" ) ) > -1, "Sakha language is spoken in Russia" ); + + assert.ok( $.uls.data.deleteLanguage( 'qqq' ), 'Deleting language qqq, which was added earlier, returns true.' ); + assert.strictEqual( $.uls.data.languages['qqq'], undefined, 'Data about qqq is undefined after being deleted.' ); + assert.ok( !$.uls.data.deleteLanguage( 'qqr' ), 'Deleting language qqr, which was never added, returns false.' ); } ); }( jQuery ) );