ReferenceError:t没有定义

我有我的express.js / blade.js模板使用i18next库这个代码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它不工作!

任何意见非常赞赏

这不是因为i18next,你的代码中缺lesst。 当你这样做

 - 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> 

但是t没有在你的header.blade中定义。 所以它显示错误。

似乎命令没有正确设置,就好像我把它app.use i18n.handle后面,然后i18nxt函数就可以用于快速应用了。

 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