2016-05-10 27 views
2

我不確定i18n是否適合此選項。因爲看起來這個模塊只關注於改變語言屬性(在視圖渲染器內)而不是視圖本身。我希望我解釋自己足夠好:)i18n根據所選語言呈現特定視圖

所以這個想法是,當一個人改變語言渲染從語言目錄中選擇視圖(具有相同的文件名)。

你可以有例如這樣的目錄結構:

-views 
--EN 
---index.jade 
---contact.jade 
--NL 
---index.jade 
---contact.jade 
--form-contact.jade 

要明確的是,'form-contact.jade'文件將包含的形式(包含在'contact.jade'文件,以便'form-contact.jade'的確會利用國際化的別名對象(這將駐留在en.json或nl.json文件)

,從而爲我現在只有具備以下條件:。

app.js

i18n.configure({ 
    locales: ['en', 'nl'], 
    directory: __dirname + '/locales', 
    defaultLocale: 'en', 
    extension: '.json', 
    register: global 
}); 

app.use(i18n.init); 

app.use(function (req, res, next) { 
    console.log(i18n); 
    if (req.query.lang != undefined && i18n.locales.indexOf(req.query.lang) >= 0) { 
    i18n.setLocale(req.query.lang); 
    }else{ 
    i18n.setLocale(i18n.defaultLocale); 
    } 
    next(); 
}); 

現在,我怎麼了res.render選擇正確的看法?

router.get('/contact', function(req, res, next) { 
    res.render('/contact', { title: '...', menu: '...' }); 
}); 

編輯:

如果你們有翻譯大靜態頁面(全HTML/CSS)的建議,請給你的意見:)

+0

爲什麼需要每種語言有不同的看法? – idbehold

+0

由於大多數頁面都是靜態的。我首先想到了將它放入json語言文件中,但它會變得混亂而漫長。 – Goowik

+0

第一步將客戶端設備語言設置爲所需的語言 – Pomagranite

回答

0

你的方式是錯誤的。 如果您對每種語言都有看法,那麼使用i18n有什麼意義?

你可以得到

?lang= 

值和發送res.render ('/' + req.query.lang + '/contact.html').

嘗試的方法是

-locales 
--en.json 
--nl.json 
-view 
--cabinet.html 
--main.html 

app.js

i18n.configure({ 
    locales: ['en', 'nl'], 
    directory: __dirname + '/locales', 
    defaultLocale: 'nl', 
    updateFiles: false, 
    objectNotation: true 
}); 

app.use(i18n.init); 

app.use(function (req, res, next) { 
    console.log(i18n); 
    if (req.query.lang != undefined && i18n.locales.indexOf(req.query.lang) >= 0) { 
    i18n.setLocale(req.query.lang); 
    }else{ 
    i18n.setLocale(i18n.defaultLocale); 
    } 
    next(); 
}); 

en.json例如

{ 
    "cabinet": { 
     "menu": { 
       "title": "Menu", 
     }, 
     "dashboard": { 
      "title": "Dashboard" 
     }, 
     "statistic": { 
      "datePeriod": "Date period", 
      "choose": "Choose", 
     } 
    }, 
    "main": { 
     "title": "Main", 
     "body": { 
        "hi": "Hi everyone!" 
     } 
    } 

相同爲nl.json

main.html中

 I use Swig template 
<html> 
<head> 
     <title>{{ translate.title }}</title> 
</head> 
<body> 
     {{ translate.body.hi }}!! 
</body> 
</html> 

路由器。JS

router.get('/main', function(req, res, next) { 
    res.render('/main', { title: '...', menu: '...', translate: res.__('main') }); 
}); 

router.get('/cabinet', function(req, res, next) { 
    res.render('/cabinet', { title: '...', menu: '...', translate: res.__('cabinet') }); 
}); 
+0

我明白,但我遇到的問題是:有8個靜態頁面,大約有150到200行代碼(其中5%是標記,95%翻譯文本)。如果我把所有這8頁的文本放到lang.json文件中,這將會變得很大而且很混亂。 – Goowik

+1

爲什麼混亂?只需創建舒適的對象並使用點符號或移除i18n並渲染** res.render('/'+ req.query.lang +'/contact.html'). – cheks

+1

我想補充一點,使用語言查詢參數是** NOT **無論如何推薦的方式做這個時考慮SEO。在這裏看看這個很好的答案:http://webmasters.stackexchange.com/questions/403/how-should-i-structure-my-urls-for-both-seo-and-localization/44289#44289 –

0

只需使用像提到或@cheks,更好單獨的頁面,就像Angular前端框架,你可以去:

<div if-language="en">my english text</div> <div if-language="fr">mon texte français</div>