Ruby on Rails locales in .rb extension - ruby-on-rails

Taken from the rails guide:
You may use YAML (.yml) or plain Ruby (.rb) files for storing your translations in SimpleStore. YAML is the preferred option among Rails developers. However, it has one big disadvantage. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly.
Im trying to load my i18n locale files in a .rb files since the spanish accents in the .yml file end up throwing a I18n::InvalidLocaleData error.
However Ive tried copy pasting the file into a .rb and it doesnt work. How is this supposed to be done?

One option would be to do it like this:
{ de: {
errors: {
messages: {
restrict_dependent_destroy: {
one: "Datensatz kann nicht gelöscht werden, da ein abhängiger %{record}-Datensatz existiert.",
many: "Datensatz kann nicht gelöscht werden, da abhängige %{record} existieren." }}}}}
Where %{record} would be interpolated.

Related

RoR - Download ZIP file using gmail gem

I am trying to set up some rake tasks. It requires me to connect to gmail and download a Zip file which is sent as an attachment.
I have written the following code(which works fine for downloading csv) -
gmail = Gmail.connect(ENV["USERNAME"], ENV["PASSWORD"])
msg = gmail.inbox.find(from: ENV["REC_USER"],
subject: args[:subject])
dir_path = "lib/mfu_payment_data/"
Dir.mkdir dir_path unless File.exists?(dir_path)
if msg.first
msg.first.attachments.each do |attachment|
File.write(File.join(dir_path,attachment.filename),attachment.body.decoded)
end
end
It throws the following error -
rake aborted!
Encoding::UndefinedConversionError: "\xED" from ASCII-8BIT to UTF-8
I assume that this has got something to do with the attachment.body.decoded, but I do not know how else to do this.
You can try writing the file in binary mode:
File.open('/path/to/file;, 'wb') { |file| file.write(attachment.body.decoded) }
"b" Binary file mode
Suppresses EOL <-> CRLF conversion on Windows. And
sets external encoding to ASCII-8BIT unless explicitly
specified.
The modes are described in the IO class which File inherits from.
I think you have many option to generate zip file
Download and unzip

Translation in different language

I have an application with different languages.I have translated the word Logout ** in spanish language.The translation for **Logout in spanish is Cerrar sesión.But when it is translated it gives Cerrar sesi�n.please Help.How can i overcome from this. It gives � when any spanish word have some sign upon the letter.
this is encoding problem, check if all the files or resources are saved or served as UTF-8 and if utf-8 encoding is defined in the page
Define alerts you want in Several language like this,
alert_indiff_language = {
"english" : "Are you sure you want to delete?",
"Dutch" : "Sind Sie sicher, dass Sie löschen möchten?"
}
your_function() {
alert(alert_indiff_language[language])
}

Rails CSV file upload character encoding issues

I know there are a lot of threads about this already, but none of the solutions suggested do not seem to work for me for some reason...
I am using:
Ruby 1.9.2
Rails 2.3.8
My users author CSV files in MS Excel and then need to upload these files to the web application. My web application and the database backend uses UTF-8 and all special characters, such as the £ sign, get corrupted on upload.
I am reading in the file like this:
#file = params[:import_file][:uploaded_data]
Then get encoding of the file using:
source_encoding = "UTF-8"
if #file.external_encoding
source_encoding = #file.external_encoding.name
end
For my test file the source encoding value is ASCII-8BIT.
Then I try to do:
#file.each {|line|
print "#{line.force_encoding(source_encoding).encode!("UTF-8") }\n"
}
in order to see if all texts are displaying ok. However this gives me error like this:
"\xA3" from ASCII-8BIT to UTF-8
If I am trying to read the CSV with:
dataArray = CSV.read(#file, encoding: source_encoding)
No errors this time, but all special characters go as ? characters.
Any pointers where I might be going wrong or is importing CSV file authored with MS Excel just a mission impossible?
Regards,
Olli

Firefox addon localization

I have problem with localization my addon. I followed this tutorial on Using Localized Strings in Preferences but I can't compile my addon because I use polish characters ć and others.
I've made locale folder and put there pl-PL.properties file with this content:
my_tag_title = Co robić?
and I got error:
Following locale file is not a valid UTF-8 file: C:\path\pl-PL.properties
'utf8' codec can't decode byte 0xe6 in position 22: invalid continuation byte"
Is there way to put special characters directly inside package.json?
How to solve this problem?
Make sure that the locale file is saved in UTF-8 format.

Character Encoding issue in Rails v3/Ruby 1.9.2

I get this error sometimes "invalid byte sequence in UTF-8" when I read contents from a file. Note - this only happens when there are some special characters in the string. I have tried opening the file without "r:UTF-8", but still get the same error.
open(file, "r:UTF-8").each_line { |line| puts line.strip(",") } # line.strip generates the error
Contents of the file:
# encoding: UTF-8
290919,"SE","26","Sk‰l","",59.4500,17.9500,, # this errors out
290956,"CZ","45","HornÌ Bradlo","",49.8000,15.7500,, # this errors out
290958,"NO","02","Svaland","",58.4000,8.0500,, # this works
This is the CSV file I got from outside and I am trying to import it into my DB, it did not come with "# encoding: UTF-8" at the top, but I added this since I read somewhere it will fix this problem, but it did not. :(
Environment:
Rails v3.0.3
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]
Ruby has a notion of an external encoding and internal encoding for each file. This allows you to work with a file in UTF-8 in your source, even when the file is stored in a more esoteric format. If your default external encoding is UTF-8 (which it is if you're on Mac OS X), all of your file I/O is going to be in UTF-8 as well. You can check this using File.open('file').external_encoding. What you're doing when you opening your file and passing "r:UTF-8" is forcing the same external encoding that Ruby is using by default.
Chances are, your source document isn't in UTF-8 and those non-ascii characters aren't mapping cleanly to UTF-8 (if they were, you would either get the correct characters and no error, and if they mapped by incorrectly, you would get incorrect characters and no error). What you should do is try to determine the encoding of the source document, then have Ruby transcode the document on read, like so:
File.open(file, "r:windows-1251:utf-8").each_line { |line| puts line.strip(",") }
If you need help determining the encoding of the source, give this Python library a whirl. It's based on the automatic charset detection fallback that was in Seamonkey/Mozilla (and is possibly still in Firefox).
If you want to change your file encoding, you can use gem 'charlock holmes'
https://github.com/brianmario/charlock_holmes
$require 'charlock_holmes/string'
content = File.read('test2.txt')
if !content.is_utf8?
detection = CharlockHolmes::EncodingDetector.detect(content)
utf8_encoded_content = CharlockHolmes::Converter.convert content, detection[:encoding], 'UTF-8'
end
Then you can save your new content in a temp file and overwrite your original file.
Hope this help.

Resources