rails-i18n

Access translation file (i18n) from inside rails model

此生再无相见时 提交于 2019-12-21 06:46:36
问题 What I have in my model is: def body_color_enum [ ['Aqua', '#009c9c'], ['Grey', '#6d6e71'], ['Yellow', '#ffe600'], ['White', 'white'] ] end I want these values to come from the translation file 'en.yml' en: group: hero: hex1: '#6d6e71' name1: 'Dark grey' hex2: '#ccc' name2: 'Light grey' hex3: '#0099ce' name3: 'Blue' hex4: '#ffffff' name4: 'White' I have tried this: def body_color_enum [ [t('group.hero.name1'), '#009c9c'], ['Grey', '#6d6e71'], ['Yellow', '#ffe600'], ['White', 'white'] ] end

Interpolation in I18n array

吃可爱长大的小学妹 提交于 2019-12-20 14:36:02
问题 I'm using arrays in a locale file to be able to generate blocks of text in various output methods (ActionMailer templates, Prawn documents, HAML templates) that are locale-specific. It works perfectly, but sometimes I want to pass variables into these I18n calls. However, this doesn't work. Say my locale file looks like this: en: my_array: - "Line 1" - "Line 2" - "Line 3 has a %{variable}" - "Line 4" I want the output of I18n.t('my_array', :variable => 'variable named variable') to be as

i18n for select boxes

此生再无相见时 提交于 2019-12-18 23:38:13
问题 I have a model named Role. And i am using the helper below in a form. Is there a way to change the value of name attribute to another language? <%= f.collection_select :role_id, Role.all, :id, name, {} -%> locales/de.yml de: role: admin: "something" editor: "something something" 回答1: In the model: class Role < ActiveRecord::Base def translated_name I18n.t(name, :scope => 'role') end end In the view: <%= f.collection_select :role_id, Role.all, :id, :translated_name -%> 来源: https:/

Rails: How to make Date strftime aware of the default locale?

扶醉桌前 提交于 2019-12-18 10:31:18
问题 I have my default locale set in the environment.rb as de (German). I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following: some_date.strftime('%B, %y') It prints in English ( January, 11 ), and not the expected German ( Januar, 11 ). How can I print the date according to the default locale? 回答1: Use the l (alias for localize ) method instead of raw strftime, like this: l(date, format: '%B %d, in the year

Rails Devise I18n Flash Messages with Twitter Bootstrap

廉价感情. 提交于 2019-12-18 10:29:45
问题 Hello I am new to ruby on rails and I am struggling to understand I18n's flash messages. I am using devise, rails 4, and twitter bootstrap.I understand that devise only uses flash[:notice] and flash[:alert] . I am able to get flash messages working for my user model with signing in and logging out. However I cannot get the flash error for signup or forgot password to display properly. It looks like the default error message. I've looked at bunch of questions related to this and Im confused on

Why word 'translate' is messing irb?

試著忘記壹切 提交于 2019-12-18 09:07:10
问题 I don't understand why my method translate undefines start_with? method and is messing something in irb, so I can exit irb only by pressing Ctrl + d , not exit or quit : >> "hello".respond_to?(:start_with?) => true >> def translate(string) >> if string.start_with?("a", "e", "i", "o", "u") >> string += "ay" >> end >> end NoMethodError: undefined method `start_with?' for #<RubyVM::InstructionSequence:0x00000001d4c960> from (irb):3:in `translate' from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11

Submit button helper with I18n.t

ぐ巨炮叔叔 提交于 2019-12-18 06:06:14
问题 I want to write a helper for a submit button, that takes in account the action (create or update) to get the right translation. Here they are : fr: submit: create: user: "Créer mon compte" product: "Déposer l'objet" session: "Se connecter" update: user: "Mettre à jour mon compte" product: "Modifier l'objet" I tried this : def submit_button(model) if model == nil I18n.t('submit.create.%{model}') else I18n.t('submit.update.%{model}') end end But it didn't worked and rspec send me that :

i18n Pluralization

六眼飞鱼酱① 提交于 2019-12-17 22:59:37
问题 I want to be able to translate pluralized strings in i18n in rails. A string can be : You have 2 kids or You have 1 kid I know that I can use pluralize helper method, but I want to embed this in i18n translations so that I don't have to mess up with my views at any point in the future. I read that :count is somehow used in translations for plural, but I can't find any real resources on how it gets implemented. Notice that I know that I can pass a variable in a translation string. I also tried

I18n deprecation warning when using ActiveModel without Rails

寵の児 提交于 2019-12-13 01:24:11
问题 When I run Rspec on my models, I get this warning: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. I saw a similar question where the solution was to set config.i18n.enforce_available_locales or I18n.config.enforce_available_locales in my config/application.rb file. I tried both, but I still get the warning. The test that gives me the

Rails 4 multidomain application with locale set for each domain i18n locale

a 夏天 提交于 2019-12-12 09:42:11
问题 In a Rails 4 multidomain app, I would need a set of locale files for 4 languages for each domain (3 domains total). Some of the translations overlap between the domains but some of them are very specific, so I am thinking about a structure that would go somewhat like this: config/locales/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by all domains config/locales/domain1/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 1 config/locales/domain2/en.yml ..fr.yml ..de.yml ..it.yml #is