Why are all strings ASCII-8BIT after I upgraded to Rails 3?

后端 未结 3 667
無奈伤痛
無奈伤痛 2020-12-16 04:53

I upgraded to RoR 3.0.1 and Ruby to 1.9.2. Now all the strings in my views are ASCII-8BIT?

I believe I have my app set up to use UTF 8

相关标签:
3条回答
  • 2020-12-16 05:19

    I'm moving from Ruby 1.8.6 and Rails 2.3.5 to Ruby 1.9.2 and Rails 3.0.3, with postregsql. In order to get this working on my project, I had to do add this to the top of any of my view templates that were being translated:

    <% # coding: UTF-8 %>
    

    The rake task provided by Ole should be easy to modify to do this as well. I didn't find his solution as given had any effect, though.

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

    You need to add this to every .rb file:

    <% # coding: UTF-8 %>
    

    I use the gem magic_encoding for that.

    $ cd app/ 
    $ magic_encoding
    

    The default is UTF-8, but you can specify whatever you want as an argument.

    0 讨论(0)
  • 2020-12-16 05:39

    horrible issue. You need to put this at the top of each file

    # coding: UTF-8
    

    UPDATE Use the magic_encoding as described be Nerian.

    Does essentially same as the below, but better.

    /UPDATE

    I have a rake task I don't remember where I found (kudos to that guy!) which I have slightly modified, to have this on top of each file. I've heard people say the above(what you've done) should be sufficient, but it doesn't work for me...

    Anyhow, this is the rake task, just copy paste it


    lib/tasks/utf8encode.rake
    
    # coding: UTF-8
    
    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
    
    0 讨论(0)
提交回复
热议问题