汉字编码问题 - Heroku

时间:2014-10-22 00:29:13

标签: heroku encoding utf-8 jruby

我有一个小型JRuby Sinatra应用,它使用Jieba for JRuby gem来标记中文句子。

在本地计算机上运行应用程序时,我没有任何编码问题:

curl --data "text=Ruby China,对!经是国内最权威的 Ruby 社区,拥有国内所有资深的 Ruby 工程师。&key=myKey" http://localhost:9292
# => ["Ruby", "China", "经是", "国内", "权威", "社区", "拥有", "所有", "资深", "工程师"]

然而,当我将应用程序推送到Heroku时,我遇到了编码问题:

curl --data "text=Ruby China,对!经是国内最权威的 Ruby 社区,拥有国内所有资深的 Ruby 工程师。&key=XWtrws324FGH675BV" http://secure-wildwood-3084.herokuapp.com
# => ["\"Ruby\", \"China\", \"  \", \" 。\""]

我试图强制将编码强制为UTF-8 .force_encoding("utf-8"),但仍有问题。我也尝试将Heroku上的LANG配置变量设置为LANG: zh_CN.UTF-8,但这没有什么区别。

我用于部署应用的方法位于README。这是将应用程序推送到Heroku时的输出:

Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
-----> Fetching custom git buildpack... done
-----> Java app detected
-----> Installing OpenJDK 1.6... done
-----> executing /app/tmp/cache/.maven/bin/mvn -B -Duser.home=/tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf -Dmaven.repo.local=/app/tmp/cache/.m2/repository  -DskipTests=true clean install
   [INFO] Scanning for projects...
   [INFO]
   [INFO] ------------------------------------------------------------------------
   [INFO] Building sinatra-jruby-heroku 1.0
   [INFO] ------------------------------------------------------------------------
   [INFO]
   [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jruby-heroku ---
   [INFO]
   [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jruby-heroku ---
   [INFO] Using 'UTF-8' encoding to copy filtered resources.
   [INFO] skip non existing resourceDirectory /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/src/main/resources
   [INFO]
   [INFO] --- jruby-rake-plugin:1.6.7:jruby (install-bundler) @ jruby-heroku ---
   [INFO] Successfully installed bundler-1.7.3
   [INFO] 1 gem installed
   [INFO]
   [INFO] --- jruby-rake-plugin:1.6.7:jruby (bundle-install) @ jruby-heroku ---
   [WARNING] JRuby limited openssl loaded. http://jruby.org/openssl
   [WARNING] gem install jruby-openssl for full support.
   [INFO] Fetching gem metadata from http://rubygems.org/..........
   [INFO] Rubygems 1.8.15 is not threadsafe, so your gems must be installed one at a time. Upgrade to Rubygems 2.0.7 or higher to enable parallel gem installation.
   [INFO] Installing jieba-jruby 0.0.1
   [INFO] Installing jruby-rack 1.1.4
   [INFO] Installing json 1.8.0
   [INFO] Installing rack 1.4.1
   [INFO] Installing rack-protection 1.2.0
   [INFO] Installing tilt 1.3.3
   [INFO] Installing sinatra 1.3.2
   [INFO] Installing trinidad_jars 1.0.2
   [INFO] Installing trinidad 1.3.4
   [INFO] Using bundler 1.7.3
   [INFO] Your bundle is complete!
   [INFO] Gems in the groups development and test were not installed.
   [INFO] It was installed into ./vendor/bundle
   [INFO]
   [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jruby-heroku ---
   [INFO] No sources to compile
   [INFO]
   [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jruby-heroku ---
   [INFO] Using 'UTF-8' encoding to copy filtered resources.
   [INFO] skip non existing resourceDirectory /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/src/test/resources
   [INFO]
   [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jruby-heroku ---
   [INFO] No sources to compile
   [INFO]
   [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jruby-heroku ---
   [INFO] Tests are skipped.
   [INFO]
   [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jruby-heroku ---
   [WARNING] JAR will be empty - no content was marked for inclusion!
   [INFO] Building jar: /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/target/jruby-heroku-1.0.jar
   [INFO]
   [INFO] --- maven-dependency-plugin:2.3:copy (default) @ jruby-heroku ---
   [INFO] Configured Artifact: org.jruby:jruby-complete:1.6.7:jar
   [INFO] Copying jruby-complete-1.6.7.jar to /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/target/dependency/jruby-complete.jar
   [INFO]
   [INFO] --- maven-install-plugin:2.4:install (default-install) @ jruby-heroku ---
   [INFO] Installing /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/target/jruby-heroku-1.0.jar to /app/tmp/cache/.m2/repository/br/com/tomasmuller/jruby-heroku/1.0/jruby-heroku-1.0.jar
   [INFO] Installing /tmp/build_bfb3b1dc-701f-4558-8560-9617ff535adf/pom.xml to /app/tmp/cache/.m2/repository/br/com/tomasmuller/jruby-heroku/1.0/jruby-heroku-1.0.pom
   [INFO] ------------------------------------------------------------------------
   [INFO] BUILD SUCCESS
   [INFO] ------------------------------------------------------------------------
   [INFO] Total time: 38.742 s
   [INFO] Finished at: 2014-10-17T22:39:20+00:00
   [INFO] Final Memory: 11M/513M
   [INFO] ------------------------------------------------------------------------
 -----> Discovering process types
   Procfile declares types -> console, jruby, rake, web
 -----> Compressing... done, 72.2MB
 -----> Launching... done, v20
   http://secure-wildwood-3084.herokuapp.com/ deployed to Heroku
 To git@heroku.com:secure-wildwood-3084.git
 504ab7d..2f4b7c0  master -> master

1 个答案:

答案 0 :(得分:1)

查看输出/项目它似乎使用了相当旧的JRuby零碎,在这种情况下最重要的是JRuby版本(你可能在本地有一个不同的版本): 1.6.7 https://github.com/diasks2/jieba-heroku/blob/master/pom.xml#L14

我会把它带到JRuby 1.7.16并捆绑更新以下输出中的其他内容:

[INFO] Installing jruby-rack 1.1.4
[INFO] Installing json 1.8.0
[INFO] Installing rack 1.4.1
[INFO] Installing rack-protection 1.2.0
[INFO] Installing tilt 1.3.3
[INFO] Installing sinatra 1.3.2
[INFO] Installing trinidad_jars 1.0.2
[INFO] Installing trinidad 1.3.4