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

Uk/1562 add i18n js gem #1564

Merged
merged 11 commits into from Aug 2, 2017
Merged

Uk/1562 add i18n js gem #1564

merged 11 commits into from Aug 2, 2017

Conversation

jeronimo
Copy link
Contributor

@jeronimo jeronimo commented May 8, 2017

Task - #1562

Comments on steps:

  1. Did not create precompiled separate files because:

    • would have to leave default assets pipeline.
    • with config.i18n.available_locales translations are not that big anymore.
    • it might allow to change translations dynamically.
  2. It falls back to default translation and doesn't break javascript. The problem is still with back-end translations inside placeholders for input fields if translation is still missing.

@enricostano
Copy link
Contributor

Nice! 👏

@daniellemoorhead daniellemoorhead added this to the Next small release (1.8.11 or 1.9.1) milestone May 12, 2017
@daniellemoorhead
Copy link
Contributor

@enricostano you ok to review this before we push it over to Australia for final test/merge?

Copy link
Contributor

@enricostano enricostano left a comment

Choose a reason for hiding this comment

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

👍

@daniellemoorhead
Copy link
Contributor

Hey @oeoeaio I reckon this one is going to be a quick push through yourself and Sally to get it merged. Will leave you to it :)

Copy link
Contributor

@RohanM RohanM left a comment

Choose a reason for hiding this comment

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

In this PR I see a lot of cases where we try implementing something one way and then switch to doing it another way in a later commit. To me that reads a bit like research / experimentation rather than building. That's a necessary and important part about developing something, but a side-effect is that any developer looking back on this feature will have to understand all the side investigations to understand what's going on in the feature. We'd like to make it as easy as possible for other people to look over this code and understand what's going on.

To achieve that, sometimes in this situation I'd recommend editing the git history to clean up a few things. But in this case there are several changes happening in each commit, which makes it harder to work with. Therefore, it seems like it'd be quicker to treat this branch as a spike and re-implement it fresh in another branch. With the knowledge developed in this one I expect it would fall out quite quickly.

Thanks for all your work on this one, and sorry to add more to your plate. Let me know if you have any questions!

text = text.split("%{#{name}}").join(value)
text
I18n.t(key, options)
# dict = I18n
Copy link
Contributor

Choose a reason for hiding this comment

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

Committed comments later removed.

@@ -9,6 +9,7 @@
remove_method :options_text if instance_methods(false).include? :options_text
include OpenFoodNetwork::VariantAndLineItemNaming

acts_as_taggable
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this belong in this commit (or even this PR)? It seems like it might be a separate concern.

@@ -9,7 +9,6 @@
remove_method :options_text if instance_methods(false).include? :options_text
include OpenFoodNetwork::VariantAndLineItemNaming

acts_as_taggable
Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, I see that this has been removed. In this sort of case I'd recommend editing history to remove the introduction in the first place. But see above for comments about treating this as a spike.

@@ -108,7 +109,5 @@ class Application < Rails::Application
config.assets.precompile += ['qz/*']

config.active_support.escape_html_entities_in_json = true

config.middleware.use I18n::JS::Middleware
Copy link
Contributor

Choose a reason for hiding this comment

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

This line was added and then removed - I take it that we don't need it? In this sort of case I'd recommend editing history to remove the introduction in the first place. But see above for comments about treating this as a spike.

@@ -1,35 +0,0 @@
# Split context in several files.
Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks like another case of trying one approach in one commit, then switching to another in a later commit. See above for comments about treating this PR as a spike.

@@ -13,6 +13,7 @@
//= require textAngular-sanitize.min.js
//= require textAngular.min.js
//= require moment
//= require i18n
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change related to Karma? If not, I'd suggest moving this change to a separate commit. But see above for comments about treating this as a spike.

@jeronimo
Copy link
Contributor Author

@RohanM, You are totally right it felt like a spike :) At some point it felt right to leave it with that commit history as it would help to understand what is necessary to use from gem's documentation. But apparently not. Fair enough :)
Instead of creating another branch it was easier to squash current one and the PR stays the same.
Old commits' history is here https://github.com/jeronimo/openfoodnetwork/commits/uk/add-i18n-js-gem-spike

@lin-d-hop
Copy link
Contributor

@RohanM Are you happy to sign this off now ready for merge at next opportunity?

@RohanM
Copy link
Contributor

RohanM commented May 31, 2017

Hi @jeronimo,

Unfortunately, squashing the commits puts the entire 22 file, 150 line diff in a single commit - that's too much code for someone to easily absorb, especially when that one commit is accomplishing many distinct things. Having the code in one commit obscures the steps that went into achieving the feature. Additionally, in the future we might discover a problem that involves this code. If it's in a single commit, the we're not able to use git bisect to narrow down on the cause of the problem, and it's harder for a developer to understand the code.

