ruby syntax error with multiple language in hash

2019-09-07 17:56发布

The following hash is causing a syntax error. I assume it is because of the funky characters.

Any way to fix this? I'm using macvim, in case that matters.

  {
    :en => ['English',  'en_US'], 
    :es => ['español',  'es_MX'],
    :fr => ['français', 'fr_FR'],
    :de => ['Deutsch',  'de_DE'],
    :ru => ['русский',  'ru_RU'],
    :zh => ['中国的',   'zh_CN'],
    :ar => ['العربية',   'ar_AR'],
  }

2条回答
相关推荐>>
2楼-- · 2019-09-07 18:44

If this is Ruby 1.9, then you can set the magic comment to tell Ruby this is a UTF8 file instead of ASCII:

How does the magic comment ( # Encoding: utf-8 ) in ruby​​ work?

查看更多
Animai°情兽
3楼-- · 2019-09-07 18:57

You could always escape your unicode values.

{
  :en => ['English',  'en_US'], 
  :es => ['espa\u00F1ol',  'es_MX'],
  :fr => ['fran\u00E7ais', 'fr_FR'],
  :de => ['Deutsch',  'de_DE'],
  :ru => ['\u0440\u0443\u0441\u0441\u043A\u0438\u0439',  'ru_RU'],
  :zh => ['\u4E2D\u56FD\u7684',   'zh_CN'],
  :ar => ['\u0627\u0644\u0639\u0631\u0628\u064A\u0629',   'ar_AR'],
}
查看更多
登录 后发表回答