2

我使用 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 中它不起作用!

非常感谢任何建议

4

2 回答 2

2

It is not because of i18next, t is missing in your code. When you do

- var foo = 'bar'
= foo
h1= foo

You get

bar<h1>bar</h1>

So

a(href=""+i class=""+i data-i18n="menu."+i)!=t("ns.layout:menu."+i)

translates to

a<href=""+i class=""+i data-i18n="menu."+i> ##Content from t##</a>

But t is not defined in your header.blade. So it shows that error.

于 2013-05-05T05:58:24.787 回答
0

似乎顺序设置不正确,好像我在app.use i18n.handlei18next 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
于 2013-05-05T14:24:02.547 回答