Strange error while translating the word "admin" in Rails - ruby-on-rails

Observe the following console output:
$ grep -IirF admin config/locales/
config/locales/static.el.yml: Administered: Χορηγήθηκε
config/locales/el.yml: admin: διαχειριστής
config/locales/system.el.yml: administered: Χορηγήθηκε
$ rails c
Running via Spring preloader in process 8176
Loading development environment (Rails 6.0.2.1)
irb(main):001:0> helper.t('admin1', locale: :el)
=> "admin1"
irb(main):002:0> helper.t('admin', locale: :el)
=> "διαχειριστής"
irb(main):003:0> helper.t('admin1', locale: :en)
=> "admin1"
irb(main):004:0> helper.t('admin', locale: :en)
=> {:js=>{true=>true, false=>false, :is_present=>"Is present", :is_blank=>"Is blank", :date=>"Date ...", :between_and_=>"Between ... and ...", :today=>"Today", :yesterday=>"Yesterday", :this_week=>"This week", :last_week=>"Last week", :number=>"Number ...", :contains=>"Contains", :is_exactly=>"Is exactly", :starts_with=>"Starts with", :ends_with=>"Ends with", :too_many_objects=>"Too many objects, use search box above", :no_objects=>"No objects found"}, :loading=>"Loading...", :toggle_navigation=>"Toggle navigation", :home=>{:name=>"Home"}, :pagination=>{:previous=>"« Prev", :next=>"Next »", :truncate=>"…"}, :misc=>{:search=>"Search", :filter=>"Filter", :refresh=>"Refresh", :show_all=>"Show all", :add_filter=>"Add filter", :bulk_menu_title=>"Selected items", :remove=>"Remove", :add_new=>"Add new", :chosen=>"Chosen %{name}", :chose_all=>"Choose all", :clear_all=>"Clear all", :up=>"Up", :down=>"Down", :navigation=>"Navigation", :root_navigation=>"Actions", :navigation_static_label=>"Links", :log_out=>"Log out", :time_ago=>"%{time} ago", :ago=>"ago", :more=>"Plus %{count} more %{models_name}"}, :flash=>{:successful=>"%{name} successfully %{action}", :error=>"%{name} failed to be %{action}", :noaction=>"No actions were taken", :model_not_found=>"Model '%{model}' could not be found", :object_not_found=>"%{model} with id '%{id}' could not be found"}, :table_headers=>{:model_name=>"Model name", :last_created=>"Last created", :records=>"Records", :username=>"User", :changes=>"Changes", :created_at=>"Date/Time", :item=>"Item", :message=>"Message"}, :actions=>{:dashboard=>{:title=>"Site Administration", :menu=>"Dashboard", :breadcrumb=>"Dashboard"}, :index=>{:title=>"List of %{model_label_plural}", :menu=>"List", :breadcrumb=>"%{model_label_plural}"}, :show=>{:title=>"Details for %{model_label} '%{object_label}'", :menu=>"Show", :breadcrumb=>"%{object_label}"}, :show_in_app=>{:menu=>"Show in app"}, :new=>{:title=>"New %{model_label}", :menu=>"Add new", :breadcrumb=>"New", :link=>"Add a new %{model_label}", :done=>"created"}, :edit=>{:title=>"Edit %{model_label} '%{object_label}'", :menu=>"Edit", :breadcrumb=>"Edit", :link=>"Edit this %{model_label}", :done=>"updated"}, :delete=>{:title=>"Delete %{model_label} '%{object_label}'", :menu=>"Delete", :breadcrumb=>"Delete", :link=>"Delete '%{object_label}'", :done=>"deleted"}, :bulk_delete=>{:title=>"Delete %{model_label_plural}", :menu=>"Multiple delete", :breadcrumb=>"Multiple delete", :bulk_link=>"Delete selected %{model_label_plural}"}, :export=>{:title=>"Export %{model_label_plural}", :menu=>"Export", :breadcrumb=>"Export", :link=>"Export found %{model_label_plural}", :bulk_link=>"Export selected %{model_label_plural}", :done=>"exported"}, :history_index=>{:title=>"History for %{model_label_plural}", :menu=>"History", :breadcrumb=>"History"}, :history_show=>{:title=>"History for %{model_label} '%{object_label}'", :menu=>"History", :breadcrumb=>"History"}}, :form=>{:cancel=>"Cancel", :basic_info=>"Basic info", :required=>"Required", :optional=>"Optional", :one_char=>"character", :char_length_up_to=>"length up to", :char_length_of=>"length of", :save=>"Save", :save_and_add_another=>"Save and add another", :save_and_edit=>"Save and edit", :all_of_the_following_related_items_will_be_deleted=>"? The following related items may be deleted or orphaned:", :are_you_sure_you_want_to_delete_the_object=>"Are you sure you want to delete this %{model_name}", :confirmation=>"Yes, I'm sure", :bulk_delete=>"The following objects will be deleted, which may delete or orphan some of their related dependencies:", :new_model=>"%{name} (new)"}, :export=>{:confirmation=>"Export to %{name}", :select=>"Select fields to export", :select_all_fields=>"Select All Fields", :fields_from=>"Fields from %{name}", :fields_from_associated=>"Fields from associated %{name}", :display=>"Display %{name}: %{type}", :options_for=>"Options for %{name}", :empty_value_for_associated_objects=>"<empty>", :click_to_reverse_selection=>"Click to reverse selection", :csv=>{:header_for_root_methods=>"%{name}", :header_for_association_methods=>"%{name} [%{association}]", :encoding_to=>"Encode to", :encoding_to_help=>"Choose output encoding. Leave empty to let current input encoding untouched: (%{name})", :skip_header=>"No header", :skip_header_help=>"Do not output a header (no fields description)", :default_col_sep=>",", :col_sep=>"Column separator", :col_sep_help=>"Leave blank for default ('%{value}')"}}, :history_rollback=>{:cancel=>"Cancel", :confirm=>"Are you sure?", :table_headers=>{:changes=>"Changes", :field=>"Field", :rollback=>"Rollback", :view_changes=>"View changes"}, :rollback_destroy=>"Rolled back newly-created record by destroying it.", :rollback_success=>"Rolled back changes to this record.", :rollback_impossible=>"Couldn't rollback. Sorry.", :version_not_found=>"Version does not exist.", :rollback_violates_foreign_key=>"Couldn't rollback: this model links to another model, which doesn't exists."}}
Translation seems to work correctly, except for a single word: 'admin'
Can someone explain this error, and how to overcome it?

