Skip to content

Commit ef369ed

Browse files
JasonYCHuangjustin808
authored andcommitted
Add an i18n functionality to change language using a dropdown in real time. (shakacode#340)
Demonstrates use of i18n (internationalization) with React on Rails. Docs: https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md Relevant PRs from React on Rails: * shakacode/react_on_rails#642 * shakacode/react_on_rails#717
1 parent 567417f commit ef369ed

30 files changed

+392
-134
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ vendor/ruby
3333
.idea
3434

3535
spec/examples.txt
36+
37+
# Ignore i18n-js
38+
client/app/libs/i18n/translations.js
39+
client/app/libs/i18n/default.js

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ install:
2929
- npm install npm@latest -g
3030
- npm --version
3131
- npm install
32-
- npm run build:client && npm run build:server
32+
- rake react_on_rails:locale
33+
- cd client && npm run build:test
3334
- rake db:setup
3435

3536
# Tip: No need to run xvfb if running headless testing. However, we're going to start with

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gem "sdoc", group: :doc
3838
# Use Rails Html Sanitizer for HTML sanitization
3939
gem "rails-html-sanitizer"
4040

41-
gem "react_on_rails", "~> 6.1"
41+
gem "react_on_rails", "~> 6.7.1"
4242

4343
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
4444
# mini_racer is probably faster than therubyracer

Gemfile.lock

+47-48
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
actioncable (5.0.0.1)
5-
actionpack (= 5.0.0.1)
4+
actioncable (5.0.1)
5+
actionpack (= 5.0.1)
66
nio4r (~> 1.2)
77
websocket-driver (~> 0.6.1)
8-
actionmailer (5.0.0.1)
9-
actionpack (= 5.0.0.1)
10-
actionview (= 5.0.0.1)
11-
activejob (= 5.0.0.1)
8+
actionmailer (5.0.1)
9+
actionpack (= 5.0.1)
10+
actionview (= 5.0.1)
11+
activejob (= 5.0.1)
1212
mail (~> 2.5, >= 2.5.4)
1313
rails-dom-testing (~> 2.0)
14-
actionpack (5.0.0.1)
15-
actionview (= 5.0.0.1)
16-
activesupport (= 5.0.0.1)
14+
actionpack (5.0.1)
15+
actionview (= 5.0.1)
16+
activesupport (= 5.0.1)
1717
rack (~> 2.0)
1818
rack-test (~> 0.6.3)
1919
rails-dom-testing (~> 2.0)
2020
rails-html-sanitizer (~> 1.0, >= 1.0.2)
21-
actionview (5.0.0.1)
22-
activesupport (= 5.0.0.1)
21+
actionview (5.0.1)
22+
activesupport (= 5.0.1)
2323
builder (~> 3.1)
2424
erubis (~> 2.7.0)
2525
rails-dom-testing (~> 2.0)
2626
rails-html-sanitizer (~> 1.0, >= 1.0.2)
27-
activejob (5.0.0.1)
28-
activesupport (= 5.0.0.1)
27+
activejob (5.0.1)
28+
activesupport (= 5.0.1)
2929
globalid (>= 0.3.6)
30-
activemodel (5.0.0.1)
31-
activesupport (= 5.0.0.1)
32-
activerecord (5.0.0.1)
33-
activemodel (= 5.0.0.1)
34-
activesupport (= 5.0.0.1)
30+
activemodel (5.0.1)
31+
activesupport (= 5.0.1)
32+
activerecord (5.0.1)
33+
activemodel (= 5.0.1)
34+
activesupport (= 5.0.1)
3535
arel (~> 7.0)
36-
activesupport (5.0.0.1)
36+
activesupport (5.0.1)
3737
concurrent-ruby (~> 1.0, >= 1.0.2)
3838
i18n (~> 0.7)
3939
minitest (~> 5.1)
@@ -50,7 +50,7 @@ GEM
5050
binding_of_caller (0.7.2)
5151
debug_inspector (>= 0.0.1)
5252
brakeman (3.4.1)
53-
builder (3.2.2)
53+
builder (3.2.3)
5454
bundler-audit (0.5.0)
5555
bundler (~> 1.2)
5656
thor (~> 0.18)
@@ -82,7 +82,7 @@ GEM
8282
coffee-script-source
8383
execjs
8484
coffee-script-source (1.10.0)
85-
concurrent-ruby (1.0.2)
85+
concurrent-ruby (1.0.5)
8686
connection_pool (2.2.1)
8787
coveralls (0.8.15)
8888
json (>= 1.8, < 3)
@@ -109,7 +109,7 @@ GEM
109109
railties (>= 3.0.0)
110110
globalid (0.3.7)
111111
activesupport (>= 4.1.0)
112-
i18n (0.7.0)
112+
i18n (0.8.1)
113113
interception (0.5)
114114
io-like (0.3.0)
115115
jbuilder (2.6.0)
@@ -134,10 +134,10 @@ GEM
134134
mini_portile2 (2.1.0)
135135
mini_racer (0.1.7)
136136
libv8 (~> 5.3)
137-
minitest (5.9.1)
137+
minitest (5.10.1)
138138
multi_json (1.12.1)
139139
nio4r (1.2.1)
140-
nokogiri (1.6.8.1)
140+
nokogiri (1.7.0.1)
141141
mini_portile2 (~> 2.1.0)
142142
parser (2.3.2.0)
143143
ast (~> 2.2)
@@ -165,45 +165,44 @@ GEM
165165
pry-stack_explorer (0.4.9.2)
166166
binding_of_caller (>= 0.7)
167167
pry (>= 0.9.11)
168-
public_suffix (2.0.4)
168+
public_suffix (2.0.5)
169169
puma (3.6.2)
170170
rack (2.0.1)
171171
rack-test (0.6.3)
172172
rack (>= 1.0)
173-
rails (5.0.0.1)
174-
actioncable (= 5.0.0.1)
175-
actionmailer (= 5.0.0.1)
176-
actionpack (= 5.0.0.1)
177-
actionview (= 5.0.0.1)
178-
activejob (= 5.0.0.1)
179-
activemodel (= 5.0.0.1)
180-
activerecord (= 5.0.0.1)
181-
activesupport (= 5.0.0.1)
173+
rails (5.0.1)
174+
actioncable (= 5.0.1)
175+
actionmailer (= 5.0.1)
176+
actionpack (= 5.0.1)
177+
actionview (= 5.0.1)
178+
activejob (= 5.0.1)
179+
activemodel (= 5.0.1)
180+
activerecord (= 5.0.1)
181+
activesupport (= 5.0.1)
182182
bundler (>= 1.3.0, < 2.0)
183-
railties (= 5.0.0.1)
183+
railties (= 5.0.1)
184184
sprockets-rails (>= 2.0.0)
185-
rails-dom-testing (2.0.1)
185+
rails-dom-testing (2.0.2)
186186
activesupport (>= 4.2.0, < 6.0)
187-
nokogiri (~> 1.6.0)
187+
nokogiri (~> 1.6)
188188
rails-html-sanitizer (1.0.3)
189189
loofah (~> 2.0)
190-
railties (5.0.0.1)
191-
actionpack (= 5.0.0.1)
192-
activesupport (= 5.0.0.1)
190+
railties (5.0.1)
191+
actionpack (= 5.0.1)
192+
activesupport (= 5.0.1)
193193
method_source
194194
rake (>= 0.8.7)
195195
thor (>= 0.18.1, < 2.0)
196-
rainbow (2.1.0)
196+
rainbow (2.2.1)
197197
rake (11.3.0)
198198
rb-fsevent (0.9.8)
199199
rb-inotify (0.9.7)
200200
ffi (>= 0.5.0)
201201
rdoc (4.3.0)
202-
react_on_rails (6.1.2)
202+
react_on_rails (6.7.1)
203203
addressable
204204
connection_pool
205205
execjs (~> 2.5)
206-
foreman
207206
rails (>= 3.2)
208207
rainbow (~> 2.1)
209208
redis (3.3.0)
@@ -265,7 +264,7 @@ GEM
265264
activesupport (>= 4.2)
266265
spring-commands-rspec (1.0.4)
267266
spring (>= 0.9.1)
268-
sprockets (3.7.0)
267+
sprockets (3.7.1)
269268
concurrent-ruby (~> 1.0)
270269
rack (> 1, < 3)
271270
sprockets-rails (3.2.0)
@@ -274,8 +273,8 @@ GEM
274273
sprockets (>= 3.0.0)
275274
term-ansicolor (1.4.0)
276275
tins (~> 1.0)
277-
thor (0.19.1)
278-
thread_safe (0.3.5)
276+
thor (0.19.4)
277+
thread_safe (0.3.6)
279278
tilt (2.0.5)
280279
tins (1.12.0)
281280
tzinfo (1.2.2)
@@ -289,7 +288,7 @@ GEM
289288
debug_inspector
290289
railties (>= 5.0)
291290
websocket (1.2.3)
292-
websocket-driver (0.6.4)
291+
websocket-driver (0.6.5)
293292
websocket-extensions (>= 0.1.0)
294293
websocket-extensions (0.1.2)
295294
xpath (2.0.0)
@@ -330,7 +329,7 @@ DEPENDENCIES
330329
rails
331330
rails-html-sanitizer
332331
rainbow
333-
react_on_rails (~> 6.1)
332+
react_on_rails (~> 6.7.1)
334333
redis
335334
rspec-rails (~> 3)
336335
rspec-retry

Procfile.dev

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
rails: REACT_ON_RAILS_ENV=HOT rails s -b 0.0.0.0
88

99
# Run the hot reload server for client development
10-
hot-assets: sh -c 'rm app/assets/webpack/* || true && HOT_RAILS_PORT=3500 npm run hot-assets'
10+
hot-assets: sh -c 'rm app/assets/webpack/* || true && bundle exec rake react_on_rails:locale && HOT_RAILS_PORT=3500 npm run hot-assets'
1111

1212
# Render static client assets
1313
rails-static-client-assets: sh -c 'npm run build:dev:client'

Procfile.hot

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
rails: REACT_ON_RAILS_ENV=HOT rails s -b 0.0.0.0
77

88
# Run the hot reload server for client development
9-
hot-assets: sh -c 'rm app/assets/webpack/* || true && HOT_RAILS_PORT=3500 npm run hot-assets'
9+
hot-assets: sh -c 'rm app/assets/webpack/* || true && bundle exec rake react_on_rails:locale && HOT_RAILS_PORT=3500 npm run hot-assets'
1010

1111
# Keep the JS fresh for server rendering. Remove if not server rendering
1212
rails-server-assets: sh -c 'npm run build:dev:server'

Procfile.spec

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# in rails_helper.rb.
44

55
# Build client assets, watching for changes.
6-
rails-client-assets: sh -c 'npm run build:dev:client'
6+
rails-client-assets: sh -c 'bundle exec rake react_on_rails:locale && npm run build:dev:client'
77

88
# Build server assets, watching for changes. Remove if not server rendering.
99
rails-server-assets: sh -c 'npm run build:dev:server'

Procfile.static

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
rails: REACT_ON_RAILS_ENV= rails s -b 0.0.0.0
33

44
# Build client assets, watching for changes.
5-
rails-client-assets: rm app/assets/webpack/* || true && npm run build:dev:client
5+
rails-client-assets: rm app/assets/webpack/* || true && bundle exec rake react_on_rails:locale && npm run build:dev:client
66

77
# Build server assets, watching for changes. Remove if not server rendering.
88
rails-server-assets: npm run build:dev:server

Procfile.static.trace

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
rails: TRACE_REACT_ON_RAILS=TRUE rails s -b 0.0.0.0
33

44
# Build client assets, watching for changes.
5-
rails-client-assets: npm run build:dev:client
5+
rails-client-assets: bundle exec rake react_on_rails:locale && npm run build:dev:client
66

77
# Build server assets, watching for changes. Remove if not server rendering.
88
rails-server-assets: npm run build:dev:server

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ You can see this tutorial live here: [http://reactrails.com/](http://reactrails.
9292
- Enabling the use of npm modules and [Babel](https://babeljs.io/) with a Rails application using [Webpack](https://webpack.github.io/).
9393
- Easily enable retrofitting such a JS framework into an existing Rails app. You don't need a brand new single page app!
9494
- Example setting up Ruby and JavaScript linting in a real project, with corresponding CI rake tasks.
95+
- Enabling the i18n functionality with [react-intl](https://github.com/yahoo/react-intl).
9596

9697
### Technologies involved
9798

client/.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
node_modules
2+
3+
app/libs/i18n/translations.js
4+
app/libs/i18n/default.js

client/app/bundles/comments/actions/commentsActionCreators.js

+7
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,10 @@ export function submitComment(comment) {
7171
);
7272
};
7373
}
74+
75+
export function setLocale(locale) {
76+
return {
77+
type: actionTypes.SET_LOCALE,
78+
locale,
79+
};
80+
}

0 commit comments

Comments
 (0)