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

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 に答える