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 lang fields to field projection #485

Merged
merged 2 commits into from Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions dadi/lib/model/collections/find.js
Expand Up @@ -72,11 +72,9 @@ function find ({
// include a field and request a certain language, we must also include
// that corresponding variation).
config.get('i18n.languages').forEach(supportedLanguage => {
if (options.fields[field] === 0 || supportedLanguage === language) {
let langField = name + config.get('i18n.fieldCharacter') + supportedLanguage
let langField = name + config.get('i18n.fieldCharacter') + supportedLanguage

queryFields[langField] = options.fields[field]
}
queryFields[langField] = options.fields[field]
})

// If we're limiting the fields we're requesting, we need to
Expand Down
3 changes: 2 additions & 1 deletion features.json
@@ -1,3 +1,4 @@
[
"aclv1"
"aclv1",
"i18nv1"
]
50 changes: 49 additions & 1 deletion test/acceptance/i18n.js
Expand Up @@ -319,7 +319,7 @@ describe('Multi-language', function () {
})
})

it('should return the translation version of a field when the fields projection is set to include the field in question', done => {
it('should return the translation version of a field when the fields projection is set to include the field in question (with `lang` param)', done => {
config.set('i18n.languages', ['pt', 'fr'])

let documents = [
Expand Down Expand Up @@ -369,6 +369,54 @@ describe('Multi-language', function () {
})
})

it('should return the translation version of a field when the fields projection is set to include the field in question (without `lang` param)', done => {
config.set('i18n.languages', ['pt', 'fr'])

let documents = [
{
title: 'The Little Prince',
'title:pt': 'O Principezinho',
'title:fr': 'Le Petit Prince'
},
{
title: 'The Untranslatable'
}
]

client
.post('/v1/library/book')
.set('Authorization', `Bearer ${bearerToken}`)
.send(documents)
.expect(200)
.end((err, res) => {
if (err) return done(err)

client
.get(`/v1/library/book?fields={"title":1}`)
.set('Authorization', `Bearer ${bearerToken}`)
.expect(200)
.end((err, res) => {
res.body.results.length.should.eql(2)

let results = res.body.results

results[0].title.should.eql(documents[0]['title'])
should.not.exist(results[0]._i18n)
results[0]['title:pt'].should.eql(documents[0]['title:pt'])
results[0]['title:fr'].should.eql(documents[0]['title:fr'])

results[1].title.should.eql(documents[1].title)
should.not.exist(results[1]._i18n)
should.not.exist(results[1]['title:pt'])
should.not.exist(results[1]['title:fr'])

config.set('i18n.languages', configBackup.i18n.languages)

done()
})
})
})

it('should return the original version of a field when the requested language is not part of `i18n.languages`', done => {
config.set('i18n.languages', ['fr'])

Expand Down