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

Adds support to localized components files #977

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

lfalcao
Copy link
Contributor

@lfalcao lfalcao commented Jun 23, 2021

Related: #894

I just combined the "localized" information from component files (my_component.es.html.erb) into variant attribute to avoid adding new parameters or functions and worked well 😄

It's not done yet, needs some work, more tests, benchmarks... but I wanted to see some opinions about this approach, if there's something else I'm not seeing.

Copy link
Collaborator

@Spone Spone left a comment

Thanks for tackling this! A few quick comments/questions.

lib/view_component/base.rb Outdated Show resolved Hide resolved
lib/view_component/compiler.rb Outdated Show resolved Hide resolved
lib/view_component/compiler.rb Outdated Show resolved Hide resolved
@lfalcao
Copy link
Contributor Author

@lfalcao lfalcao commented Jun 24, 2021

Benchmark:

localized components:

Warming up --------------------------------------
component: 13.862k i/100ms
partial: 2.296k i/100ms
Calculating -------------------------------------
component: 139.812k (± 0.9%) i/s - 1.400M in 10.014746s
partial: 22.876k (± 3.2%) i/s - 229.600k in 10.047667s

main branch:

Warming up --------------------------------------
component: 13.007k i/100ms
partial: 2.440k i/100ms
Calculating -------------------------------------
component: 129.883k (± 1.9%) i/s - 1.301M in 10.018044s
partial: 24.145k (± 3.6%) i/s - 241.560k in 10.021579s

@lfalcao lfalcao requested a review from Spone Jun 24, 2021
@Spone
Copy link
Collaborator

@Spone Spone commented Jun 25, 2021

I'm doing some tests in a dummy app, with the following configuration:

I18n.available_locales = [:en, :es, :fr]
I18n.default_locale = :en

I'm adding a FooComponent with 3 partials:

app/components/foo_component.en.html.erb
# no partial for `es`
app/components/foo_component.fr.html.erb
app/components/foo_component.html.erb
  • If I set I18n.locale = :en in my request, I get the foo_component.html.erb ➡️ I would expect foo_component.en.html.erb
  • If I set I18n.locale = :es in my request, I get the foo_component.html.erb ✔️
  • If I set I18n.locale = :fr in my request, I get the foo_component.fr.html.erb ✔️
  • If I set no locale in my request, I get the foo_component.html.erb ➡️ I would expect the I18n.default_locale to be used, so foo_component.en.html.erb

Now If I'm removing this partial and restart the server :

app/components/foo_component.en.html.erb
app/components/foo_component.fr.html.erb
- app/components/foo_component.html.erb
  • If I set I18n.locale = :en in my request, I get an error undefined local variable or method 'call' for #<FooComponent:0x00007f9720c56610>
  • If I set I18n.locale = :es in my request, I get an error undefined local variable or method 'call' for #<FooComponent:0x00007f9720c56610>
  • If I set I18n.locale = :fr in my request, I get the foo_component.fr.html.erb ✔️
  • If I set no locale in my request, I get an error undefined local variable or method 'call' for #<FooComponent:0x00007f9720c56610>

I guess it should work without a "default" partial, what do you think?


EDIT: here is the standard behavior with localized views in Rails:

app/views/pages/show.en.html.erb
# no view for `es`
app/views/pages/show.fr.html.erb
app/views/pages/show.html.erb
  • If I set I18n.locale = :en in my request, I get the show.en.html.erb
  • If I set I18n.locale = :es in my request, I get the show.en.html.erb
  • If I set I18n.locale = :fr in my request, I get the show.fr.html.erb
  • If I set no locale in my request, I get the show.en.html.erb
  • Actually, I never get the show.html.erb and I can remove it without getting errors

@lfalcao
Copy link
Contributor Author

@lfalcao lfalcao commented Jun 26, 2021

Thanks for reviewing it @Spone

I think every case is expected as I used the I18n.default_locale as the default, without locale information in the filename. I mean, if you set I18n.default_locale = :en there's no need to have all your files with '.en'.... 🤔

There's a (test) for this.

If I set I18n.locale = :es in my request, I get an error undefined local variable or method 'call' for #FooComponent:0x00007f9720c56610

This happened because there's no foo_component.es.html.erb. You think we should to fallback to the foo_component.html.erb ?

If I set no locale in my request, I get an error undefined local variable or method 'call' for #FooComponent:0x00007f9720c56610

Also expected, as the file foo_component.html.erb was deleted.

If I set no locale in my request, I get the show.en.html.erb
Actually, I never get the show.html.erb and I can remove it without getting errors

Without locale, but with I18n.default_locale = :en right?

@Spone
Copy link
Collaborator

@Spone Spone commented Jun 26, 2021

This happened because there's no foo_component.es.html.erb. You think we should to fallback to the foo_component.html.erb ?

In this case, Rails localized views fall back to the default locale (so foo_component.en.html.erb in this case).

Also expected, as the file foo_component.html.erb was deleted.

Yes but in this case Rails localized views fall back to the default locale as well.

Without locale, but with I18n.default_locale = :en right?

Yes. Actually you can also remove it from the config since :en is the default value.

To sum up, I think the fall back behavior should be the same as Rails standard behavior with localized views, as described in the EDIT of my previous comment. What do you think?

Copy link
Collaborator

@jonspalmer jonspalmer left a comment

Looks powerful. A couple of suggested simplifications and cleanups

lib/view_component/compiler.rb Outdated Show resolved Hide resolved
lib/view_component/compiler.rb Outdated Show resolved Hide resolved
lib/view_component/compiler.rb Outdated Show resolved Hide resolved
test/test_helper.rb Outdated Show resolved Hide resolved
lib/view_component/base.rb Outdated Show resolved Hide resolved
@lfalcao
Copy link
Contributor Author

@lfalcao lfalcao commented Jun 26, 2021

Thanks @Spone and @jonspalmer for the comments. I'll have a look into it carefully.

@23tux
Copy link

@23tux 23tux commented Jun 9, 2022

@lfalcao is there any news on this PR?

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

4 participants