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 #translations, #init_translations and #intialized? #97

Merged
merged 1 commit into from
Feb 27, 2020

Conversation

vipera
Copy link
Contributor

@vipera vipera commented Nov 12, 2019

I'm using fnando/i18n-js in my project, which basically calls #translations to fetch an entire hash of translations to output in JavaScript. Their project documents that it is incompatible with anything other than a Simple backend, but it actually only requires a couple of hook methods to get working which I have added in this patch. I think that a number of users would benefit from being able to get translations from the ActiveRecord backend into the frontend.

The methods are unused inside the project itself, but I've added some tests to cover their expected behaviour.

Thanks for taking the time to take a look at this :)

@timfjord
Copy link
Collaborator

@vipera Thanks for the contribution.
In general, I like the idea, I just need some time to think how to integrate it nicely

@nicovak
Copy link

nicovak commented Dec 18, 2019

It would be great to have this, thanks. I just experienced the issue right now. I'll keep JS translations in YAML for the moment.
@Timsly any news to integrate the PR ?

@timfjord
Copy link
Collaborator

I will try to review it next week, so potentially in 1-2 weeks, it should be merged.

@nicovak
Copy link

nicovak commented Dec 18, 2019

I just tested @vipera repo and it works for me. In case It can helps :

Before (PR) I had to add these block in i18n_active_record.rb initalizer to make it works.

module I18n
  def self.reload!
    I18n::JS.backend.reload!
    super
  end
end

and

I18n::JS.backend = I18n.backend

And the full config :

# frozen_string_literal: true

module I18n
  def self.reload!
    I18n::JS.backend.reload!
    super
  end
end

require "i18n/backend/active_record"

I18n::JS.backend = I18n.backend
I18n.backend = I18n::Backend::ActiveRecord.new

I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

# Populate missing translations to database
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::ActiveRecord::Missing)

I18n.backend = I18n::Backend::Chain.new(I18n.backend, I18n::Backend::Simple.new)

I18n::Backend::ActiveRecord.configure do |config|
  config.cleanup_with_destroy = true # defaults to false
end

With the PR, this config works :

# frozen_string_literal: true

require "i18n/backend/active_record"

I18n.backend = I18n::Backend::ActiveRecord.new

I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

# Populate missing translations to database
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::ActiveRecord::Missing)

I18n.backend = I18n::Backend::Chain.new(I18n.backend, I18n::Backend::Simple.new)

I18n::Backend::ActiveRecord.configure do |config|
  config.cleanup_with_destroy = true # defaults to false
end

@nicovak
Copy link

nicovak commented Jan 27, 2020

Hi @Timsly, any news for the merge ? Thanks

lib/i18n/backend/active_record.rb Outdated Show resolved Hide resolved
lib/i18n/backend/active_record.rb Show resolved Hide resolved
@vipera vipera force-pushed the init-translations branch from 796792e to 9d3ab9e Compare February 3, 2020 12:53
For compatibility with i18n-js, these methods are required to
build a complete representation of all translations in the
backend.
@vipera vipera requested a review from timfjord February 27, 2020 12:38
Copy link
Collaborator

@timfjord timfjord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@timfjord timfjord merged commit 752ad19 into svenfuchs:master Feb 27, 2020
@timfjord
Copy link
Collaborator

@vipera Thanks for the contribution!

@reinaris
Copy link

In my case, it did work when using:

 config.middleware.use I18n::JS::Middleware

But it didn't exported the translations from the database when using:

rake i18n:js:export

@timfjord
Copy link
Collaborator

I guess you need to debug how i18n:js:export works, maybe it excepts more methods to be implemented

@reinaris
Copy link

I guess you need to debug how i18n:js:export works, maybe it excepts more methods to be implemented

Thanks Tim! I just mentioned it here, for people who read the conversation and maybe run into the same problem 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants