Last active
December 15, 2015 10:49
-
-
Save henrik/5248086 to your computer and use it in GitHub Desktop.
Working sketch of Rails i18n syntax for more easily passing in formatted (in your templating language) values, and also using multiple keys under the same scope (which is a good idea). Also see [a previous sketch with similar ideas](https://gist.github.com/henrik/4605425).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= t_scope(:"my.example.text") do |scope| | |
- scope.world do | |
span.worldly= my_world_helper | |
- scope.sign_up do | |
= link_to(scope.t(:sign_up), sign_up_path) | |
- scope.log_in do | |
= link_to(scope.t(:log_in), log_in_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
my: | |
example: | |
text: "Hello %{world}! Please %{sign_up} or %{log_in}!" | |
sign_up: "sign up" | |
log_in: "log in" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module I18nHelper | |
class Scope | |
def initialize(base_key, helper) | |
@base_key = base_key | |
@helper = helper | |
@hash = {} | |
end | |
def to_hash | |
hash | |
end | |
def t(key, opts = {}) | |
helper.t("#{base_key}.#{key}", opts) | |
end | |
def method_missing(name, *args, &block) | |
html = helper.capture(&block) | |
hash[name] = html | |
end | |
private | |
attr_reader :base_key, :helper, :hash | |
end | |
def t_scope(key) | |
base_key = key.to_s.rpartition(".").first | |
scope = Scope.new(base_key, self) | |
yield(scope) | |
t(key, scope.to_hash).html_safe | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
require "i18n_helper" | |
describe I18nHelper do | |
let(:view) { double.extend(described_class) } | |
# Not very integrated, but request tests will do that. | |
describe "#t_scope" do | |
it "can abbreviate scopes" do | |
view.should_receive(:t).with("foo.bar.text", {}). | |
and_return("whatever") | |
view.should_receive(:t).with("foo.bar.baz", two: 2) | |
view.t_scope("foo.bar.text") do |scope| | |
scope.t(:baz, two: 2) | |
end | |
end | |
it "can capture and interpolate view blocks" do | |
view.stub(:capture) { |&block| block.call } | |
view.should_receive(:t).with("foo.hello", world: "a view"). | |
and_return("whatever") | |
view.t_scope("foo.hello") do |scope| | |
scope.world do | |
"a view" | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment