GUAC-1217: Fallback to standard dialects when no specific dialect is available/requested.

This commit is contained in:
Michael Jumper
2015-06-05 19:39:59 -07:00
parent f711baefab
commit ed744d9d1d
7 changed files with 32 additions and 4 deletions

View File

@@ -74,12 +74,15 @@ public class LanguageResourceService {
* The regular expression to use for parsing the language key from the
* filename.
*/
private static final Pattern LANGUAGE_KEY_PATTERN = Pattern.compile(".*/([a-z]+_[A-Z]+)\\.json");
private static final Pattern LANGUAGE_KEY_PATTERN = Pattern.compile(".*/([a-z]+(_[A-Z]+)?)\\.json");
/**
* Map of all language resources by language key. Language keys are
* language and country code pairs, separated by an underscore, like
* "en_US".
* "en_US". The country code and underscore SHOULD be omitted in the case
* that only one dialect of that language is defined, or in the case of the
* most universal or well-supported of all supported dialects of that
* language.
*/
private final Map<String, Resource> resources = new HashMap<String, Resource>();

View File

@@ -30,7 +30,7 @@ angular.module('index').config(['$injector', function($injector) {
var preferenceServiceProvider = $injector.get('preferenceServiceProvider');
// Fallback to US English
var fallbackLanguages = ['en_US'];
var fallbackLanguages = ['en'];
// Prefer chosen language, use fallback languages if necessary
$translateProvider.fallbackLanguage(fallbackLanguages);

View File

@@ -81,6 +81,31 @@ angular.module('locale').factory('translationLoader', ['$injector', function tra
};
/**
* Given a valid language key, returns all possible legal variations of
* that key. Currently, this will be the given key and the given key
* without the country code. If the key has no country code, only the
* given key will be included in the returned array.
*
* @param {String} key
* The language key to generate variations of.
*
* @returns {String[]}
* All possible variations of the given language key.
*/
var getKeyVariations = function getKeyVariations(key) {
var underscore = key.indexOf('_');
// If no underscore, only one possibility
if (underscore === -1)
return [key];
// Otherwise, include the lack of country code as an option
return [key, key.substr(0, underscore)];
};
/**
* Custom loader function for angular-translate which loads the desired
* language file dynamically via HTTP. If the language file cannot be
@@ -101,7 +126,7 @@ angular.module('locale').factory('translationLoader', ['$injector', function tra
var requestedKey = options.key;
// Append fallback languages to requested language
var keys = [requestedKey].concat(options.fallbackLanguages);
var keys = getKeyVariations(requestedKey).concat(options.fallbackLanguages);
// Satisfy the translation request
satisfyTranslation(translation, requestedKey, keys);