Because of those reasons, it's useful to break the code down into smaller commits (both for a reviewer and for any future developers who might work with this code). Preferably each commit introduces one part of the change, comes with its own specs, and those specs pass. That way a developer can read the commit message and glance at the code and confirm that the code does indeed do what the commit message says. And because the specs build green at most if not all commits along the way, we can bisect over it and get useful results.

Finally, a comment on the situation where a branch is broken down into multiple commits but changes one thing and then changes it back later. Although this can be useful in describing the process taken to arrive at a feature, it can result in unnecessary conflicts when rebasing. And when multiple changes back and fourth are combined in a single commit, it can be hard to pick apart what's happening. So I'd see this situation as a trade-off, and unless I really wanted to illustrate a path that didn't work out, I'd tend to the side of simply removing those changes from the code, and writing about it in the PR description (which a future developer could look up from the merge commit).

For those reasons, would you be able to rewrite this PR step by step?

@jeronimo
Copy link
Contributor Author

@RohanM, actually I do not agree. Nearly 60% of added code is test coverage. 20% of code is a workaround until Spree gets upgrade. The rest of the code is used from Rails guides and READMEs and this whole commit is like a small feature, which is great for git bisect. If you would notice, original PR had 3 commits only, and that was only because I tried various approaches how to use i18n-js gem until I chose best one (IMHO) for our case.

But, if you still do think it is necessary to split, maybe you could give an example with this code for the future reference we could follow?

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Hi @jeronimo. I agree with Rohan that smaller commits are easier to read. Your commit is not huge, but it could be nicer. Below I commented on a few things that could be separated, in my opinion. Ideally, you read one commit at a time like pages of a book. And that includes test cases.

Anyway, that's great work. I have a question. We include all supported translations in all.js, right? Can you tell how big each locale is? How much longer does it need to load with e.g. five locales?
I had in mind that each locale might be available as separate js file (except the default locale). Each user wouldn't need to load all the unused locales then. I'm just imagining a platform that wants to support a lot of languages.

@@ -4,6 +4,7 @@ ruby "2.1.5"
gem 'rails', '3.2.21'
gem 'rails-i18n', '~> 3.0.0'
gem 'i18n', '~> 0.6.11'
gem 'i18n-js', '~> 3.0.0'
Copy link
Member

Choose a reason for hiding this comment

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

Adding a gem can be in a single commit.

@@ -71,6 +71,7 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = ENV["LOCALE"]
config.i18n.available_locales = ENV["AVAILABLE_LOCALES"].andand.split(',').andand.map(&:strip) || [config.i18n.default_locale]
Copy link
Member

Choose a reason for hiding this comment

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

Support for multiple locales can be seen as a feature on it's own. It can be in a separate commit.

@jeronimo
Copy link
Contributor Author

jeronimo commented Jun 8, 2017

@RohanM, @mkllnk, @oeoeaio please have a look now :)

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Wow. This is much easier to read. Excellent.

# no: "No"
# yes: "Yes"
# Which become to true and false and those have no #to_sym method
# TODO - remove this after spree core locales are fixed
Copy link
Member

Choose a reason for hiding this comment

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

Great. This way it will be super easy to see what needs to be changed after the Spree upgrade.

@mkllnk
Copy link
Member

mkllnk commented Jun 9, 2017

I think before we merge this, we need to update our deploy scripts. @jeronimo Have a look at: https://github.com/openfoodfoundation/ofn-install/blob/master/roles/app/templates/post-receive.j2

We need to add the line:

$BUNDLE exec rake tmp:cache:clear

Can that happen any time or do we need to do it before the asset compilation?

@jeronimo
Copy link
Contributor Author

@mkllnk It seems it has to be before assets:precompile. More details inside PR openfoodfoundation/ofn-install#73.

Note: tmp:cache:clear need should disappear after upgrade to rails 4.x which will introduce sprockets.

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Great. This is ready for testing now.

@mkllnk
Copy link
Member

mkllnk commented Aug 2, 2017

@sstead This adds a feature to provide more than one language. Currently, our staging server is configured to show only English. Nothing should change. And we don't have a UI element for language switching yet. That comes later.

@sstead
Copy link

sstead commented Aug 2, 2017

Looks fine.

@RohanM RohanM merged commit 686a8f3 into openfoodfoundation:master Aug 2, 2017
@mkllnk
Copy link
Member

mkllnk commented Aug 2, 2017

Great work @jeronimo.

I needed to add a fall-back strategy for old application.yml files that don't have the locale set correctly. Otherwise that i18n-js gem would crash. But that wasn't your fault. Glad to have this merged.

@mkllnk mkllnk requested a deployment to staging August 2, 2017 03:08 Abandoned
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.

None yet

7 participants