This most likely has to do with a conflict with some other gem that adds an admin namespace, like maybe ActiveAdmin or rails_admin, or maybe a role authorization plugin. That's why it's returning a hash instead of a single string value.
Some other code or gem has loaded the admin key to look something like this:
en:
admin:
js:
'true': true
'false': false
is_present: Is present
....etc....
I would suggest nesting your key to avoid the conflict with something like:
en:
user_type:
admin: Administrator
helper.t('user_type.admin')
I would also suggest setting up the i18n-tasks gem, which can find and correct issues with translation files, as part of your CI or test suite, which I believe would have detected this conflict with the command i18n-tasks health and shown that admin is a dupilcate key, so when your yaml files and the gem's get combined, only the last one to write to the translations hash will be preserved.

Related

Rails internationalization is not working in production mode

I have a non-AR model, which I use for a form/composition object.
There are lots input fields, which I collect in order to do some data manipulation inside AR transaction.
I use I18n for internationalization.
yml:
pl:
activemodel:
attributes:
catalog/checkout:
name: Imię
form:
= f.input :name,
required: true,
label: I18n.t(:'.activemodel.attributes.catalog/checkout.name')
The issue is that in development and production modes (locally) I see the input having proper label taken from config/locales, but on production server it says
"translation missing: pl.activemodel.attributes.catalog/checkout.name"
It happens exclusively with this non-AR model - everywhere else I18n works perfectly fine.
Any thoughts?
EDIT
Logged into the production server's console:
I18n.reload!
#=> nil
I18n.t(:'.activemodel').keys
#=> [:errors, :attributes] # attributes is what wee need
But
I18n.t(:'.activemodel.attributes')
#=> "translation missing: pl.activemodel.attributes"
Wth?...
EDIT #2
I have to notice, that having
pl:
activemodel:
attributes:
catalog/checkout:
errors:
models:
catalog/checkout:
attributes:
errors section works as expected, problems are only with activemodel.attributes

