0.1.2 • Published 4 years ago

localized-http-messages v0.1.2

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

localized-http-messages

A simple package giving you translated http messages.

npm bundle size Coverage Status dependencies Status

devDependencies Status Maintainability

Known locales

  • en (english)
  • fr (français)

You can send me translations in your locale if you want it in the module!

Basic usage

const translator = require('localized-http-messages')

let tr = new translator('fr') // or a JS Object with translations
console.log( tr.getMessage(418) )
// > Je suis une théière

API

Constructor(String|JS Object)

The parameter is the locale to set. If no locale is set, it will fallback to en.

The behavior is the same than calling setLocale method.

setLocale(String|JS Object)

Set the current locale.

If the parameter is a String, the module will try to find it in the list of known locale. If the locale is not known, it is ignored.

If the parameter is a JS Object, the module will load the object as the current locale. The object must be formatted like this:

{
    code: "Message",
    code: "Message",
    etc.
}

Example:

{
    404: "Custom message when not found",
    418: "Custom message for teapot"
}

getMessage(Number|String)

Return the String of the code given in paramter.

If the code is not found in the current locale, it falls back to the previous locale set. If the code is not found in the previous locale, it falls back to en.

If the code is not found in any of the locales, it return the code as a String.

Example:

const translator = require('localized-http-messages')

let tr = new translator('fr')
tr.setLocale({
    404: 'Not found, sorry!'
})
console.log( tr.getMessage(404) )
// > Not found, sorry!
console.log( tr.getMessage(418) )
// > Je suis une théière
console.log( tr.getMessage(999) )
// > 999

List of known codes

CodeEnglish translation
100Continue
101Switching Protocols
102Processing
103Early Hints
200OK
201Created
202Accepted
203Non-Authoritative Information
204No Content
205Reset Content
206Partial Content
207Multi-Status
208Already Reported
210Content Different
226IM Used
300Multiple Choices
301Moved Permanently
302Found
303See Other
304Not Modified
305Use Proxy
306Switch Proxy
307Temporary Redirect
308Permanent Redirect
310Too many Redirects
400Bad Request
401Unauthorized
402Payment Required
403Forbidden
404Not Found
405Method Not Allowed
406Not Acceptable
407Proxy Authentication Required
408Request Timeout
409Conflict
410Gone
411Length Required
412Precondition Failed
413Request Entity Too Large
414Request-URI Too Long
415Unsupported Media Type
416Requested range unsatisfiable
417Expectation failed
418I'm a teapot
421Bad mapping / Misdirected Request
422Unprocessable entity
423Locked
424Method failure
425Unordered Collection
426Upgrade Required
428Precondition Required
429Too Many Requests
431Request Header Fields Too Large
449Retry With
450Blocked by Windows Parental Controls
451Unavailable For Legal Reasons
456Unrecoverable Error
444No Response
495SSL Certificate Error
496SSL Certificate Required
497HTTP Request Sent to HTTPS Port
498Token expired/invalid
499Client Closed Request
500Internal Server Error
501Not Implemented
502Bad Gateway
503Service Unavailable
504Gateway Timeout
505HTTP Version not supported
506Variant Also Negotiates
507Insufficient storage
508Loop detected
509Bandwidth Limit Exceeded
510Not extended
511Network authentication required
520Unknown Error
521Web Server Is Down
522Connection Timed Out
523Origin Is Unreachable
524A Timeout Occurred
525SSL Handshake Failed
526Invalid SSL Certificate
527Railgun Error

Sources

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago