在 Heroku 中将 UTF-8 设置为默认字符串编码 [英] Set UTF-8 as default string encoding in Heroku

查看:37
本文介绍了在 Heroku 中将 UTF-8 设置为默认字符串编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Heroku 中将默认的 ruby​​ 字符串编码更改为 UTF-8.出于某种原因,它是 US-ASCII.

<前>$ heroku 控制台myapp.heroku.com 的 Ruby 控制台>> "a".encoding=> #<编码:ASCII-8BIT>

但是,如果我在本地运行 irb 会得到不同的结果:

<前>$ irbruby-1.9.2-p136 :001 > "a".encoding=> #<编码:UTF-8>

两者都在 ruby​​ 1.9.2 上运行.我也试过设置这个,但没有用:

<前>Encoding.default_internal = Encoding.default_external = "UTF-8"

想法?

谢谢,菲利普

解决方案

根据 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=> #<编码:UTF-8>

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>

这篇关于在 Heroku 中将 UTF-8 设置为默认字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