import url from 'url';
import cookie from 'cookie';
import _ from 'lodash';

Accept

class
 new Accept() 

class Accept {

getAcceptLanguage

method
 Accept.prototype.getAcceptLanguage() 

Option name Type Description
req object

The request object.

return string

The parsed Accept-Language.

getAcceptLanguage(req) {
    if (req)
      this['accept-language'] = req.header['accept-language'] ||
      req.headers['accept-language'] || '';
    else
      this['accept-language'] = this.headers['accept-language'];
    return this['accept-language'] || undefined;
  }

getLocale

method
 Accept.prototype.getLocale() 

getLocale() {
    return this.locale;
  }

setLocale

method
 Accept.prototype.setLocale() 

Option name Type Description
locale string

The locale to override the current.

Sets the locale.

setLocale(locale) {
    this.override = true;
    this.locale = this.isSupported(locale);
    return this.locale;
  }
  // From accept-language, `Accept-Language: ja`

getFromHeader

method
 Accept.prototype.getFromHeader() 

Option name Type Description
req object

The request object.

fallback Boolean

Fallback to default.

return string, string

The parsed locale.

Parses the Accept-Language.

getFromHeader(req, fallback) {
    this.getAcceptLanguage(req);
    var reg = /(^|,\s*)([a-z-0-9-]+)/gi,
      match, result;
    while ((match = reg.exec(this['accept-language']))) {
      if (!result)
        result = match[2];
    }
    if (req)
      return result || undefined;
    else {
      this.locale = result = this.isSupported(result);
      return fallback ? result || undefined : (result || undefined);
    }
  }
  // From query, 'lang=en'

getFromQuery

method
 Accept.prototype.getFromQuery() 

Option name Type Description
key string

The key for the query.

fallback Boolean

Fallback to default.

return string

The parsed locale.

Parses the query.

getFromQuery(key, fallback) {
    var result;
    var query;
    if (this.isKoa || this.isHapi)
      query = this.request.query;
    else
      query = this.request.query ||
      url.parse(this.request.url, true).query;
    this.locale = result = this.isSupported(!_.isEmpty(query) ? query[key] ||
      query[this.options.keys.query] : undefined);
    return fallback ? result || undefined : (result || undefined);
  }
  // From domain

getFromDomain

method
 Accept.prototype.getFromDomain() 

Option name Type Description
fallback Boolean

Fallback to default.

return string

The parsed locale.

Parses the domain.

getFromDomain(fallback) {
    var result,
      hostname = this.request.hostname || this.request.info.hostname;
    result = hostname ? hostname
      .toString()
      .toLowerCase()
      .trim()
      .split(':')[0].split(/\./gi)
      .reverse()[0] : undefined;
    this.locale = result = this.isSupported(result);
    return fallback ? result || undefined : (result || undefined);
  }
  // From subdomain, 'en.gengojs.com'

getFromSubdomain

method
 Accept.prototype.getFromSubdomain() 

Option name Type Description
fallback Boolean

Fallback to default.

return string

The parsed locale.

Parses the subdomain.

getFromSubdomain(fallback) {
    var result;
    if (this.isKoa)
      result = this.request.subdomains[0];
    else
      result = this.headers.host.split('.')[0];
    this.locale = result = this.isSupported(result);
    return fallback ? result || undefined : (result || undefined);
  }
  // From cookie, 'lang=ja'

getFromCookie

method
 Accept.prototype.getFromCookie() 

Option name Type Description
key string

The key for the cookie.

fallback Boolean

Fallback to default.

return string

The parsed locale.

Parses the cookie.

getFromCookie(key, fallback) {
    var result;
    result = this.cookie ? cookie.parse(this.cookie)[key] ||
      cookie.parse(this.cookie)[this.options.keys.cookie] : undefined;
    this.locale = result = this.isSupported(result);
    return fallback ? result || undefined : (result || undefined);
  }
  // From URL, 'http://gengojs.com/en'

getFromUrl

method
 Accept.prototype.getFromUrl() 

Option name Type Description
fallback Boolean

Fallback to default.

return string

The parsed locale.

Parses the url.

getFromUrl(fallback) {
    var result,
      path = this.request.path || this.request.url.path;
    this.locale = result = this.isSupported(
      path ? path
      .substring(1)
      .split('/')
      .shift() : '');
    return fallback ? result || undefined : (result || undefined);
  }
  // From all, when specified in options

detectLocale

method
 Accept.prototype.detectLocale() 

Option name Type Description
locale string

The locale to override.

return string

The parsed locale.

Parses the locale by the specified type of parsing.

detectLocale(locale) {
    _.forEach(this.options.detect, function(value, key) {
      switch (key) {
        case 'header':
          if (value && !this.override)
            this.locale = this.getFromHeader();
          break;
        case 'cookie':
          if (value && !this.override)
            this.locale = this.getFromCookie(this.options.keys.cookie);
          break;
        case 'url':
          if (value && !this.override)
            this.locale = this.getFromUrl();
          break;
        case 'domain':
          if (value && !this.override)
            this.locale = this.getFromDomain();
          break;
        case 'subdomain':
          if (value && !this.override)
            this.locale = this.getFromSubdomain();
          break;
        case 'query':
          if (value && !this.override)
            this.locale = this.getFromQuery(this.options.keys.query);
          break;
      }
    }, this);

    // override?
    if (locale)
      this.locale = this.isSupported(locale) || this.locale;
    // reset the override
    if (this.override)
      this.override = false;

    return this.locale;
  }
Option name Type Description
req object

The request object.

options object

The options to configure accept.

return Accept

The Accept instance.

export default (req, options) => {
  'use strict';
  return new Accept(req, options);
};