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 ability to import libraries on top of the translations file in an es6 way #549

Merged
merged 3 commits into from
Oct 10, 2019
Merged

Conversation

mcamara
Copy link

@mcamara mcamara commented Sep 16, 2019

In order to use rails with webpack and the i18n-js npm package all together, import I18n from 'i18n-js'; should be added to the resultant translations.js file.

This PR allows an user to add the import programatically just adding the option in the i18n-js.yml file.

Thanks @PikachuEXE for your package, I hope you find this option useful 😄

@PikachuEXE
Copy link

I currently use https://github.com/usabilityhub/rails-erb-loader:

// i18n-js.js.erb or i18n-js-ts.erb
<%# encoding: UTF-8 %>

// Optional dependency watching
// Tell webpack to watch these files
// https://github.com/usabilityhub/rails-erb-loader#dependencies
/* rails-erb-loader-dependencies ./../config/locales/ */

import I18n from "i18n-js"

I18n.translations = <%= I18n::JS.filtered_translations.to_json %>;

export default I18n

My erb loader file:

module.exports = {
  test:     /\.erb$/,
  enforce:  "pre",
  exclude:  /node_modules/,

  use: [{
    loader:   "rails-erb-loader",
    options:  {
      runner:     (/^win/.test(process.platform) ? "ruby " : "") + "bin/rails runner",
      env:        {
        ...process.env,
        DISABLE_SPRING: 1,
        RAILS_CONFIG_EAGER_LOAD_ENABLED_IN_DEVELOPMENT: "false",
      },
    },
  }],
}

@mcamara
Copy link
Author

mcamara commented Sep 17, 2019

I really like your solution @PikachuEXE , but you need an extra package and change the options file in webpacker, what do you think about the solution I offer in this PR? The new option in the yml file is completely optional.

README.md Outdated Show resolved Hide resolved
lib/i18n/js/formatters/js.rb Outdated Show resolved Hide resolved
@rafaell-lycan
Copy link

LGTM

@PikachuEXE
Copy link

Still pending change from @mcamara though

@mcamara
Copy link
Author

mcamara commented Oct 9, 2019

@PikachuEXE done :)

@TravisBuddy
Copy link

Travis tests have failed

Hey @mcamara,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

Ruby: 2.5

View build log

rake
Using bundler 1.17.3
Using thor 0.20.3
Using json 2.2.0
Using docile 1.3.2
Using simplecov-html 0.10.2
Using tins 1.21.1
Using gem-release 2.0.3
Using i18n 0.7.0
Using diff-lcs 1.3
Using appraisal 2.2.0
Using simplecov 0.16.1
Using term-ansicolor 1.7.1
Using i18n-js 3.3.0 from source at `../`
Fetching rspec-support 3.9.0
Using coveralls 0.8.23
Installing rspec-support 3.9.0
Fetching rspec-core 3.9.0
Fetching rspec-expectations 3.9.0
Installing rspec-expectations 3.9.0
Installing rspec-core 3.9.0
Fetching rspec-mocks 3.9.0
Installing rspec-mocks 3.9.0
Fetching rspec 3.9.0
Installing rspec 3.9.0
Bundle complete! 7 Gemfile dependencies, 20 gems now installed.
Bundled gems are installed into `./gemfiles/vendor/bundle`
travis_time:end:021db642:start=1570611323856195776,finish=1570611326931619234,duration=3075423458,event=install
travis_fold:end:install.bundler

travis_time:start:10bb8aec
$ bundle exec rake
/home/travis/.rvm/rubies/ruby-2.5.5/bin/ruby -I/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-core-3.9.0/lib:/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-support-3.9.0/lib /home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-core-3.9.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
[Coveralls] Set up the SimpleCov formatter.
[Coveralls] Using SimpleCov's 'rails' settings.
.........................................................................F............................

