index.js 644 B

123456789101112131415
  1. // @flow
  2. // "lookup" algorithm http://tools.ietf.org/html/rfc4647#section-3.4
  3. // assumes normalized language tags, and matches in a case sensitive manner
  4. module.exports = function lookupClosestLocale (locale/*: string | string[] | void */, available/*: { [string]: any } */)/*: ?string */ {
  5. if (typeof locale === 'string' && available[locale]) return locale
  6. var locales = [].concat(locale || [])
  7. for (var l = 0, ll = locales.length; l < ll; ++l) {
  8. var current = locales[l].split('-')
  9. while (current.length) {
  10. var candidate = current.join('-')
  11. if (available[candidate]) return candidate
  12. current.pop()
  13. }
  14. }
  15. }