소스 검색

Merge pull request #12 from gablau/master

Italian translation + general improvement
Andi Dittrich 4 년 전
부모
커밋
8c786ec724
11개의 변경된 파일201개의 추가작업 그리고 21개의 파일을 삭제
  1. 6 2
      assets/template.ejs
  2. 35 5
      bin/generator.js
  3. 12 2
      config-dist.json
  4. 12 2
      config.json
  5. 3 1
      examples/express.js
  6. 3 1
      examples/koa.js
  7. 4 3
      gulpfile.js
  8. 79 0
      i18n/pages-it_IT.json
  9. 20 2
      lib/express.js
  10. 7 1
      lib/json-reader.js
  11. 20 2
      lib/koa.js

+ 6 - 2
assets/template.ejs

@@ -1,13 +1,17 @@
 <!DOCTYPE html>
-<html lang="en">
+<html lang="<%= lang %>">
 <head>
     <!-- Simple HttpErrorPages | MIT License | https://github.com/HttpErrorPages -->
     <meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1" />
+<% if (locals.page_title) { %>
+    <title><%= page_title %></title>
+<% } else { %>
     <title>We've got some trouble | <%= code %> - <%= title %></title>
+<% } %>
     <style type="text/css"><%- inlinecss %></style>
 </head>
 <body>
-    <div class="cover"><h1><%= title %> <small>Error <%= code %></small></h1><p class="lead"><%= message %></p></div>
+    <div class="cover"><h1><%= title %> <small><%= error %></small></h1><p class="lead"><%= message %></p></div>
     <% if (footer){ %><footer><p><%- footer %></p></footer><% } %>
 </body>
 </html>

+ 35 - 5
bin/generator.js