Rails I18n: error 'translation missing' when i try translate this words "off"

Why I can't translate this word?
config/locales/pt-BR.yml
pt-BR:
testing:
off: 'Desligado'
offff: 'Test'
rails console
> I18n.t 'testing.offff'
=> "Test"
> I18n.t 'testing.off'
=> "translation missing: pt-BR.testing.off"
Special words are restricted such as
'true, false, yes, no, on, off'
Juste change the key 'off' to something else, which is not restricted.
The doc about booleans in YAML

Rails 4: I18n::InvalidLocaleData

I installed the Kaminari gem and followed the instructions. I customized my theme to the foundations but when trying to further customize the pagination style using the en.yml file, I run into this error:
I18n::InvalidLocaleData in Campaigns#index
can not load translations from /myapp/config/locales/en.yml: #<Psych::SyntaxError: (/myapp/config/locales/en.yml): did not find expected key while parsing a block mapping at line 23 column 3>
Extracted source (around line #2):
1 <li>
2 <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
3 </li>
I simply copied and pasted what was in the docs..
en:
hello: "Hello world"
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"
If I delete everything other than pagination: line, the error still remains. I'm not super familiar with the locale file in Rails as well as YAML files so any help is highly appreciated.
According to the results from YAML Lint, it appears that the hello: "Hello world" line is invalid.
Also, it looks like the views.pagination.first line in your code is looking for a path like: views -> pagination -> first in your YAML file (and because this pattern can't be found, there's another error).
Therefore, it might be better to use this kind of YAML example:
en:
views:
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"

created data did not appear when testing

hye, I'm new to BDD testing and testing particularly. I'm using rspec, factory_girl_rails, selenium and capybara gem. I want to test for editing a data and save it like so :
it "edit consultant" do
# c = FactoryGirl.create(:consultant)
c = Consultant.create(
orgorcom: "xyz",
year_id: 8,
bidang: "IT & Networking",
project: "E-Sw",
professional_fee: "1000000",
role: "Database Architect",
user_id: 19,
nopkj: "075899 "
)
visit "/consultants?year=#{Time.now.year}"
string = "#consultant#{c.id}"
page.find(string).click_link('Edit')
fill_in "Organization / Company" , :with => c.orgorcom + "aaa"
fill_in "Field", :with => c.bidang + "aaa"
fill_in "Project" , :with => c.project + "aaa"
fill_in "Role", :with => c.role + "aaa"
fill_in "Professional fee", :with => c.professional_fee + 111
click_button "Save"
expect(page).to have_content "Data updated."
sleep 10
# c.destroy
# find('a[href="/consultants/6144/edit?year=2013"]').click
end
But the data I created did not appear & I get this message
1) the consultation edit consultant
Failure/Error: page.find(string).click_link('Edit')
Capybara::ElementNotFound:
Unable to find css "#consultant6157"
when I tried click on existing data like below, it passed.
page.find("#consultant6144").click_link('Edit')
I am able to print out the consultant id but still the record mysteriously did not appear before the test ends (which the db will rollback).
This is a known issue if you're using Selenium for browser testing, with transactional fixtures configured in your spec_helper. Selenium will run in a different thread and with a different database connection than the one being used by Rspec to create your database objects inside transactions, therefore the Selenium thread won't be able to see them.
For these kind of specs, you will need to not use transactional fixtures and use something like database_cleaner to create objects at the start of your specs and truncate/delete them afterwards.
Your problem doesn't seem related to FactoryGirl. The error message thrown by Capybara indicates that the object's id was properly concatenated into the string, but the corresponding CSS wasn't found in the page.
I would recommend you install the launchy gem by inserting this into your Gemfile:
group :development, :test do
gem 'launchy'
end
That will enable you to use the save_and_open_page method in your feature specs. Then insert this method right before the line that's provoking the error, like so:
# ...
save_and_open_page
page.find(string).click_link('Edit')
# ...
When save_and_open_page is called, your default browser will open at the same page. Then you can manually inspect the page's HTML and find out if it does, in fact, have the element you're looking for and, if it doesn't, why that happens.

