Is "Products" a reserved word in YAML in Rails? - ruby-on-rails

This is an extremely bizarre one. In my en and es.yml localization files, I have this block for the products controller:
products:
word: Products
show:
exam: "Exam:"
related: "People who bought this, were also interested in..."
buy_now: Buy Now
Now, this works 100% when the site is in English, but when it's in Spanish using the es.yml locale I get a missing translations error!? I've checked the spacing, indentation, possible repeats of a products key, etc and nothing. When I try to reference it directly (i.e. <%= t('products.show.exam') %>) it still does not work.
If, however, I change the key to ANYTHING except "products" and then reference it directly, it works fine.
Has anyone ever seen anything like this? Im in Rails 2.3.12 BTW.

The word "Products" only occurs once, in a comment in the YAML documentation. So, it's not a reserved directive in YAML:
http://yaml.org/spec/1.2/spec.html
It is not in the Ruby specification either, so it's not a Ruby reserved word:
http://ruby-doc.org/stdlib-1.9.3/
I also checked the spanish version of Ruby documentation, which just directed me to the English documentation:
http://www.ruby-lang.org/es/documentation/

Related

Prevent Ruby from changing & to &?

I need to display some superscript and subscript characters in my webpage title. I have a helper method that recognizes the pattern for a subscript or superscript, and converts it to &sub2; or ²
However, when it shows up in the rendered page's file, it shows up in the source code as:
&sub2;
Which is not right. I have it set up to be:
<% provide(:title, raw(format_title(#hash[:page_title]))) %>
But the raw is not working. Any help is appreciated.
Method:
def format_title(name)
label = name
if label.match /(_[\d]+_)+|(~[\d]+~)+/
label = label.gsub(/(_([\d]+)_)+/, '&sub\2;')
label = label.gsub(/(~([\d]+)~)+/, '&sup\2;')
label.html_safe
else
name
end
end
I have even tried:
str.gsub(/&/, '&')
but it gives back:
&amp;sub2;
You can also achieve this with Rails I18n.
<%= t(:page_title_html, scope: [:title]) %>
And in your respective locale file. (title.en.yml most probably):
title:
page_title: "Title with ²"
Here is a chart for HTML symbols regarding subscript and superscripts.
For more information check Preventing HTML character entities in locale files from getting munged by Rails3 xss protection
Update:
In case you need to load the page titles dynamically, first, you'll have to install a gem like Page Title Helper.
You can follow the guide in the gem documentation.
There are two of issues with your example, one is of matter and the other is just a coincidence.
The first issue is you are trying to use character entities that do not actually exist. Specifically, there are only ¹, ² and ³ which provide 1, 2 and 3 superscript symbols respectively. There is no such character entity as &sup4; nor any other superscript digits. There are though bare codepoints for other digits which you can use but this would require a more involved code.
More importantly, there are no subscript character entities at all in HTML5 character entities list. All subscript digits are bare codepoints. Therefore it makes no sense to replace anything with &sub2; or any other "subscript" digit.
The reason you didn't see your example working is due to the test string you chose. Supplying anything with underscores, like _2_mystring will be properly replaced with &sub2;. As &sub2; character entity is non-existent, the string will appear as is, creating an impression that raw method somehow doesn't work.
Try to use ~2~mystring and it will be replaced with the superscript character entity ² and will be rendered correctly. This illustrates that your code correct, but the basic assumption about character entities is not.

Should I use unique ids or the whole sentences when localizing software?

I need to translate a website to a couple languages, and I've already read how to do it:
Mark strings for translation
Generate messages file
Translate messages file
The problem is, if I use whole sentences as the message ids, in english, for example, then if later I decide to modify the text, I'll have to change it in the code and on each message file . Or I could just change the english translation, but then my english message file will look weird, with translations from english to english which do not match.
Example:
Original: "I don't know what to do."
Translation: "I'm not sure what to do."
An alternative is to use unique message ids such as:
Original: "INDECISION_MESSAGE"
Translation: "I'm not sure what to do."
The advantage is that I can change translations without changing the id and things will still be consistent. But then there is no easy way for a translator to know what the message should be like as there is no context except by looking at the code.
What would you use?
In PHP people normally use the first approach you mentioned. In ASP.NET the second.
I think it's more of a personal taste and a matter of the framework that you use.
Personally I prefer the second option. Since you normally already have the ID/translation pair somewhere in a list, the translator can just take that list to translate.
What framework do you use?

Using "&" in a slug

I have a slug field in the database which has been created from a name containing &:
name: Hansel & Gretel
slug: hansel-&-gretel
Doctrine removes from the name characters which are not url-friendly. Well, & is definitely url-friendly.
Now, when I generate a link to the fairytale with symfony's link_to() I get:
http://myfairytalesite.ft/tale/hansel-%26amp%3B-gretel
So before the & gets url-encoded it gets changed to a HTML entity.
When I navigate to this URL I get 404 error because the slug is only url-decoded and the route (sfDoctrineRoute) tries to find an object with the slug hansel-&-gretel which obviously does not exist in the database.
My question: what can I do about it?
The name is imported from an external source so I can't change it manually.
I thought of adding a html_entities_decode to the action where I use the slug but it feels like attaching a wing to a plane with duct-tape...
I even tried to dig into the Symfony's internals and see what could I change there and I went so deep I was afraid I'm going to wake up the Balrog but couldn't find anything interesting. :/
Has anyone had a similar problem? Can it be a Sf or Doctrine bug?
To avoid this issue, I replace any & symbols as well as & amp; with an "and" when I save my slugs.
E.g.
hansel-and-gretel

Translate Model's value

What's the best way to handle the following problem with rails 3.0.3?
I have a Model(id, name) Nationality, in which I store different nationalities
ie: French, German, Belgian
My application should be available in multiples languages, so the select input which contains the nationalities should show French, German, Belgian if the locale is set to english, and should show Francais, Allemand, Belge if the locale is set to french.
Where to store the translation and how to use them in my code?
Thanks for your help.
If you create the following structure in your en.yml:
#en.yml
en:
label_french: French
label_german: German
Then you can call the following from your views:
<%= t("label_#{#nationality.name}") %>
Have a look at the puret gem which hooks into your existing schema without changing it.
https://github.com/jo/puret#readme
All is explain on i18n rails guides : http://guides.rubyonrails.org/i18n.html

Rails i18n strings auto-lowercased?

I've noticed that in my new Rails 3.0 application all German i18n strings are converted to lowercase (except for the first letter).
When having a string like this:
de:
email: "E-Mail"
the output is always like "E-mail". Same story with all the other strings - uppercase letters within a sentence are auto-converted to lowercase.
Is this default behaviour that I have to disable, or is there any other problem? I have successfully set the locale correctly, as these strings to actually work.
Thanks for your help
Arne
There should be no modifications to the content you specify as part of the internationalization process. It sounds like something is calling humanize on the string before it is output. Some of the standard Rails form helper methods do this I believe. If you just output the translation using t('email') you should see 'E-Mail' correctly.
Update:
From your comments it seems like it is a label that is causing the problem. If you explicitly specify the text for the label rather than relying on the default behaviour you will get the translation exactly as you specify. So,
<%= f.label(:email, t('email')) %>
should generate the correct label from the translations.
However, it isn't ideal. I think you may also run into problems with the generated validation error messages.
Got the same issue. solved it by adding the _html suffix to the I18n translation key. it seems that using this suffix suppresses the humanize usage.

Resources