我已添加到我的项目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 版本