Skip to content

Commit eccbd03

Browse files
authored
Merge pull request #66 from levimykel/i18n
Add i18n functionality
2 parents 5a63f3b + 825f2bc commit eccbd03

8 files changed

Lines changed: 104 additions & 13 deletions

File tree

lib/prismic.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ def serialize(field)
209209
# @param fields [String] The fields separated by commas (,)
210210
# @return [SearchForm] self
211211

212+
# @!method lang(lang)
213+
# Specify a language for this form.
214+
# @param lang [String] The document language
215+
# @return [SearchForm] self
216+
212217
# Create the fields'helper methods
213218
def create_field_helper_method(name)
214219
return if name == 'ref'
@@ -461,6 +466,10 @@ class Document
461466
attr_accessor :first_publication_date
462467
# @return Time
463468
attr_accessor :last_publication_date
469+
# @return [String]
470+
attr_accessor :lang
471+
# @return [Array<AlternateLanguage>]
472+
attr_accessor :alternate_languages
464473
# @return [Array<Fragment>]
465474
attr_accessor :fragments
466475

@@ -473,6 +482,8 @@ def initialize(
473482
slugs,
474483
first_publication_date,
475484
last_publication_date,
485+
lang,
486+
alternate_languages,
476487
fragments
477488
)
478489
@id = id
@@ -483,6 +494,8 @@ def initialize(
483494
@slugs = slugs
484495
@first_publication_date = first_publication_date
485496
@last_publication_date = last_publication_date
497+
@lang = lang
498+
@alternate_languages = alternate_languages
486499
@fragments = fragments
487500
end
488501

@@ -556,7 +569,7 @@ def link_to(doc)
556569
if doc.is_a? Prismic::Fragments::DocumentLink
557570
@blk.call(doc)
558571
elsif doc.is_a? Prismic::Document
559-
doc_link = Prismic::Fragments::DocumentLink.new(doc.id, doc.uid, doc.type, doc.tags, doc.slug, doc.fragments, false)
572+
doc_link = Prismic::Fragments::DocumentLink.new(doc.id, doc.uid, doc.type, doc.tags, doc.slug, doc.lang, doc.fragments, false)
560573
@blk.call(doc_link)
561574
end
562575
end
@@ -574,6 +587,28 @@ def serialize(element, content)
574587
@blk.call(element, content)
575588
end
576589
end
590+
591+
592+
# A class for the alternate language versions of a document
593+
#
594+
# The {Prismic.alternate_language} function is the recommended way to create an AlternateLanguage.
595+
class AlternateLanguage
596+
# @return [String]
597+
attr_accessor :id
598+
# @return [String]
599+
attr_accessor :uid
600+
# @return [String]
601+
attr_accessor :type
602+
# @return [String]
603+
attr_accessor :lang
604+
605+
def initialize(json)
606+
@id = json['id']
607+
@uid = json['uid']
608+
@type = json['type']
609+
@lang = json['lang']
610+
end
611+
end
577612

578613
# Default HTTP client implementation, using the standard Net::HTTP library.
579614
module DefaultHTTPClient

lib/prismic/api.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def all(opts={})
103103
# @param opts [Hash] query options (page, pageSize, ref, etc.)
104104
# @return the document, or nil if not found
105105
def get_by_id(id, opts={})
106+
unless opts.key?("lang")
107+
opts["lang"] = '*'
108+
end
106109
query(Prismic::Predicates::at('document.id', id), opts)[0]
107110
end
108111
alias :getByID :get_by_id
@@ -113,6 +116,9 @@ def get_by_id(id, opts={})
113116
# @param opts [Hash] query options (ref, etc.)
114117
# @return the document, or nil if not found
115118
def get_by_uid(typ, uid, opts={})
119+
unless opts.key?("lang")
120+
opts["lang"] = '*'
121+
end
116122
query(Prismic::Predicates::at('my.'+typ+'.uid', uid), opts)[0]
117123
end
118124
alias :getByUID :get_by_uid
@@ -122,6 +128,9 @@ def get_by_uid(typ, uid, opts={})
122128
# @param opts [Hash] query options (page, pageSize, ref, etc.)
123129
# @return the document, or nil if not found
124130
def get_by_ids(ids, opts={})
131+
unless opts.key?("lang")
132+
opts["lang"] = '*'
133+
end
125134
query(Prismic::Predicates::in('document.id', ids), opts)
126135
end
127136
alias :getByIDs :get_by_ids
@@ -148,7 +157,7 @@ def preview_session(token, link_resolver, default_url)
148157
return default_url
149158
end
150159
json = JSON.load(response.body)
151-
documents = self.form('everything').query(Prismic::Predicates.at('document.id', json['mainDocument'])).submit(token)
160+
documents = self.form('everything').query(Prismic::Predicates.at('document.id', json['mainDocument'])).lang("*").submit(token)
152161
if documents.results.size > 0
153162
link_resolver.link_to(documents.results[0])
154163
else

lib/prismic/fragments/link.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ def url(link_resolver=nil)
9696

9797
class DocumentLink < Link
9898
include Prismic::WithFragments
99-
attr_accessor :id, :uid, :type, :tags, :slug, :fragments, :broken
99+
attr_accessor :id, :uid, :type, :tags, :slug, :lang, :fragments, :broken
100100

101-
def initialize(id, uid, type, tags, slug, fragments, broken)
101+
def initialize(id, uid, type, tags, slug, lang, fragments, broken)
102102
@id = id
103103
@uid = uid
104104
@type = type
105105
@tags = tags
106106
@slug = slug
107+
@lang = lang
107108
@fragments = fragments
108109
@broken = broken
109110
end

lib/prismic/json_parsers.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def document_link_parser(json)
4848
type,
4949
doc['tags'],
5050
URI.unescape(doc['slug']),
51+
doc['lang'],
5152
fragments,
5253
json['value']['isBroken'])
5354
end
@@ -221,6 +222,10 @@ def fragment_parser(fragment)
221222
end
222223
end
223224

