Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add locale(s) support #39

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 96 additions & 23 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = (opts) ->
@roots.config.locals.asset = asset_view_helper

setup: ->
configure_content(opts.content_types).with(@)
configure_content(opts).with(@)
.then(get_all_content)
.tap(set_urls)
.then(transform_entries)
Expand All @@ -56,18 +56,70 @@ module.exports = (opts) ->
* @return {Promise} - returns an array of configured content types
###

configure_content = (types) ->
if _.isPlainObject(types) then types = reconfigure_alt_type_config(types)
W.map types, (t) ->
if not t.id then return W.reject(errors.no_type_id)
t.filters ?= {}
if (not t.name || (t.template && not t.path))
return W client.contentType(t.id).then (res) ->
t.name ?= pluralize(S(res.name).toLowerCase().underscore().s)
if t.template
t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}"
return t
return W.resolve(t)
configure_content = (opts) ->
types = opts.content_types
locales = opts.locale
lPrefixes = opts.locales_prefix

isWildcard = -> # if locales is wildcard `*`, fetch & set locales
return W(
if locales is "*"
fetch_all_locales().then (res) ->
locales = res
W.resolve locales
else
W.resolve
)

reconfigObj = ->
types = reconfigure_alt_type_config(types) if _.isPlainObject(types)

localesArray = ->
if _.isArray(locales) # duplicate & update type to contain locale's data
for locale in locales
for t in types
unless t.locale? # type's locale overrides global locale
tmp = _.clone(t, true) # create clone
tmp.locale = locale
tmp.prefix = lPrefixes?[locale] ? "#{locale.replace(/-/,'_')}_"
types.push tmp # add to types
else
# set prefix, only if it isn't set
t.prefix ?= lPrefixes?[locale] ? "#{locale.replace(/-/,'_')}_"

types = _.remove types, (t) -> t.locale? # remove dupes w/o locale
else
if _.isString opts.locale
global_locale = true

isWildcard()
.then reconfigObj
.then localesArray
.then ->
W.map types, (t) ->
if not t.id then return W.reject(errors.no_type_id)
t.filters ?= {}

if (not t.name || (t.template && not t.path))
return W client.contentType(t.id).then (res) ->
t.name ?= pluralize(S(res.name).toLowerCase().underscore().s)

unless _.isUndefined lPrefixes
t.name = t.prefix + t.name

if t.template or lPrefixes?
t.path ?= (e) ->
"#{t.name}/#{S(e[res.displayField]).slugify().s}"

return t

unless _.isUndefined lPrefixes
t.name = t.prefix + t.name

if global_locale? then t.locale or= opts.locale

return W.resolve(t)


###*
* Reconfigures content types set in app.coffee using an object instead of
Expand Down Expand Up @@ -104,11 +156,30 @@ module.exports = (opts) ->

