Rails : How to put I18N into SVG file
Rails : How to put I18N into SVG file
I want to internationalize several SVG files but I have no idea how I could do that.
Here's my SVG file :
<text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF">
<tspan x="39" y="157">Accessoires</tspan>
</text>
Should I do something like :
<text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF">
<tspan x="39" y="157"><%= t('accessoires') %></tspan>
</text>
Or is there an other way to do it ?
Thanks for your answers.
1 Answer
1
I know three ways to build svg with parameter. The first one is save it as string and then call html_safe
. The second way use object tag according to W3C SVG with parameters
html_safe
<object type="image/svg+xml" data="text.svg" class="logo">
<param text="<%= t('accessoires') %>">
</object>
<text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF">
<tspan x="39" y="157" content-value="param(label)" />
</text>
Third way is use raw svg or partial _my_svg.html.erb
:
_my_svg.html.erb
<text font-family="OpenSans-Semibold, Open Sans" font-size="22" font-weight="500" fill="#FFFFFF">
<tspan x="39" y="157" content-value="<%= t('accessoires') %>" />
</text>
Hi, thanks for your answer, but what do you mean by save it as string and call
html_safe
? Should it looks like <tspan x="39" y="157">t('Accessoires')</tspan>.html_safe
?– Enner31
Jul 4 at 7:45
html_safe
<tspan x="39" y="157">t('Accessoires')</tspan>.html_safe
It should be
"<svg><tspan x="39" y="157">#{t('Accessoires')}</tspan></svg>".html_safe
. You need to interpolate I18n to string before converting to html.– Artem Dorodovskyi
Jul 4 at 9:52
"<svg><tspan x="39" y="157">#{t('Accessoires')}</tspan></svg>".html_safe
I think best way is third. You create partial (html.erb file) where you copy your svg code and just add erb code where you need.
– Artem Dorodovskyi
Jul 4 at 9:57
Should I create the partial into the same folder ? In ``app/assets/images ``` ? But how do I add erb code ? I tried to do the way you explained to me but do not seems to work...
– Enner31
Jul 4 at 14:23
You don't need to save svg to assets. Create file
_accessoires.html.erb
in views/layouts
folder. Then you can use your svg by adding to view <%= render 'layouts/accessoires' %>
– Artem Dorodovskyi
Jul 4 at 14:35
_accessoires.html.erb
views/layouts
<%= render 'layouts/accessoires' %>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@RobertLongson what kind of joke is that ?
– Enner31
Jul 2 at 13:07