I could create one CSS for each locale, as suggested in this answer , but since the CSS is the same, except for the I18n bit, I would succeed: / p>
A folder with lots of static CSS / LESS with an embedded language, for example:
.observation { .box("Observation"); }
Lots of exactly identical dynamic CSSs like
html[lang=en] { .observation { .box("Observation") } }
Instead, I decided to create a CSS and ERB view and deliver using cached pages with locale code in the URL, so there is no duplication. See code below.
config / routes.rb
X::Application.routes.draw do get 'index.:locale.:format' => 'css#index', constraints: { locale: /[az]{2}(-[AZ]{2})?/, format: 'css' } end
application / controllers / css_controller.rb
class CssController < ActionController::Base caches_page :index def index @locale = params[:locale] end end
app / views / CSS / index.css.less.erb
@import "mixins"; .observation { .box("<%= t 'box.observation', locale: @locale %>"); }
Application / assets / style sheets /mixins.less
.box (@legend) { &:after { content: @legend; } }
This example will work as if it were a simple ERB view , but since it uses Less, I have to manually analyze ERB and LESS using rails 4:
class CssController < ActionController::Base caches_page :index def index @locale = params[:locale] erb_source = find_template.source less_source = Tilt::ERBTemplate.new { erb_source }.render(self) css_source = Less::Parser.new(paths: Rails.application.config.less.paths).parse(less_source).to_css render text: css_source end private def find_template(_action_name = action_name) lookup_context.disable_cache { lookup_context.find_template(_action_name, lookup_context.prefixes) } end end
source share