Rails I18n prepositions localization unpredictable results - ruby-on-rails

I have a small localization yaml file like this:
en:
preposition:
with: with
on: on
and similar German locale. When I try to use I18n.translate('preposition.on') it produces unpredictable results and returns true instead of normal preposition. As far as I've understood while experimenting, Rails consider value on as true and opposite to off, but how should I deal with this if I really need just normal translation?

Solution found here - http://juliankniephoff.wordpress.com/2012/09/01/reserved-words-in-yaml-and-translating-booleans-in-rails/
That says, that keys and values like on, no, yes are really YAML reserved words, so in order to make this work I had to change my locale file to this:
en:
preposition:
with: with
"on": 'on'
That is to make key and value both look like strings.

Related

How to find the i18n key from the value

I want to make a helper function which can take an English string as input and translate it to the desired language chosen by the user.
As all the locale files would be having key in common so I am looking for a way to find key using the string value. I am using default.yml files for storing translations.
After finding the key, I can use <%= t() %> for translating key to other languages.
I don't know how your yaml file looks like and it probably depends on what kind of I18n backend you use. If it's just key-value pairs for every language then something like this could work
I18n.backend.translations[:en].key "English string"
There is also the simple way. Just load the yaml file with the translations.
YAML.load_file("path_to_translations.yml").key "English string"

using 18n and I18n:Alchemy in rails in order to translate a text value according to the locale

In my application, there is a field credit_debit that has two string values: 'credit' and 'debit'.
These two terms should be shown in a localized form to the user, so that when the app is using an italian locale they should be translated into 'avere' and 'dare'.
I could write my own setter/getter method for the credit_debit field and handle this issue inside it, but maybe I could leverage the I18n architecture to do so in a way that is consistent with the rest of the localization. If it's possible, how can I implement this?
Can't see the problem if you're already using i18n localization.
Just use the field as a key for i18n (with a prefix if you like)
t("prefix_#{credit_debit}")
and just define the strings for both your locales.
So in your en locale:
prefix_credit: credit
prefix_debit: debit
And in your it locale:
prefix_credit: avere
prefix_debit: dare

Is there an equivalent in RoR for PHP's gmdate?

For instance, if I called:
gmdate("M-D-yTh:i:s")
Is there something similar for this case in RoR? I guess I could always DateTime.now.hour, DateTime.now.year, etc. etc. but that seems extremely wrong.
See strftime() in Ruby's documentation on Time, which formats a time according to the directives in the given format string.

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.

Config file format

does anyone knows a file format for configuration files easy to read by humans? I want to have something like tag = value where value may be:
String
Number(int or float)
Boolean(true/false)
Array(of String values, Number values, Boolean values)
Another structure(it will be more clear what I mean in the fallowing example)
Now I use something like this:
IntTag=1
FloatTag=1.1
StringTag="a string"
BoolTag=true
ArrayTag1=[1 2 3]
ArrayTag2=[1.1 2.1 3.1]
ArrayTag3=["str1" "str2" "str3"]
StructTag=
{
NestedTag1=1
NestedTag2="str1"
}
and so on.
Parsing is easy but for large files I find it hard to read/edit in text editors. I don't like xml for the same reason, it's hard to read. INI does not support nesting and I want to be able to nest tags. I also don't want a complicated format because I will use limited kind of values as I mentioned above.
Thanks for any help.
What about YAML ? It's easy to parse, nicely structured has wide programming language support. If you don't need the full feature set, you could also use JSON.
Try YAML - is (subjectively) easy to read, allows nesting, and is relatively simple to parse.

Resources