-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.js
83 lines (69 loc) · 3.32 KB
/
static.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var express = require('express')
var router = express.Router()
var session = require('express-session') // to generate session object
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser') // to get body inputs variables
/*###############################################################################################################################
- INTL POLYFILLING and IMPLEMENTATION (Internationalisation API) -
NOTE: In Node.js [Intl](https://github.com/nodejs/node/wiki/Intl) support is not enabled by default.
Also Intl ICU files should be prepared and added in different ways according to the OS platform.
Beacuse of that, Intl module is used. It provides a run-time and cross-platform solution.
*********************************************************/
var areIntlLocalesSupported = require('intl-locales-supported')
/* DEFINING GLOBAL VARIABLES */
global.appLocales = ["en-US", "tr-TR" /* , YOUR_LANG */];
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(appLocales)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and replace the constructors with need with the polyfill's.
var IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
}
/**
* MessageFormat provides to show conditional message with a given parameter
* https://github.com/yahoo/intl-messageformat#how-it-works
* It takes three parameters:
* @param Message {String | AST} String message (or pre-parsed AST) that serves as formatting pattern.
* @param Locales {String | String[]} A string with a BCP 47 language tag, or an array of such strings
* @param Formats {Object} Optional object with user defined options for format styles.
* @return {String}
* @public
* MessageFormat(message, locales, [formats])
********************/
global.Intl.MessageFormat = require('intl-messageformat');
global.Intl.RelativeFormat = require('intl-relativeformat');
// HERE IS STATIC.JS - ADDING ONLY STATIC TRANSLATIONS
global.Intl.Translations = require('./intl-polyfill').static
// ADDING COMMON TRANSLATIONS - IF YOU SHARE SOME COMMON FILES WITH OTHER APPS
global.Intl.Translations.common = require('./intl-polyfill').common
/*###############################################################################################################################*/
// CREATING AND SETTING THE EXPRESS APP
var static = express()
static.set('view engine', 'ejs');
static.set('views', __dirname + '/views/static');
static.use(bodyParser.json({limit: '5mb'})) // to provide JSON-encoded body
static.use(bodyParser.urlencoded({ extended: true, limit: '5mb' })) // to provide URL-encoded body
static.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
// ADDING LOCALE INFO TO THE REQUEST OBJECT
app.use(function (req,res,next) {
// Choosing 'en-US' as default
req.locale = req.acceptsLanguages(appLocales) || 'en-US'
next()
})
// ADDING ROUTES
static.use(require('./routes/staticDemo'))
// STARTING TO LISTEN
static.listen(4000, function() {
console.log('static is listening HTTP port 4000')
})