开发者

Rails: Hash#to_json - split over multiple lines?

开发者 https://www.devze.com 2023-01-06 15:25 出处:网络
I\'m having a slight annoyance with the javascript_I18n plugin, which generates js-friendly versions of the I18n translation tables, so that you can localise your javascript.It all works fine, but it

I'm having a slight annoyance with the javascript_I18n plugin, which generates js-friendly versions of the I18n translation tables, so that you can localise your javascript. It all works fine, but it works by calling to_json on each locale's translations hash and outputting the results into a file. When you call to_json on a hash the resulting string is all a single lne, which means you end up with files with an enormous line at the end.

This in turn is stopping git from being able to merge any changes, because the merge works on a line by line basis and can't deal with a single massive line with changes in the middle somewhere. It's not a massive problem because i can always just re-generate the js-friendly translation file with a rake task that comes with the plugin (which replaces the mid-merge file with a brand new one which i can just commit in), but it's just a bit annoying. It occurred to me that if the json was output on different lines, instead of all the same line, then it wouldn't be a problem, and it wouldn't even make the file that much b开发者_运维技巧igger, just inserting two characters (\n) per line.

Before i try and hack the resulting string with a gsub to split it onto seperate lines, is there a nicer way to call to_json on a hash and output the results onto seperate lines? Or a nicer way to solve this problem generally? (i can't find much useful in the documentation for javascript_I18n).

Grateful for any advice - max


Not to answer you question, but to give a suggestion:

It would probably be easier to just ignore all the generated js translation files.

This is because even if you separate the translation into multiple lines, there are still chances of merge conflicts, which you probably already resolved once in your yaml file.

I would set it up this way:

1). gitignore all the js translation files.

2). In ActionController, add a before filter to generate the js files automatically on each load in development mode only.

3). Tweak your deploy.rb file to generate the js files after code update.

No more merge conflicts! (at least for js translation files) :D

Aaron Qian

0

精彩评论

暂无评论...
验证码 换一张
取 消