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
+2-1
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,8 @@ All notable changes to this project's source code will be documented in this fil
4
4
Contributors: please follow the recommendations outlined at [keepachangelog.com](http://keepachangelog.com/). Please use the existing headings and styling as a guide, and add a link for the version diff at the bottom of the file. Also, please update the `Unreleased` link to compare to the latest release version.
5
5
6
6
## [Unreleased]
7
-
*Please add entries here for your pull requests.*
7
+
##### Changed
8
+
- 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).
Copy file name to clipboardExpand all lines: README.md
+101-11
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
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.
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
6
7
7
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
8
@@ -50,6 +50,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)
53
+
-[I18n](#i18n)
53
54
+[How it Works](#how-it-works)
54
55
-[Client-Side Rendering vs. Server-Side Rendering](#client-side-rendering-vs-server-side-rendering)
55
56
-[Building the Bundles](#building-the-bundles)
@@ -168,7 +169,104 @@ Configure the `config/initializers/react_on_rails.rb`. You can adjust some neces
168
169
// inside your React component
169
170
this.props.name // "Stranger"
170
171
```
171
-
172
+
173
+
### I18n
174
+
175
+
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.
176
+
177
+
For more detail, you can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial)
178
+
179
+
1. Add `react-intl` to `client/package.json`, and remember to `bundle && npm install`
180
+
181
+
```js
182
+
"dependencies": {
183
+
...
184
+
"intl": "^1.2.5",
185
+
"react-intl": "^2.1.5",
186
+
...
187
+
}
188
+
```
189
+
190
+
2. In `client/webpack.client.base.config.js`, set `react-intl` as an entry point.
191
+
192
+
```js
193
+
module.exports = {
194
+
...
195
+
entry: {
196
+
...
197
+
vendor: [
198
+
...
199
+
'react-intl',
200
+
],
201
+
...
202
+
```
203
+
204
+
3. ReactOnRails uses locale files as you did before in Rails: `config/locales/*.yml`. Therefore, you don't need to create additional local files.
205
+
206
+
4. Update settings in `config/initializers/react_on_rails.rb` to what you need:
207
+
208
+
```ruby
209
+
# 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.
218
+
219
+
```js
220
+
module YourModule
221
+
class Application < Rails::Application
222
+
...
223
+
config.after_initialize do
224
+
ReactOnRails::LocalesToJs.new.convert
225
+
end
226
+
end
227
+
end
228
+
```
229
+
230
+
6. In React, you need to initialize `react-intl`, and set parameters for it.
231
+
232
+
For more detail, you can refer to [react-webpack-rails-tutorial](https://github.com/shakacode/react-webpack-rails-tutorial)
233
+
234
+
>`translations.js`: All your locales in json format.
235
+
>
236
+
>`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.
237
+
>
238
+
>There is no need to track and lint `translations.js`&`default.js`, and you can add them to `.gitignore`and`.eslintignore`.
239
+
240
+
```js
241
+
...
242
+
import { addLocaleData } from 'react-intl';
243
+
import en from 'react-intl/locale-data/en';
244
+
import de from 'react-intl/locale-data/de';
245
+
import { translations } from 'path_to/i18n/translations';
246
+
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
272
@@ -241,11 +339,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
344
# render something different on the server than the client, then React will only show the
@@ -257,9 +350,6 @@ The `railsContext` has: (see implementation in file [react_on_rails_helper.rb](a
257
350
##### Needing the current url path for server rendering
258
351
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
352
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
353
##### Configuring different code for server side rendering
264
354
Suppose you want to turn off animation when doing server side rendering. The `serverSide` value is just what you need.
265
355
@@ -473,7 +563,7 @@ If you are using [jquery-ujs](https://github.com/rails/jquery-ujs) for AJAX call
473
563
## Integration with Node
474
564
Node.js can be used as the backend for server-side rendering instead of [execJS](https://github.com/rails/execjs). Before you try this, consider the tradeoff of extra complexity with your deployments versus *potential* performance gains. We've found that using ExecJS with [mini_racer](https://github.com/discourse/mini_racer) to be "fast enough" so far. That being said, we've heard of other large websites using Node.js for better server rendering performance. See [Node.js for Server Rendering](docs/additional-reading/node-server-rendering.md) for more information.
475
565
476
-
## Additional Documentation
566
+
## Additional Documentation
477
567
**Try out our new [Documentation Gitbook](https://shakacode.gitbooks.io/react-on-rails/content/) for improved readability & reference!**
0 commit comments