Skip to content

Instantly share code, notes, and snippets.

@kml
Created December 13, 2016 22:42
Show Gist options
  • Save kml/6aa62936ea8c4def8b1ffced87951d54 to your computer and use it in GitHub Desktop.
Save kml/6aa62936ea8c4def8b1ffced87951d54 to your computer and use it in GitHub Desktop.
require "active_support/inflector"
# http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-titleize
module LocaleDependentTitleize
def titleize(word)
return humanize(underscore(word)) if I18n.locale == :pl
super
end
end
ActiveSupport::Inflector.prepend(LocaleDependentTitleize)
ActiveSupport::Inflector.singleton_class.prepend(LocaleDependentTitleize)
if __FILE__ == $PROGRAM_NAME
require "minitest/autorun"
describe "titleize" do
let(:string) { "test_string_id" }
describe "pl locale" do
before do
I18n.available_locales = [:pl, :en]
I18n.locale = :pl
end
it "capitalize only first word" do
string.titleize.must_equal "Test string"
ActiveSupport::Inflector.titleize(string).must_equal "Test string"
Class.new { include ActiveSupport::Inflector }.new.titleize(string).must_equal "Test string"
end
end
describe "en locale" do
before do
I18n.locale = :en
end
it "capitalize all words" do
string.titleize.must_equal "Test String"
ActiveSupport::Inflector.titleize(string).must_equal "Test String"
Class.new { include ActiveSupport::Inflector }.new.titleize(string).must_equal "Test String"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment