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

432 - test and document fae_translate on Fae::StaticPage #472

Merged
merged 1 commit into from
May 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions docs/features/multi_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,39 @@ Finally, to display the language select menu, you'll need to add `language: true
Multiple inputs will be generated for blocks that support for multiple languages. Add a `:languages` key to the field's definition.

```ruby
def self.fae_fields
{
body: {
type: Fae::TextArea,
languages: [:en, :zh]
},
annual_report: {
type: Fae::File,
languages: Fae.languages.keys # Set in config/initializers/fae.rb
class AboutPage < Fae::StaticPage

@slug = 'about'

fae_translate :body, :annual_report

def self.fae_fields
{
body: {
type: Fae::TextArea,
languages: [:en, :zh]
},
annual_report: {
type: Fae::File,
languages: Fae.languages.keys # Set in config/initializers/fae.rb
}
}
}
end

end
```

Utilizing `fae_translate` in a `Fae::StaticPage` will automatically use the set locale to determine which content to return.

```ruby
# set locale
I18n.locale = :zh

# calling an attribute will return the translation content
AboutPage.instance.body_content
# => content set in body_zh
```

Add `languages: true` to the page's `fae/shared/form_header` partial to utilize Fae's language switcher.

## In the Application
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/contact_us_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class ContactUsPage < Fae::StaticPage

@slug = 'contact_us'

fae_translate :body

# required to set the has_one associations, Fae::StaticPage will build these associations dynamically
def self.fae_fields
{
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/contact_us_page_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryGirl.define do

factory :contact_us_page do
title 'Contact Us'
slug 'contact_us'
end

end
13 changes: 13 additions & 0 deletions spec/models/fae/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@

expect( Wine.find_by_name('Funky') ).to be_present
end

it 'should translate Fae::StaticPage attributes' do
FactoryGirl.create(:contact_us_page)
contact_page = home_page = ContactUsPage.instance
contact_page.create_body_en(attached_as: 'body_en', content: 'EN body')
contact_page.create_body_zh(attached_as: 'body_zh', content: 'ZH body')

I18n.locale = :zh
expect(contact_page.body_content).to eq('ZH body')

I18n.locale = :en
expect(contact_page.body_content).to eq('EN body')
end
end

end