Rails I18n of CSS file

前端 未结 3 1624
长情又很酷
长情又很酷 2020-12-16 06:09

I am trying to internationalize my site, and one thing is to use different font-size for different languages. Also some text-images need to be replaced as well.

I th

相关标签:
3条回答
  • 2020-12-16 06:30

    Your best bet in organization is to have different style sheets specific to localization, then set up a condition in your layout on what style sheets to render based of the locale.

    Just only put local specific style, and if you think about it...it shouldn't effect load times that much because I believe you are only changing font sizes.

    UPDATE from OP:

    Here is what I have configured to have this working:

    • I created a locales directory under app/assets/stylesheets
    • I put locale specific stylesheets inside, such as fr.sass
    • I setup the condition in the layouts/application.html.erb to reference the css files: <% if I18n.locale != :en %> <%= stylesheet_link_tag "locales/" + I18n.locale.to_s %> <% end %>
    • I setup the pre-compile rules in config/application.rb

    config.assets.precompile += 'locales/*.css'

    Note that I am white-listing the assets I want to compile into application.css, so the locale specific styles will not get into the application.css.

    0 讨论(0)
  • 2020-12-16 06:34

    You could also use locale-specific class attributes in html rendered. I think that is better/easier way to achieve what you want. Putting css in public is not so nice.

    0 讨论(0)
  • 2020-12-16 06:41

    I agree with Onno. I only needed very simple changes, so I added the locale as language tag, as described in this answer: https://stackoverflow.com/a/11577356/1822977

    Html:

    <html lang="<%= I18n.locale || 'en' %>">
    

    Sass:

    body {
        font-family:verdana,arial,helvetica,sans-serif;
        html[lang="jp"] & {
            font-family:"ヒラギノ角ゴ Pro W3", "Hiragino Kaku Gothic Pro", sans-serif;
         }
    }
    
    0 讨论(0)
提交回复
热议问题