Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

t("foo", default: false) doesn't respect the default #31426

Closed
jdelStrother opened this issue Dec 13, 2017 · 7 comments
Closed

t("foo", default: false) doesn't respect the default #31426

jdelStrother opened this issue Dec 13, 2017 · 7 comments
Labels

Comments

@jdelStrother
Copy link
Contributor

Steps to reproduce

Try to translate a key, supplying a default of false ( eg t("foo", default: false)).

Expected behavior

If the translation is missing, it returns false

Actual behavior

If the translation is missing, it returns <span class="translation_missing" title="translation missing: en.false">False</span>

This is also demonstrated in this sample project:
rails51.zip

System configuration

Rails version:

5.1.4

Ruby version:

2.3.4

This appears to be a change in the i18n gem since 0.8.3 - if you downgrade to 0.8.1, t("foo", default: false) return false.
However, the problem only occurs if you use Rails t helper - if you go via I18n.t("foo", default: false), it returns false regardless of the i18n version.

This was originally filed on the i18n repo at ruby-i18n/i18n#379.

@utilum
Copy link
Contributor

utilum commented Dec 13, 2017

@jdelStrother could you reproduce with an executable script based on https://github.com/rails/rails/blob/master/guides/bug_report_templates/generic_gem.rb ?

@jdelStrother
Copy link
Contributor Author

Will see what I can do

@aditya-vector
Copy link
Contributor

I think the issue is with Array being passed as an option here in the Rails helper.
The code in the helper encapsulates the default option to an array no matter what. And, thus on the I18n side when I run,
I18n.translate('foo', default: [false]) it returns "translation missing: en.foo"

And then, the helper transforms it to the message "<span class=\"translation_missing\" title=\"translation missing: en.false\">False</span>" here in the rescue block. I wonder if its an update on Rails side or I18n side that has caused this to happen.

@jdelStrother
Copy link
Contributor Author

FWIW, a simple test case. For me, flipping between i18n 0.8.1 & 0.8.3 will fix/break the test.

# frozen_string_literal: true

gem "bundler", "< 1.16"

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", tag: 'fd1304d'
  
  # gem "i18n", "0.8.1"
  gem "i18n", "0.8.3"
end

require "action_controller/railtie"

class TestApp < Rails::Application
  config.root = __dir__
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  routes.draw do
    get "/" => "test#index"
  end
end

class TestController < ActionController::Base
  include Rails.application.routes.url_helpers

  def index
    render inline: "<%= t(:foo, default: false) || 'no default' %>"
  end
end

require "minitest/autorun"
require "rack/test"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_default_false_translations_should_return_false
    assert I18n.t(:foo, default: false) == false
    
    get "/"
    assert_equal "no default", last_response.body
  end

  private
    def app
      Rails.application
    end
end

@rafaelfranca
Copy link
Member

If that is the case this is a i18n issue since they changed the behavior in a patch version.

@fatkodima
Copy link
Member

Opened PR on i18n side ruby-i18n/i18n#399.

@y-yagi
Copy link
Member

y-yagi commented Mar 18, 2018

Fixed with ruby-i18n/i18n#399

@y-yagi y-yagi closed this as completed Mar 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants