Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #13 from stuartf/broadMatch
Language without country doesn't match language with country
  • Loading branch information
stuartf committed Oct 31, 2013
2 parents ec8049d + 7f0126c commit bdc3768
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/index.coffee
Expand Up @@ -70,8 +70,10 @@ class app.Locales
for item in @
if index[item]
return item
else if index[item.language] then locale = new Locale item.language
else
if index[item.language] then locale = new Locale item.language
for l in locales
if l.language == item.language then return l

locale

Expand Down
23 changes: 20 additions & 3 deletions src/test.coffee
Expand Up @@ -9,10 +9,11 @@ defaultLocale = locale.Locale.default
before (callback) ->
app = do express

app.use locale ["en-US", "en", "ja"]
app.use locale ["en-US", "en", "ja", "da-DK"]
app.get "/", (req, res) ->
res.header "content-language", req.locale
do res.end
res.set "content-language", req.locale
res.set "Connection", "close"
res.send(200)
server = app.listen 8000, callback

describe "Defaults", ->
Expand Down Expand Up @@ -51,6 +52,22 @@ describe "Priority", ->
)
callback()

it "should use a country specific language when an unsupported general language is requested", (callback) ->
http.get port: 8000, headers: "Accept-Language": "da", (res) ->
assert.equal(
res.headers["content-language"]
"da_DK"
)
callback()

it "should fallback to a country specific language even when there's a lower quality exact match", (callback) ->
http.get port: 8000, headers: "Accept-Language": "ja;q=.8, da", (res) ->
assert.equal(
res.headers["content-language"]
"da_DK"
)
callback()

after ->
do server.close
do process.exit 0

0 comments on commit bdc3768

Please sign in to comment.