gengojs-default-plugin
A gengojs plugin for gengojs-accept

import accept from 'gengojs-accept';
import debug from 'gengojs-debug';
import _ from 'lodash';
var log = debug('header');
class Header {

constructor

constructor
 Header.prototype.constructor() 

Option name Type Description
request object
  • The request object
options object
  • The options object
constructor(request, options) {
    // Debug
    log.debug('header', 'debug',
        `class: ${Header.name}`, 'process: constructor')
      .debug(`request exists: ${!!request}`)
      .debug(`options exists: ${!!options}`)
      .debug(
        `gengojs-accept exists: ${
        !!(this.header = accept(request, options))
      }`)
      .debug(`locale: ${
        (this.locale = this.header.getLocale())
      }`);
  }

getAcceptLanguge

method
 Header.prototype.getAcceptLanguge() 

Option name Type Description
[request] object
  • The request object

Parses the Accept-Launguage from the header

getAcceptLanguge() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getAcceptLanguge.name}`);

    this.locale = this.header
      .getAcceptLanguage
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getLocale

method
 Header.prototype.getLocale() 

Returns the current locale

getLocale() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getLocale.name}`);

    this.locale = this.header
      .getLocale
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

setLocale

method
 Header.prototype.setLocale() 

Option name Type Description
- string

The locale to override

Sets the current locale

setLocale() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.setLocale.name}`);

    this.locale = this.header
      .setLocale
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromHeader

method
 Header.prototype.getFromHeader() 

Option name Type Description
[request] object
  • The request object
[fallback] string
  • The locale to fallback

Parses the Accept-Language from the header and extracts the locale

getFromHeader() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromHeader.name}`);

    this.locale = this.header
      .getFromHeader
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromQuery

method
 Header.prototype.getFromQuery() 

Option name Type Description
key object
  • The key of the query string
[fallback] string
  • The locale to fallback

Parses the query string from the url and extracts the locale

getFromQuery() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromQuery.name}`);

    this.locale = this.header
      .getFromQuery
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromDomain

method
 Header.prototype.getFromDomain() 

Option name Type Description
[fallback] string
  • The locale to fallback

Parses the domain from the url and extracts the locale

getFromDomain() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromDomain.name}`);

    this.locale = this.header
      .getFromDomain
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromSubDomain

method
 Header.prototype.getFromSubDomain() 

Option name Type Description
[fallback] string
  • The locale to fallback

Parses the sub-domain from the url and extracts the locale

getFromSubDomain() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromSubDomain.name}`);

    this.locale = this.header
      .getFromSubDomain
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromCookie

method
 Header.prototype.getFromCookie() 

Option name Type Description
key object
  • The key of the query string
[fallback] string
  • The locale to fallback

Parses the cookie from the request object and extracts the locale

getFromCookie() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromCookie.name}`);

    this.locale = this.header
      .getFromCookie
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

getFromUrl

method
 Header.prototype.getFromUrl() 

Option name Type Description
[fallback] string
  • The locale to fallback

Parses the url and extracts the locale

getFromUrl() {
    log.debug(`class: ${Header.name}`,
      `process: ${this.getFromUrl.name}`);

    this.locale = this.header
      .getFromUrl
      .apply(this.header, arguments);

    log.info(`locale: ${this.locale}`);
    return this.locale;
  }

detectLocale

method
 Header.prototype.detectLocale() 

Option name Type Description
[locale] string
  • The locale to override

Detects the locale by using the 'detect' options.

detectLocale() {
  log.debug(`class: ${Header.name}`,
    `process: ${this.detectLocale.name}`);

  this.locale = this.header
    .detectLocale
    .apply(this.header, arguments);

  log.info(`locale: ${this.locale}`);
  return this.locale;
}

}

export default () => {
'use strict';
return {
  main: function(req) {
    var options = this.options.header;
    this.header = new Header(req, options);
  },
  package: _.merge({
    type: 'header'
  }, require('../package')),
  defaults: require('../defaults')
};
};