fetch_content = (type) ->
W(
client.entries(
_.merge(type.filters, content_type: type.id, include: 10)
client.entries(_.merge(
type.filters,
content_type: type.id,
include: 10,
locale: type.locale
)
)
)

###*
* Fetch all locales in space
* Used when `*` is used in opts.locales
* @return {Array} - returns array of locales
###

fetch_all_locales = ->
W(client.space()
.then (res) ->
locales = []
for locale in res.locales
locales.push locale.code
W.resolve locales
)

###*
* Formats raw response from Contentful
* @param {Object} content - entries API response for a content type
Expand All @@ -125,7 +196,7 @@ module.exports = (opts) ->

format_entry = (e) ->
if _.has(e.fields, 'sys') then return W.reject(errors.sys_conflict)
_.assign(_.omit(e, 'fields'), e.fields)
_.assign(_.omit(_.omit(e, 'sys'), 'fields'), e.fields)

###*
* Sets `_url` and `_urls` properties on content with single entry views
Expand All @@ -150,8 +221,10 @@ module.exports = (opts) ->
###

set_locals = (types) ->
W.map types, (t) =>
@roots.config.locals.contentful[t.name] = t.content
contentful = @roots.config.locals.contentful
W.map types, (t) ->
if contentful[t.name] then contentful[t.name].push t.content[0]
else contentful[t.name] = t.content

###*
* Transforms every type with content with the user provided callback
Expand All @@ -160,9 +233,9 @@ module.exports = (opts) ->
###

transform_entries = (types) ->
W.map types, (t) =>
W.map types, (t) ->
if t.transform
W.map t.content, (entry) =>
W.map t.content, (entry) ->
W(entry, t.transform)
W.resolve(t)

Expand All @@ -173,10 +246,10 @@ module.exports = (opts) ->
###

sort_entries = (types) ->
W.map types, (t) =>
W.map types, (t) ->
if t.sort
# Unfortunately, in order to sort promises we have to resolve them first.
W.all(t.content).then (data) =>
# In order to sort promises we have to resolve them first.
W.all(t.content).then (data) ->
t.content = data.sort(t.sort)
W.resolve(t)

Expand Down
26 changes: 26 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports =
contentful
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'xxxxxx'
locale: 'tlh'
locales_prefix:
tlh: 'klingon_'
content_types:
blog_posts:
id: 'xxxxxx'
Expand Down Expand Up @@ -104,6 +107,29 @@ Required. The space ID containing the content you wish to retrieve.

Optional. (Boolean) Allows you use the Contentful Preview API. Also able to be accessed by setting the environment variable `CONTENTFUL_ENV` to `"develop"` (preview api) or `"production"` (default cdn).

#### locales
Locales allow you to request your content in a different language, `tlh` is Klingon.

##### Global locale
Optional. (String or Array) Defines locale for all content_types.
String: `'tlh'`
Array: `['en-es', 'tlh']`
Wildcard: `'*'` - grabs all locales from contentful

##### content_type specific locale
Optional. (String) Define content_types locale, will override global locale. Add `locale: 'tlh'` to any of your content types and you'll retrieve that post in the specific locale.

#### locales_prefix
Optional. (Object) Defines the prefix given to a group of locales.

```
locales_prefix:
'tlh': 'klingon_'
'en-es': 'spanish_'
```

Lets say you have 3 global locales defined, `['tlh', 'en-us', 'en-es']`, and above is our defined locales_prefix. Since we did not declare `'en-us'` you can access it with `contentful.en_us_blog_posts`. Similarly you can access each preix according to what you've set in the object above. `'tlh'` would be accessible by `contentful.klingon_blog_posts` and `en-es` by `contentful.spanish_blog_posts`.

#### content_types

An object whose key-value pairs correspond to a Contentful Content Types. Each
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/locale_global/about.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 wow
17 changes: 17 additions & 0 deletions test/fixtures/locale_global/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: ['en-US']
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
locale: 'en-es'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/locale_global/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/locale_global/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
16 changes: 16 additions & 0 deletions test/fixtures/locale_multi/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: ['en-es', 'tlh']
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/locale_multi/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/locale_multi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
19 changes: 19 additions & 0 deletions test/fixtures/locale_prefix/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: ['en-es', 'tlh']
locales_prefix: {
'tlh': 'klingon_'
}
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/locale_prefix/klingon.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.klingon_blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/locale_prefix/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/locale_prefix/spanish.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.en_es_blog_posts
li
h1= p.title
p= p.body
1 change: 1 addition & 0 deletions test/fixtures/locale_scope/about.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 wow
17 changes: 17 additions & 0 deletions test/fixtures/locale_scope/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: ['tlh']
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
locale: 'en-es'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/locale_scope/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/locale_scope/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
1 change: 1 addition & 0 deletions test/fixtures/locale_setup/about.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 wow
16 changes: 16 additions & 0 deletions test/fixtures/locale_setup/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: '*'
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
}
]
)
]
5 changes: 5 additions & 0 deletions test/fixtures/locale_setup/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ul
- for p in contentful.blog_posts
li
h1= p.title
p= p.body
6 changes: 6 additions & 0 deletions test/fixtures/locale_setup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test",
"dependencies": {
"jade": "*"
}
}
1 change: 1 addition & 0 deletions test/fixtures/locale_single/about.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 wow
16 changes: 16 additions & 0 deletions test/fixtures/locale_single/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contentful = require '../../..'

module.exports =
ignores: ["**/_*", "**/.DS_Store"]
extensions: [
contentful(
access_token: 'YOUR_ACCESS_TOKEN'
space_id: 'aqzq2qya2jm4'
locale: ['tlh']
content_types: [
{
id: '6BYT1gNiIEyIw8Og8aQAO6'
}
]
)
]
Loading