File: getBestLocaleMatch.js

package info (click to toggle)
greasemonkey 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,820 kB
  • sloc: xml: 171; makefile: 10
file content (32 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download
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
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import('resource://greasemonkey/util.js');

const EXPORTED_SYMBOLS = ['getBestLocaleMatch'];

// This function tries to find the best matching locale.
// Locales should be given in the form "lang[-COUNTRY]".
// If an exact match (i.e. both lang and country match) can be found, it is
// returned. Otherwise, a partial match based on the lang part is attempted.
// Partial matches without country are preferred over lang matches with
// non-matching country.
// If no locale matches, null is returned.
function getBestLocaleMatch(aPreferred, aAvailable) {
  var preferredLang = aPreferred.split("-")[0];

  var langMatch, partialMatch = null;
  for (var i = 0, current; current = aAvailable[i]; i++) {
    // Both lang and country match
    if (current == aPreferred)
      return current;

    if (current == preferredLang) {
      // Only lang matches, no country
      langMatch = current;
    } else if (current.split("-")[0] == preferredLang) {
      // Only lang matches, non-matching country
      partialMatch = current;
    }
  }

  return langMatch || partialMatch;
}