我使用i18next库在我的express.js / blade.js模板上有这个代码index.coffee
express = require "express"
gzippo = require "gzippo"
assets = require "connect-assets"
jsPaths = require "connect-assets-jspaths"
#stylus = require "stylus"
blade = require "blade"
i18n = require "i18next"
http = require "http"
https = require "https"
fs = require "fs"
json = ""
#### Application initialization
# Create app instance.
app = express()
# Define Port
app.port = process.env.PORT or process.env.VMC_APP_PORT or process.env.VCAP_APP_PORT or 3000
# Config module exports has `setEnvironment` function that sets app settings depending on environment.
config = require "./config"
app.configure "production", "development", "testing", ->
config.setEnvironment app.settings.env
# i18next init
i18n.init
detectLngQS: "lang"
,ns: { namespaces: ['ns.common', 'ns.layout'], defaultNs: 'ns.common'}
,resSetPath: "./locales/__lng__/new.__ns__.json"
,ignoreRoutes: ["images/", "public/", "css/", "js/"]
#,locales:['de', 'en', 'fr', 'pt']
,extension:".json"
#,saveMissing: true
#,sendMissingTo: 'all'
,debug: true
#### View initialization
# Add Connect Assets.
app.use assets(build : true)
jsPaths assets, console.log
#app.use i18n.init
# Set the public folder as static assets.
app.use gzippo.staticGzip(process.cwd() + "/assets")
app.use gzippo.staticGzip(process.cwd() + "/public")
app.use express.favicon(process.cwd() + "/images/favicon.ico")
app.use express.logger('dev')
# Set the nowjs folder as static assets and locales for i18next
app.use gzippo.staticGzip(process.cwd() + "/nowjs")
app.use gzippo.staticGzip(process.cwd() + "/locales")
app.use gzippo.staticGzip(process.cwd() + "/data/topo")
app.use (req, res, next) ->
res.render '404',
status: 404, url: req.url
# Set Blade View Engine and tell Express where our views are stored
app.set "view engine", "blade"
app.set "views", process.cwd() + "/views"
try
app.set "chapters", require(process.cwd() + "/data/chapters.json")
app.set "languages", require(process.cwd() + "/locales/config.json")
app.set "translation", require(process.cwd() + "/locales/dev/translation.json")
catch e
console.warn "files not found: " + e
app.set "chapters", []
app.set "languages", []
app.set "translation", []
next()
return
# [Body parser middleware](http://www.senchalabs.org/connect/middleware-bodyParser.html) parses JSON or XML bodies into `req.body` object
app.use express.bodyParser()
app.use i18n.handle
app.use blade.middleware(process.cwd() + "/views")
app.use app.router
#### Finalization
# Register i18next AppHelper so we can use the translate function in template
i18n.registerAppHelper(app)
# Initialize routes
routes = require "./routes"
app.locals.pretty=true
routes(app)
# Export application object
module.exports = app
我的head.blade模板就像:
header
.navbar.navbar-inverse.navbar-fixed-top
.navbar-inner
button.btn.btn-navbar.collapsed(type="button" data-toggle="collapse" data-target="#navbar-nav-collapse")
span.icon-bar
span.icon-bar
span.icon-bar
a(href="/" class="brand")
img(src="/images/tzm-logo-32.png")
//span(class="tzm-i18n")=t("tzm")
#navbar-nav-collapse.nav-collapse.collapse
ul.nav
- var menu = locals.settings.translation.menu
- for(var i in menu)
- if (i === 'projects')
// FIXME class active does not toggle
li
a(href=""+i class=""+i data-i18n="menu."+i)!=t("ns.layout:menu."+i)
我得到的错误是当我加载网站时:
☺ cake dev
Watching coffee files
Watching js files and running server
DEBUG: Running node-supervisor with
DEBUG: program 'server'
DEBUG: --watch '.app,views'
DEBUG: --ignore 'undefined'
DEBUG: --extensions 'js|blade'
DEBUG: --exec 'node'
DEBUG: Starting child process with 'node server'
DEBUG: Watching directory '/Users/khinester/Documents/Tutorials/Node/Blade/.app' for changes.
DEBUG: Watching directory '/Users/khinester/Documents/Tutorials/Node/Blade/views' for changes.
02:17:59 - compiled src/routes.coffee
02:17:59 - compiled src/index.coffee
02:17:59 - compiled src/social.coffee
02:17:59 - compiled src/config/index.coffee
02:17:59 - compiled src/controllers/guide.coffee
02:17:59 - compiled src/controllers/index.coffee
02:17:59 - compiled src/controllers/map.coffee
DEBUG: crashing child
DEBUG: Starting child process with 'node server'
set app environment: development
currentLng set to: dev
Assetizing map
Assetizing utils
SockJS v0.3.1 bound to "/_nowjs"
Server running at http://127.0.0.1: 9080
Press CTRL-C to stop server.
loaded file: locales/dev/ns.common.json
loaded file: locales/dev/ns.layout.json
ReferenceError: t is not defined
at /Users/khinester/Documents/Tutorials/Node/Blade/views/header.blade:18:33
我的错误发生在t("ns.layout:menu."+i)
我正在初始化i18next,但无法看到为什么在这个index.coffee中它无效!
任何建议非常感谢
答案 0 :(得分:2)
这不是因为i18next,代码中缺少t。当你这样做
- var foo = 'bar'
= foo
h1= foo
你得到了
bar<h1>bar</h1>
所以
a(href=""+i class=""+i data-i18n="menu."+i)!=t("ns.layout:menu."+i)
转换为
a<href=""+i class=""+i data-i18n="menu."+i> ##Content from t##</a>
但是你的header.blade中没有定义t。所以它显示了错误。
答案 1 :(得分:0)
似乎订单设置不正确,好像我在app.use i18n.handle
之后放置了i18next t函数然后可用于快递应用程序。
app.use (req, res, next) ->
#the status option, or res.statusCode = 404
#are equivalent, however with the option we
#get the "status" local available as well
res.render '404',
status: 404, url: req.url