Created
December 12, 2010 19:03
-
-
Save francesc/738245 to your computer and use it in GitHub Desktop.
Manage the UTF-8 encoding header for ruby 1.9
This file contains 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
desc "Manage the encoding header of Ruby files" | |
task :utf8_encode_headers => :environment do | |
files = Array.new | |
["*.rb", "*.rake"].each do |extension| | |
files.concat(Dir[ File.join(Dir.getwd.split(/\\/), "**", extension) ]) | |
end | |
files.each do |file| | |
content = File.read(file) | |
next if content[0..16] == "# coding: UTF-8\n\n" || | |
content[0..22] == "# -*- coding: utf-8 -*-" | |
["\n\n", "\n"].each do |file_end| | |
content = content.gsub(/(# encoding: UTF-8#{file_end})|(# coding: UTF-8#{file_end})|(# -*- coding: UTF-8 -*-#{file_end})|(# -*- coding: utf-8 -*-#{file_end})/i, "") | |
end | |
new_file = File.open(file, "w") | |
new_file.write("# coding: UTF-8\n\n"+content) | |
new_file.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could also include the follow
Encoding.default_external = Encoding::UTF_8 if RUBY_VERSION > "1.9"
https://rails.lighthouseapp.com/projects/8994/tickets/2188-i18n-fails-with-multibyte-strings-in-ruby-19-similar-to-2038#ticket-2188-60