225+
def alternate_language_parser(alternate_language)
226+
Prismic::AlternateLanguage.new(alternate_language)
227+
end
228+
224229
def document_parser(json)
225230
data_json = json['data'].values.first # {"doc_type": data}
226231

@@ -234,6 +239,13 @@ def document_parser(json)
234239
known_type
235240
}
236241

242+
alternate_languages = nil
243+
if json.key?('alternate_languages')
244+
alternate_languages = Hash[json['alternate_languages'].map { |doc|
245+
[doc['lang'], alternate_language_parser(doc)]
246+
}]
247+
end
248+
237249
fragments = Hash[data_json.map { |name, fragment|
238250
[name, fragment_parser(fragment)]
239251
}]
@@ -247,6 +259,8 @@ def document_parser(json)
247259
json['slugs'].map { |slug| URI.unescape(slug) },
248260
json['first_publication_date'] && Time.parse(json['first_publication_date']),
249261
json['last_publication_date'] && Time.parse(json['last_publication_date']),
262+
json['lang'],
263+
alternate_languages,
250264
fragments)
251265
end
252266

spec/fragments_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def strong(start, stop)
4848

4949
describe 'DocumentLink' do
5050
before do
51-
@document_link = Prismic::Fragments::DocumentLink.new("UdUjvt_mqVNObPeO", nil, "product", ["Macaron"], "dark-chocolate-macaron", {}, false)
51+
@document_link = Prismic::Fragments::DocumentLink.new("UdUjvt_mqVNObPeO", nil, "product", ["Macaron"], "dark-chocolate-macaron", "en-us", {}, false)
5252
end
5353

5454
describe 'url' do
@@ -59,6 +59,12 @@ def strong(start, stop)
5959
@document_link.url(@link_resolver).should == 'http://localhost/UdUjvt_mqVNObPeO'
6060
end
6161
end
62+
63+
describe 'lang' do
64+
it 'is available' do
65+
@document_link.lang.should == 'en-us'
66+
end
67+
end
6268
end
6369

6470
describe 'ImageLink' do
@@ -638,6 +644,7 @@ def strong(start, stop)
638644
"product",
639645
["Macaron"],
640646
"dark-chocolate-macaron",
647+
"fr-fr",
641648
{},
642649
false # not broken
643650
)

spec/json_parsers_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"id": "UdUjvt_mqVNObPeO",
1212
"type": "product",
1313
"tags": ["Macaron"],
14-
"slug": "dark-chocolate-macaron"
14+
"slug": "dark-chocolate-macaron",
15+
"lang": "en-us"
1516
},
1617
"isBroken": false
1718
}
@@ -26,6 +27,7 @@
2627
document_link.type.should == "product"
2728
document_link.tags.should == ['Macaron']
2829
document_link.slug.should == "dark-chocolate-macaron"
30+
document_link.lang.should == "en-us"
2931
document_link.broken?.should == false
3032
end
3133
end
@@ -361,13 +363,23 @@
361363
@document.href.should == 'doc-url'
362364
@document.tags.should == ['Macaron']
363365
@document.slugs.should == ['vanilla-macaron', '南大沢']
366+
@document.lang.should == 'en-us'
364367
end
365368

366369
it "correctly parses the document's publication dates" do
367370
@document.first_publication_date.should == Time.at(1476845881)
368371
@document.last_publication_date.should == Time.at(1476846111)
369372
end
370373

374+
it "correctly parses the document's alternate languages" do
375+
@document.alternate_languages.size.should == 2
376+
@document.alternate_languages['fr-fr'].should be_a Prismic::AlternateLanguage
377+
@document.alternate_languages['fr-fr'].id.should == "WL2IziIAACIAem32"
378+
@document.alternate_languages['fr-fr'].type.should == "product"
379+
@document.alternate_languages['fr-fr'].lang.should == "fr-fr"
380+
@document.alternate_languages['fr-fr'].uid.should == nil
381+
end
382+
371383
it "correctly parses the document's fragments" do
372384
@document.fragments.size.should == 14
373385
@document.fragments['name'].should be_a Prismic::Fragments::StructuredText

spec/prismic_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,30 +267,30 @@ def redirect_uri_encoded
267267

268268
describe 'LinkResolver' do
269269
before do
270-
@doc_link = Prismic::Fragments::DocumentLink.new('id', nil, 'blog-post', ['tag1', 'tag2'], 'my-slug', {}, false)
271-
@document = Prismic::Document.new('id', nil, 'blog-post', nil, ['tag1', 'tag2'], ['my-slug', 'my-other-slug'], nil, nil, nil)
270+
@doc_link = Prismic::Fragments::DocumentLink.new('id', nil, 'blog-post', ['tag1', 'tag2'], 'my-slug', "fr-fr", {}, false)
271+
@document = Prismic::Document.new('id', nil, 'blog-post', nil, ['tag1', 'tag2'], ['my-slug', 'my-other-slug'], nil, nil, "en-us", nil, nil)
272272

273273
@link_resolver = Prismic::LinkResolver.new(nil) do |doc|
274-
'/'+doc.type+'/'+doc.id+'/'+doc.slug
274+
'/'+doc.lang+'/'+doc.type+'/'+doc.id+'/'+doc.slug
275275
end
276276
end
277277

278278
it 'builds the right URL from a DocumentLink' do
279-
@link_resolver.link_to(@doc_link).should == '/blog-post/id/my-slug'
279+
@link_resolver.link_to(@doc_link).should == '/fr-fr/blog-post/id/my-slug'
280280
end
281281

282282
it 'builds the right URL from a Document' do
283-
@link_resolver.link_to(@document).should == '/blog-post/id/my-slug'
283+
@link_resolver.link_to(@document).should == '/en-us/blog-post/id/my-slug'
284284
end
285285
end
286286

287287
describe 'Document' do
288288
before do
289289
fragments = {
290-
'field1' => Prismic::Fragments::DocumentLink.new(nil, nil, nil, nil, nil, {}, nil),
290+
'field1' => Prismic::Fragments::DocumentLink.new(nil, nil, nil, nil, nil, "fr-fr", {}, nil),
291291
'field2' => Prismic::Fragments::WebLink.new('weburl')
292292
}
293-
@document = Prismic::Document.new(nil, nil, nil, nil, nil, ['my-slug'], nil, nil, fragments)
293+
@document = Prismic::Document.new(nil, nil, nil, nil, nil, ['my-slug'], nil, nil, "fr-fr", nil, fragments)
294294
@link_resolver = Prismic::LinkResolver.new('master'){|doc_link|
295295
"http://host/#{doc_link.id}"
296296
}

spec/responses_mocks/document.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
"vanilla-macaron",
1212
"%E5%8D%97%E5%A4%A7%E6%B2%A2"
1313
],
14+
"lang": "en-us",
15+
"alternate_languages": [
16+
{
17+
"id": "WL6VySoAACwA_H9r",
18+
"type": "product",
19+
"lang": "es-es"
20+
},
21+
{
22+
"id": "WL2IziIAACIAem32",
23+
"type": "product",
24+
"lang": "fr-fr"
25+
}
26+
],
1427
"data": {
1528
"product": {
1629
"name": {

0 commit comments

Comments
 (0)