@@ -26,25 +26,54 @@ async function generator(configFilename, pageDefinitionFile, distPath){
     // load css
     const css = await _fs.readFile(cssPath, 'utf8');
 
+    const lang = pageDefinitionFile.substr(-10, 2);
+
+    console.log('Create/Empty distribution folder: "'+distPath+'"\n');
+
+    if(await _fs.exists(distPath)) {
+        await _fs.rmrf(distPath);
+    }
+    await _fs.mkdirp(distPath);
+
     console.log('Generating static pages');
 
     // for each errorcode generate custom page
-    await Promise.all(Object.keys(pages).map(async function(p){
+    await Promise.all(Object.keys(pages).map(async function(code){
         // page config
-        const pconf = pages[p];
+        const pconf = pages[code];
 
         // inject errorcode
-        pconf.code = p;
+        pconf.code = code;
 
-        // inject foote
+        // inject lang
+        pconf.lang = lang || 'en';
+
+        // inject page title
+        if(config && config.page_title) {
+            pconf.page_title = config.page_title.replace('%code%', code);
+            pconf.page_title = pconf.page_title.replace('%title%', pconf.title);
+        }
+
+        // inject error
+        pconf.error = 'Error '+ pconf.code 
+        if(config && config.error) {
+            pconf.error = config.error.replace('%code%', code);
+        }
+
+        // inject footer
         pconf.footer = pconf.footer || config.footer;
 
         // render page
         const content = await _pageRenderer(tpl, css, pconf);
 
         // generate filename
-        const filename = 'HTTP' + p + '.html';
+        let filename = 'HTTP' + code + '.html';
 
+        // check filename schema
+        if(config && config.scheme && config.scheme.indexOf("%code%") !== -1) {
+            filename =  config.scheme.replace('%code%', code);
+        }
+        
         // write content to file
         await _fs.writeFile(_path.join(distPath, filename), content, 'utf8');
 
@@ -105,6 +134,7 @@ _cli
             })
             .catch(function(e){
                 console.error(e);
+                console.log('\nStatic files not generated\n');
             });
     });
 

+ 12 - 2
config-dist.json

@@ -1,6 +1,16 @@
 {
-    //  Output Filename Scheme - eg. HTTP500.html
-    "scheme": "HTTP%d.html",
+    // Output Filename Scheme - eg. HTTP500.html
+    // %code% - HTTP error code eg. 400
+    "scheme": "HTTP%code%.html",
+
+    // Page title (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    // %title% - HTTP error eg. Bad Request
+    "page_title": "We've got some trouble | %code% - %title%",
+
+    // Error (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    "error": "Error %code%",
 
     // Footer content (HTML Allowed)
     "footer": null

+ 12 - 2
config.json

@@ -1,6 +1,16 @@
 {
-    //  Output Filename Scheme - eg. HTTP500.html
-    "scheme": "HTTP%d.html",
+    // Output Filename Scheme - eg. HTTP500.html
+    // %code% - HTTP error code eg. 400
+    "scheme": "HTTP%code%.html",
+
+    // Page title (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    // %title% - HTTP error eg. Bad Request
+    "page_title": "We've got some trouble | %code% - %title%",
+
+    // Error (HTML not allowed)
+    // %code% - HTTP error code eg. 400
+    "error": "Error %code%",
 
     // Footer content (HTML Allowed)
     "footer": "Tech Contact <a href=\"mailto:info@example.org\">info@example.org</a>"

+ 3 - 1
examples/express.js

@@ -26,7 +26,9 @@ async function bootstrap(){
     // because of the asynchronous file-loaders, wait until it has been executed
     await _httpErrorPages.express(_webapp, {
         lang: 'en_US',
-        footer: 'Hello <strong>World</strong>'
+        footer: 'Hello <strong>World</strong>',
+        error: 'Error %code%',
+        page_title: "We've got some trouble | %code% - %title%",
     });
 
     // start service

+ 3 - 1
examples/koa.js

@@ -10,7 +10,9 @@ async function bootstrap(){
     // because of the asynchronous file-loaders, wait until it has been executed - it returns an async handler
     _webapp.use(await _httpErrorPages.koa({
         lang: 'en_US',
-        footer: 'Hello <strong>World</strong>'
+        footer: 'Hello <strong>World</strong>',
+        error: 'Error %code%',
+        page_title: "We've got some trouble | %code% - %title%",
     }));
 
     // demo handler

+ 4 - 3
gulpfile.js

@@ -13,6 +13,7 @@ _gulp.task('sass', function (){
         .pipe(_concat('layout.css'))
 
         .pipe(_gulp.dest('./assets'));
-});
- 
-_gulp.task('default', _gulp.series('sass'));
+}); 
+
+_gulp.task('default', _gulp.series(['sass']));
+ 

+ 79 - 0
i18n/pages-it_IT.json

@@ -0,0 +1,79 @@
+{        
+    // STANDARD ERROR CODES
+    // =======================================================
+
+    //http 400 bad request
+    "400": {
+        "title": "Richiesta non valida",
+        "message": "Il server non può elaborare la richiesta a causa di qualcosa che è percepito come un errore del client." 
+    },
+
+    //http 401 unauthorized
+    "401": {
+        "title": "Accesso negato",
+        "message": "La risorsa richiesta richiede un'autenticazione."
+    },
+
+    // http 403 access denied
+    "403": {
+        "title": "Operazione non consentita",
+        "message": "La risorsa richiesta richiede un'autenticazione." 
+    },
+    
+    // http 404 not found
+    "404": {
+        "title": "Risorsa non trovata",
+        "message": "La risorsa richiesta non è stata trovata ma potrebbe essere nuovamente disponibile in futuro." 
+    },
+    
+    // internal server error
+    "500": {
+        "title": "Web Server attualmente non disponibile",
+        "message": "Si è verificata una condizione imprevista.\nIl nostro team di assistenza è stato inviato per riportarlo online." 
+    },
+    
+    // unknown http method
+    "501": {
+        "title": "Non implementato",
+        "message": "Il server Web non è in grado di riconoscere il metodo della richiesta."
+    },
+    
+    // webserver - bad gateway
+    "502": {
+        "title": "Web Server attualmente non disponibile - Gateway non valido",
+        "message": "Abbiamo qualche problema con il nostro cluster back-end.\nIl nostro team di assistenza è stato inviato per riportarlo online."
+    },
+    
+    // webserver - service unavailable
+    "503": {
+        "title": "Web Server attualmente non disponibile",
+        "message": "Abbiamo qualche problema con il nostro cluster back-end.\nIl nostro team di assistenza è stato inviato per riportarlo online."
+    },
+
+    // webserver - gateway timeout
+    "504": {
+        "title": "Web Server attualmente non disponibile - Timeout del gateway",
+        "message": "Abbiamo qualche problema con il nostro cluster back-end.\nIl nostro team di assistenza è stato inviato per riportarlo online."
+    },
+    
+    // CUSTOM ERROR CODES
+    // =======================================================
+
+    // webserver origin error
+    "520": {
+        "title": "Errore di origine - Host sconosciuto",
+        "message": "Il nome host richiesto non viene instradato. Utilizzare solo nomi host per accedere alle risorse."
+    },
+    
+    // webserver down error
+    "521": {
+        "title": "Web Server attualmente non disponibile",
+        "message": "Abbiamo qualche problema con il nostro cluster back-end.\nIl nostro team di assistenza è stato inviato per riportarlo online."
+    },
+    
+    // maintenance
+    "533": {
+        "title": "Manutenzione programmata",
+        "message": "Questo sito è attualmente fuori servizio per manutenzione.\nIl nostro team di assistenza sta lavorando sodo per riportarlo presto online."                
+    }
+}

+ 20 - 2
lib/express.js

@@ -11,7 +11,9 @@ async function createDispatcher(options={}){
         template: options.template || _path.join(__dirname, '../assets/template.ejs'),
         css: options.css || _path.join(__dirname, '../assets/layout.css'),
         lang: options.lang || 'en_US',
-        footer: options.footer || null
+        footer: options.footer || null,
+        error: options.error || null,
+        page_title: options.page_title || null
     };
 
     // load page template
@@ -39,6 +41,19 @@ async function createDispatcher(options={}){
         // extract page date
         const pageData = pages[page];
 
+        // set page title
+        let page_title = undefined;
+        if(opt && opt.page_title) {
+            page_title = opt.page_title.replace('%code%', page);
+            page_title = page_title.replace('%title%', pageData.title);
+        }
+
+        // set error
+        let error = 'Error '+ page 
+        if(opt && opt.error) {
+            error = opt.error.replace('%code%', page);
+        }
+
         // multiple response formats
         res.format({
             // standard http-error-pages
@@ -48,7 +63,10 @@ async function createDispatcher(options={}){
                     code: httpStatusCode,
                     title: pageData.title,
                     message: pageData.message,
-                    footer: opt.footer
+                    footer: opt.footer,
+                    lang: opt.lang.substr(0, 2),
+                    error: error,
+                    page_title: page_title
                 }))
             },
 

+ 7 - 1
lib/json-reader.js

@@ -9,7 +9,13 @@ async function readJSONFile(filename){
     raw = raw.replace(/^\s*\/\/.*$/gm, '');
 
     // parse text
-    return JSON.parse(raw);
+    try {
+        return JSON.parse(raw);
+    } catch(e) {
+        console.log("Error parsing JSON file: "+filename+"\n");
+        throw e;
+    }
+    //return JSON.parse(raw);
 }
 
 module.exports = readJSONFile;

+ 20 - 2
lib/koa.js

@@ -11,7 +11,9 @@ async function createDispatcher(options={}){
         template: options.template || _path.join(__dirname, '../assets/template.ejs'),
         css: options.css || _path.join(__dirname, '../assets/layout.css'),
         lang: options.lang || 'en_US',
-        footer: options.footer || null
+        footer: options.footer || null,
+        error: options.error || null,
+        page_title: options.page_title || null
     };
 
     // load page template
@@ -39,6 +41,19 @@ async function createDispatcher(options={}){
         // extract page date
         const pageData = pages[page];
 
+        // set page title
+        let page_title = undefined;
+        if(opt && opt.page_title) {
+            page_title = opt.page_title.replace('%code%', page);
+            page_title = page_title.replace('%title%', pageData.title);
+        }
+
+        // set error
+        let error = 'Error '+ page 
+        if(opt && opt.error) {
+            error = opt.error.replace('%code%', page);
+        }
+
         // multiple response formats
         switch (ctx.accepts('json', 'html', 'text')){
 
@@ -57,7 +72,10 @@ async function createDispatcher(options={}){
                     code: httpStatusCode,
                     title: pageData.title,
                     message: pageData.message,
-                    footer: opt.footer
+                    footer: opt.footer,
+                    lang: opt.lang.substr(0, 2),
+                    error: error,
+                    page_title: page_title
                 });
                 break;