且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Heroku 中将 UTF-8 设置为默认字符串编码

更新时间:2022-12-05 16:59:31

根据 Heroku 支持人员的说法,这很神奇:

heroku 配置:添加 LANG=en_US.UTF-8

尽管 heroku 控制台 将继续报告编码为 ASCII-8BIT 的字符串,但您的实际应用程序将以正确的编码运行,基于 LANG代码> 配置变量.您可以通过执行以下操作来仔细检查:

$ heroku 运行 bash运行 bash 附加到终端... up, run.2u20415@022e95bf-3ab6-4291-97b1-741f95e7fbda:/app$ irbirb(main):001:0> "a".encoding=> #

I need to change the default ruby string encoding to UTF-8 in Heroku. For some reason it is US-ASCII.

$ heroku console
Ruby console for myapp.heroku.com 
>> "a".encoding 
=> #<Encoding:ASCII-8BIT>

However, if I run irb locally I get a different result:

$ irb 
ruby-1.9.2-p136 :001 > "a".encoding 
=> #<Encoding:UTF-8>

Both run on ruby 1.9.2. I've tried setting this as well, but didn't work:

Encoding.default_internal = Encoding.default_external = "UTF-8"

Ideas?

Thanks, Felipe

As per the Heroku support staff, this is the magic thing:

heroku config:add LANG=en_US.UTF-8

Although heroku console will keep reporting strings encoding as ASCII-8BIT, your actuall app will be running with the correct encoding, based on the LANG config var. You can double check that by doing this:

$ heroku run bash
Running bash attached to terminal... up, run.2
u20415@022e95bf-3ab6-4291-97b1-741f95e7fbda:/app$ irb
irb(main):001:0> "a".encoding
=> #<Encoding:UTF-8>