Permalink
Browse files

Reorganize code using /src and /lib

  • Loading branch information...
1 parent 0331c56 commit 317b622872c4dd3355b6938ee238dea42d0d9f5c @ricardobeat committed Feb 16, 2013
Showing with 96 additions and 51 deletions.
  1. +5 −2 .npmignore
  2. +10 −0 Cakefile
  3. +1 −1 example/app.coffee
  4. +1 −1 index.js
  5. +11 −23 { → lib}/i18n.js
  6. +31 −0 lib/util.js
  7. +10 −9 package.json
  8. +12 −14 { → src}/i18n.coffee
  9. +14 −0 src/util.coffee
  10. +1 −1 test/i18n.spec.coffee
View
@@ -1,4 +1,7 @@
-*.coffee
-README.md
+node_modules
+.DS_Store
+
+src
+Cakefile
example
test
View
@@ -0,0 +1,10 @@
+flour = require 'flour'
+
+flour.minifiers.js = null
+
+task 'build:coffee', ->
+ compile 'src/*.coffee', 'lib/*'
+
+task 'watch', ->
+ invoke 'build:coffee'
+ watch 'src/', -> invoke 'build:coffee'
View
@@ -1,7 +1,7 @@
http = require 'http'
express = require 'express'
hbs = require 'hbs'
-i18n = require '../i18n'
+i18n = require '../'
# Application settings
# ---------------------
View
@@ -1 +1 @@
-module.exports = require('./i18n')
+module.exports = require('./lib/i18n')
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,11 +1,12 @@
{
- "name" : "polyglot"
- , "description" : "i18n module for express"
- , "keywords" : ["i18n", "L10n", "G11n","internationalization", "globalization", "localization", "templates"]
- , "author" : "Ricardo Tomasi <ricardobeat@gmail.com> (http://ricardo.cc/)"
- , "repository" : { "type": "git", "url": "git://github.com/ricardobeat/polyglot.git" }
- , "version" : "0.5.0"
- , "engines" : { "node": ">0.6" }
- , "dependencies" : { }
- , "scripts" : { "test": "mocha" }
+ "name" : "polyglot"
+ , "description" : "i18n module for express"
+ , "keywords" : ["i18n", "L10n", "G11n","internationalization", "globalization", "localization", "templates"]
+ , "author" : "Ricardo Tomasi <ricardobeat@gmail.com> (http://ricardo.cc/)"
+ , "repository" : { "type": "git", "url": "git://github.com/ricardobeat/polyglot.git" }
+ , "version" : "0.5.0"
+ , "engines" : { "node": ">0.6" }
+ , "dependencies" : { }
+ , "devDependencies" : { "flour": "*", "coffee-script": "*" }
+ , "scripts" : { "test": "mocha" }
}
@@ -1,33 +1,33 @@
path = require 'path'
fs = require 'fs'
-# Debug helper, enable with option `debug: true` when calling `i18n()`.
+_ = require './util'
+
+# Log messages, enable with option `debug: true`.
debug = (str) ->
i18n.options.debug && console.log "[i18n] #{str}"
-# Main method
+# Polyglot
# ------------
# Sets options, loads language files and returns an express middleware function.
-i18n = (opts) ->
+i18n = (options) ->
# Options
- options = i18n.options =
+ i18n.options = _.extend {
default: 'en'
path: '/lang'
debug: false
-
- for own key, val of opts
- options[key] = val
+ }, options
- i18n.languages.push options.default
+ i18n.languages.push i18n.options.default
i18n.loadLanguageFiles()
return (req, res, next) ->
# User doesn't have a language setting yet
unless req.session?.lang?
- locale = i18n.getLocale req, options
+ locale = i18n.getLocale req
req.session.locale = locale
req.session.lang = locale[0..2]
debug "Language set to #{req.session.lang}"
@@ -73,7 +73,7 @@ i18n.loadLanguageFiles = ->
if fs.existsSync(process.cwd() + dir)
files = fs.readdirSync(process.cwd() + dir)
.map((f) -> path.basename f, '.json')
- .filter i18n.isValidLocale
+ .filter _.isValidLocale
for locale in files when locale isnt i18n.options.default
filePath = path.join process.cwd(), dir, locale + '.json'
@@ -102,10 +102,8 @@ i18n.isValidLocale = (locale) ->
i18n.getLocale = (req) ->
languages = []
- acceptHeader = req.header('Accept-Language')
- if acceptHeader then languages = acceptHeader.split(/,|;/g).filter i18n.isValidLocale
-
- debug "Accepted languages: "+languages.join(', ')
+ if acceptHeader = req.header('Accept-Language')
+ languages = acceptHeader.split(/,|;/g).filter i18n.isValidLocale
if languages.length < 1
languages.push i18n.options.default
View
@@ -0,0 +1,14 @@
+module.exports =
+
+ extend: (obj, sources...) ->
+ for source in sources
+ obj[key] = val for key, val of source
+ return obj
+
+ parseJSON: (obj) ->
+ try res = JSON.parse obj.toString()
+ catch e then res = {}
+ return res
+
+ isValidLocale: (locale) ->
+ return /^\w\w(-\w\w)?$/.test(locale)
View
@@ -1,5 +1,5 @@
assert = require 'assert'
-polyglot = require '../i18n.coffee'
+polyglot = require '../'
describe 'Parse headers', ->

0 comments on commit 317b622

Please sign in to comment.