You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+3
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,9 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]
6
6
## [Unreleased]
7
7
*Please add entries here for your pull requests.*
8
8
9
+
##### Changed
10
+
- Automatically generate __i18n__ javascript files for `react-intl` when the serve starts up. [#642](https://github.com/shakacode/react_on_rails/pull/642) by [JasonYCHuang](https://github.com/JasonYCHuang).
11
+
9
12
## [6.3.4] - 2016-12-25
10
13
##### Fixed
11
14
- Disable Turbolinks support when not supported. [#650](https://github.com/shakacode/react_on_rails/pull/650) by [ka2n](https://github.com/ka2n).
Copy file name to clipboardExpand all lines: README.md
+99-11
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
**For a complete example of this gem, see our live demo at [www.reactrails.com](http://www.reactrails.com). ([Source Code](https://github.com/shakacode/react-webpack-rails-tutorial))**
4
4
5
-
Aloha from Justin Gordon ([bio](http://www.railsonmaui.com/about)) and the [ShakaCode](http://www.shakacode.com) Team! We're actively looking for new projects involving React, React-Native, and Rails. Please contact me at [[email protected]](mailto:[email protected]) if we could potentially help you in any way. Besides consulting on bigger projects, [ShakaCode](http://www.shakacode.com) is doing ScreenHero plus Slack/Github based coaching for React on Rails. See our blog post [Can ShakaCode Help You?](https://blog.shakacode.com/can-shakacode-help-you-4a5b1e5a8a63#.jex6tg9w9) for more information.
6
-
7
5
We're offering a free half-hour project consultation, on anything from React on Rails to any aspect of web application development for both consumer and enterprise products. In addition to React.js and Rails, we're doing React-Native iOS and Android apps!
8
6
9
7
Whether you have a new project or need help on an existing project, feel free to contact me directly at [[email protected]](mailto:[email protected]) and thanks in advance for any referrals!
@@ -50,6 +48,7 @@ React on Rails integrates Facebook's [React](https://github.com/facebook/react)
-[Including your React Component in your Rails Views](#including-your-react-component-in-your-rails-views)
51
+
-[I18n](#i18n)
53
52
+[How it Works](#how-it-works)
54
53
-[Client-Side Rendering vs. Server-Side Rendering](#client-side-rendering-vs-server-side-rendering)
55
54
-[Building the Bundles](#building-the-bundles)
@@ -168,7 +167,104 @@ Configure the `config/initializers/react_on_rails.rb`. You can adjust some neces
168
167
// inside your React component
169
168
this.props.name // "Stranger"
170
169
```
171
-
170
+
171
+
### I18n
172
+
173
+
You can enable the i18n functionality with [react-intl](https://github.com/yahoo/react-intl). ReactOnRails also converts traditional Rails locale files, `*.yml`, to required javascript files, `translations.js` & `default.js`, automatically.
174
+
175
+
For more detail, you can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial)
176
+
177
+
1. Add `react-intl` to `client/package.json`, and remember to `bundle && npm install`
178
+
179
+
```js
180
+
"dependencies": {
181
+
...
182
+
"intl": "^1.2.5",
183
+
"react-intl": "^2.1.5",
184
+
...
185
+
}
186
+
```
187
+
188
+
2. In `client/webpack.client.base.config.js`, set `react-intl` as an entry point.
189
+
190
+
```js
191
+
module.exports = {
192
+
...
193
+
entry: {
194
+
...
195
+
vendor: [
196
+
...
197
+
'react-intl',
198
+
],
199
+
...
200
+
```
201
+
202
+
3. ReactOnRails uses locale files as you did before in Rails: `config/locales/*.yml`. Therefore, you don't need to create additional local files.
203
+
204
+
4. Update settings in `config/initializers/react_on_rails.rb` to what you need:
205
+
206
+
```ruby
207
+
# Replace the following line to the location where you keep translation.js & default.js.
5. Add following lines to `config/application.rb`, this will help you to generate `translations.js` & `default.js` automatically when you starts the server.
216
+
217
+
```js
218
+
module YourModule
219
+
class Application < Rails::Application
220
+
...
221
+
config.after_initialize do
222
+
ReactOnRails::LocalesToJs.new.convert
223
+
end
224
+
end
225
+
end
226
+
```
227
+
228
+
6. In React, you need to initialize `react-intl`, and set parameters for it.
229
+
230
+
For more detail, you can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial)
231
+
232
+
>`translations.js`: All your locales in json format.
233
+
>
234
+
>`default.js`: [1] `defaultLocale` is your default locale, like "en". [2] `defaultMessages` is the place where you can get your local values with localeKeyInCamelForm, and it also contains fallback when something went wrong.
235
+
>
236
+
>There is no need to track and lint `translations.js`&`default.js`, and you can add them to `.gitignore`and`.eslintignore`.
237
+
238
+
```js
239
+
...
240
+
import { addLocaleData } from 'react-intl';
241
+
import en from 'react-intl/locale-data/en';
242
+
import de from 'react-intl/locale-data/de';
243
+
import { translations } from 'path_to/i18n/translations';
244
+
import { defaultLocale } from 'path_to/i18n/default';
AllJavaScriptinReactOnRails is loaded from npm: [react-on-rails](https://www.npmjs.com/package/react-on-rails). To manually install this (you did not use the generator), assuming you have a standard configuration, run this command:
174
270
@@ -241,11 +337,6 @@ The `railsContext` has: (see implementation in file [react_on_rails_helper.rb](a
serverSide: boolean # Are we being called on the server or client? NOTE, if you conditionally
251
342
# render something different on the server than the client, then React will only show the
@@ -257,9 +348,6 @@ The `railsContext` has: (see implementation in file [react_on_rails_helper.rb](a
257
348
##### Needing the current url path for server rendering
258
349
Suppose you want to display a nav bar with the current navigation link highlighted by the URL. When you server render the code, you will need to know the current URL/path if that is what you want your logic to be based on. The new `railsContext` has this information so the application of an "active" class can be done server side.
259
350
260
-
##### Needing the I18n.locale
261
-
Suppose you want to server render your react components with localization applied given the current Rails locale. The `railsContext` contains the I18n.locale.
262
-
263
351
##### Configuring different code for server side rendering
264
352
Suppose you want to turn off animation when doing server side rendering. The `serverSide` value is just what you need.
0 commit comments