Failures:

  1) I18n::JS namespace, prefix and pretty_print options exports with defined locale as fallback when enabled
     Failure/Error: erb_result_from_yaml_file = ERB.new(File.read(config_file_path)).result
     
     Errno::ENOENT:
       No such file or directory @ rb_sysopen - /home/travis/build/fnando/i18n-js/spec/fixtures/js_file_with_namespace_import_and_pretty_print.yml
     # ./lib/i18n/js.rb:112:in `read'
     # ./lib/i18n/js.rb:112:in `config'
     # ./lib/i18n/js.rb:247:in `export_i18n_js_dir_path'
     # ./lib/i18n/js.rb:235:in `export_i18n_js'
     # ./lib/i18n/js.rb:43:in `export'
     # ./spec/ruby/i18n/js_spec.rb:362:in `block (3 levels) in <top (required)>'

Finished in 0.17968 seconds (files took 0.22494 seconds to load)
102 examples, 1 failure

Failed examples:

rspec ./spec/ruby/i18n/js_spec.rb:361 # I18n::JS namespace, prefix and pretty_print options exports with defined locale as fallback when enabled

[Coveralls] Submitting to https://coveralls.io/api/v1
[Coveralls] Job #616.24
[Coveralls] https://coveralls.io/jobs/54195942
Coverage is at 67.98%.
Coverage report sent to Coveralls.
/home/travis/.rvm/rubies/ruby-2.5.5/bin/ruby -I/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-core-3.9.0/lib:/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-support-3.9.0/lib /home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/gems/rspec-core-3.9.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb failed
travis_time:end:10bb8aec:start=1570611326936774567,finish=1570611329403220626,duration=2466446059,event=
The command "bundle exec rake" exited with 1.
travis_fold:start:cache.2
store build cache
$ bundle clean
Removing rspec-expectations (3.8.4)
Removing rspec (3.8.0)
Removing rspec-mocks (3.8.1)
Removing rspec-support (3.8.2)
Removing rspec-core (3.8.2)
travis_time:start:08443fe2
travis_time:end:08443fe2:start=1570611329638933680,finish=1570611329642578021,duration=3644341,event=cache
travis_time:start:2ffe98cc
changes detected (content changed, file is created, or file is deleted):\n/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-3.8.0.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-3.9.0.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-core-3.8.2.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-core-3.9.0.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-expectations-3.8.4.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-expectations-3.9.0.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-mocks-3.8.1.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-mocks-3.9.0.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-support-3.8.2.gem
/home/travis/build/fnando/i18n-js/gemfiles/vendor/bundle/ruby/2.5.0/cache/rspec-support-3.9.0.gem
/home/travis/build/fnando/i18n-js/ge\n...
changes detected, packing new archive
uploading cache-linux-xenial-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855--rvm-2.5--gemfile-gemfiles/i18n_0_7.gemfile.tgz
cache uploaded
travis_time:end:2ffe98cc:start=1570611329647236389,finish=1570611331467281638,duration=1820045249,event=cache
travis_fold:end:cache.2


Done. Your build exited with 1.
TravisBuddy Request Identifier: 638af320-ea73-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 67b502e0-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 6a66dc70-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 89671c20-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 916e50f0-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 923ceb90-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 98175550-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 990c8ac0-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: c0a73e90-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: c18df510-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: c6ad4c30-ea75-11e9-90a0-7556777797a6

@TravisBuddy
Copy link

Hey @mcamara,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: d96f8b30-ea75-11e9-90a0-7556777797a6

@PikachuEXE PikachuEXE merged commit 84dec7c into fnando:master Oct 10, 2019
@PikachuEXE
Copy link

Thanks
Releasing next week if no other urgent pending changes

@PikachuEXE
Copy link

Released in 3.4.0

@@ -248,6 +248,27 @@ MyNamespace.translations["en"] = { ... }
```


### Adding a line at the beggining of the translations file (useful for imports)

Setting the `prefix: "import I18n from 'i18n-js';\n"` option will add the line at the beggining of the resultant translation file.

Choose a reason for hiding this comment

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

typo: beginning

Choose a reason for hiding this comment

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

Fixed in 64d8c5a

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.

5 participants