Encoding issues in javascript files using rails asset pipeline
When loading a file, Ruby tries to "guess" its encoding. If no UTF-8 or any other non-ASCII characters are found, it uses US-ASCII as encoding for the file and throws an error if it suddenly encounters a non-ASCII character, which e.g. is loaded at run-time.
The best solution for this problem is to force Ruby to use a certain encoding by adding # encoding: utf-8
as the first line of a .rb file or <%# encoding: utf-8 %>
if it's a .erb file.
Encoding error in JavaScript asset in Rails
try renaming it to js.erb and placing <%# encoding: utf-8 %> at the top
found a hint here: Encoding issues in javascript files using rails asset pipeline
Asset pipeline encoding problems (UTF-8 vs. ASCII-8BIT) with external gem
Is the error from a WEBrick server or something else? Does rake assets:precompile
work? (don't forget to clear the assets after)
If the latter fails, double check the value of your $LANG environment variables is UTF-8 (with env
). If the rake task works but the app is failing then it could be the server env vars.
Rails 3.2.11 asset pipeline: accented characters in javascript get converted to question marks in production
OK, so I finally got it working by replacing Rhino with Node.js (I'm required to use jRuby so therubyracer was apparently not a good option either).
==============================================
UPDATE: It's been quite a while and I had pretty much forgotten about this post, but now I have come across it again I feel there's something I should mention: Although my original solution of using Node.js worked, it turned out to be more of an inadverted workaround than anything else.
As I found out some time later, the source of the problem was that, due to my lack of experience with Linux, I was pretty sure that the locale was set up properly when it wasn't at all. Once I configured my locale correctly, it fixed this and quite a few other locale-related problems I was experiencing.
Rails Asset Pipeline JS Compression Inserting Illegal Characters
It seems as though you're using \b
in some of your JavaScript regex, which I believe isn't supported. Try replacing \b
with \u0008
, E.x.
string.replace(/\u0008/g, '\\b')
Rails: Internationalization of Javascript Strings?
Why not something simple like:
<script type="text/javascript">
window.I18n = <%= I18n.backend.send(:translations).to_json.html_safe %>
</script>
Then in JS you can do things like:
I18n["en-US"]["alpha"]["bravo"];
I've wrapped mine in an application helper.
def current_translations
@translations ||= I18n.backend.send(:translations)
@translations[I18n.locale].with_indifferent_access
end
Then my call in my application.html.erb looks like this:
<script type="text/javascript">
window.I18n = <%= current_translations.to_json.html_safe %>
</script>
This allows you to avoid having to know the current locale in JavaScript.
I18n["alpha"]["bravo"];
Or
I18n.alpha.bravo;
Related Topics
Ruby Ternary Operator Structure
Scraping a Site That Requires Login Username and Password on Two Separate Pages
How to Stop Savon from Adding Prefixes to Soap.Body
Activerecord::Associationtypemismatch in Controller#Create on Dropdown Select for a Rails Self Join
Can't Install Ruby via Rvm, Error Running '_Rvm_Make -J4' on Ubuntu 22.04
Nested Form_For Singular Resource
Requiring a Ruby Gem in Ruby Script Breaks Cron Job Execution
How to Convert PDF to Excel or CSV in Rails 4
Rails Query Timestamp Between Two Hours
Ruby Looks for Class Variable in the Object Instead of Specific Class
Gem Install Rails Doesn't Work Due to Openssl/Etimedout in Windows
Using Delta Indexes for Associations in Thinking Sphinx
Performing a "Fast-Forward" Merge with Rugged
Single Table Inheritance to Refer to a Child Class with Its Own Fields