Cucumber `press button` failure (Capybara::ElementNotFound)

I'm a relative newbie starting up a new Ruby on Rails app. I started by following a combination of instructions at https://github.com/intridea/omniauth, http://www.communityguides.eu/articles/16, http://intridea.com/2011/1/31/easy-rails-admin-login-with-google-apps-and-omniauth?blog=company . At the point everything appeared to work correctly, I started to write my very first cucumber features and steps. I was able to get a couple of steps up and running, but I've been bogged down on a step that I thought was built in. I have a form with two submit_tags but I can't get a scenario to successfully pass a basic And I press "button" step.
possibly relevant gems:
rails (3.1.0.rc4)
capybara (1.0.0)
cucumber (1.0.1)
cucumber-rails (1.0.2)
nokogiri (1.4.7)
gherkin (2.4.5)
rack-test (0.6.0)
selenium-webdriver (0.2.2)
section of the form in question:
<%= form_tag :controller => "services", :action => "newaccount" do %>
<%= submit_tag "confirm", :id => "confirm", :title => "confirm", :value => "confirm", :text => "confirm", :name => "confirm" %>
<%= submit_tag "cancel", :id => "cancel", :title => "cancel", :value => "cancel", :text => "cancel", :name => "cancel" %>
<% end %>
scenario in question:
Scenario: I register with a valid and currently active google account
Given I am not registered
When I sign in with a valid and currently active google account
And I press "confirm" # <-- THE PROBLEMATIC STEP
Then I should see "Your account has been created and you have been signed in!"
I think this is the relevant web_step (straight from the default web_steps.rb which I have not edited at all):
When /^(?:|I )press "([^"]*)"$/ do |button|
click_button(button)
end
relevant cucumber output:
Scenario: I register with a valid and currently active google account # features/auth_and_auth/initial_tests.feature:6
Given I am not registered # features/step_definitions/authentication_steps.rb:1
When I sign in with a valid and currently active google account # features/step_definitions/authentication_steps.rb:5
And I press "confirm" # features/step_definitions/web_steps.rb:52
no button with value or id or text 'confirm' found (Capybara::ElementNotFound)
(eval):2:in `click_button'
./features/step_definitions/web_steps.rb:53:in `/^(?:|I )press "([^"]*)"$/'
features/auth_and_auth/initial_tests.feature:9:in `And I press "confirm"'
Then I should see "Your account has been created and you have been signed in!" # features/step_definitions/web_steps.rb:105
relevant html output:
<input id="confirm" name="confirm" text="confirm" title="confirm" type="submit" value="confirm">
<input id="cancel" name="cancel" text="cancel" title="cancel" type="submit" value="cancel">
As is obvious, I've accounted for value, id, text, as well as name and title. I also saw a post that said the input type had to be specified as submit which it appears to have been. And I've tried it with both the confirm button and the cancel button.
After searching everywhere that I know about, and trying every suggestion that looked even remotely relevant, I'm at an impasse. What am I missing?
I'm not sure if the code below is the best way to deal with the problem I encountered, but it is getting the relevant step to pass.
When /^(?:|I )press "([^"]*)"$/ do |button|
# click_button(button) # the original web_steps.rb version that fails
%{I press (button)} # my revised version that passes
end
I'd still appreciate any feedback on:
why the original web_steps.rb version fails,
whether this is an appropriate approach or not, and
if there is a more 'rails' way to deal with this.

Resources