【发布时间】:2016-10-10 00:14:20
【问题描述】:
我已将react-rails gem 添加到我的项目中,我想将其用于已翻译的组件。
我无法放入预编译的资产 erb 模板,但我仍在尝试创建组件,使它们在所有项目中可用,然后通过一些翻译部分使用它们。
工作场景
# app/view/example/_react_component.coffee.erb
DOM = React.DOM
FormInput = React.createClass
displayName: "FormInput"
render: ->
DOM.div
className: 'row control-group'
DOM.div
className: 'form-group col-xs-12 floating-label-form-group controls'
DOM.label
htmlFor: @props.id
@props.label
DOM.input
id: @props.id
className: 'form-control'
placeholder: @props.placeholder
type: @props.type
DOM.p
className: 'help-block text-danger'
formInput = React.createFactory(FormInput)
window.ValidationFormInput = React.createClass
displayName: "ValidationFormInput"
getInitialState: ->
{ }
render: ->
formInput
id: "<%= t('validation_form.id') %>"
label: "<%= t('validation_form.label') %>"
placeholder: "<%= t('validation_form.placeholder') %>"
type: 'text'
validationFormInput = React.createFactory(ValidationFormInput)
# app/view/example/index.html.erb
<%= react_component('ValidationFormInput', {}, {class: "container"}) %>
所需的场景(不工作)
# app/assets/javascripts/components/form_input.coffee
DOM = React.DOM
FormInput = React.createClass
displayName: "FormInput"
render: ->
DOM.div
className: 'row control-group'
DOM.div
className: 'form-group col-xs-12 floating-label-form-group controls'
DOM.label
htmlFor: @props.id
@props.label
DOM.input
id: @props.id
className: 'form-control'
placeholder: @props.placeholder
type: @props.type
DOM.p
className: 'help-block text-danger'
formInput = React.createFactory(FormInput)
# app/view/example/_react_component.coffee.erb
window.ValidationFormInput = React.createClass
displayName: "ValidationFormInput"
getInitialState: ->
{ }
render: ->
formInput
id: "<%= t('validation_form.id') %>"
label: "<%= t('validation_form.label') %>"
placeholder: "<%= t('validation_form.placeholder') %>"
type: 'text'
validationFormInput = React.createFactory(ValidationFormInput)
# app/view/example/index.html.erb
<%= react_component('ValidationFormInput', {}, {class: "container"}) %>
我猜这个问题与我的组件定义的范围有关,但我不知道如何使组件可用于任何部分。
提前谢谢你
编辑
为了提供翻译,我找到了gem I18n-js。安装后,我可以轻松运行 rake 任务来创建我的config/locales/* 翻译的 js 版本
【问题讨论】:
-
注意:不惜一切代价避免使用 js.erb 和 coffee.erb =) 在下面查看我的答案。
标签: ruby-on-rails-4 coffeescript rails-i18n react-rails