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

Fails when Fixnums are present in the locale files #99

Closed
rngadam opened this issue Oct 9, 2014 · 3 comments
Closed

Fails when Fixnums are present in the locale files #99

rngadam opened this issue Oct 9, 2014 · 3 comments
Labels
Milestone

Comments

@rngadam
Copy link

rngadam commented Oct 9, 2014

i18n-tasks v0.7.7

$ i18n-tasks translate-missing
/Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:74:in `next': undefined method `each' for nil:NilClass (NoMethodError)
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:74:in `parse_value'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:72:in `block in parse_value'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:72:in `map'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:72:in `parse_value'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:53:in `from_values'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:30:in `fetch_google_translations'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:23:in `block in google_translate_list'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:22:in `each'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:22:in `map'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:22:in `google_translate_list'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:10:in `block in google_translate_forest'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/data/tree/nodes.rb:13:in `each'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/data/tree/nodes.rb:13:in `each'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:9:in `inject'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/google_translation.rb:9:in `google_translate_forest'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/command/commands/missing.rb:29:in `translate_missing'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/command/commander.rb:38:in `run'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/command/commander.rb:19:in `safe_run'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/bin/i18n-tasks:41:in `block (3 levels) in <top (required)>'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/slop_command.rb:19:in `call'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/lib/i18n/tasks/slop_command.rb:19:in `block (3 levels) in slop_command'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/slop-3.6.0/lib/slop.rb:260:in `call'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/slop-3.6.0/lib/slop.rb:260:in `parse!'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/slop-3.6.0/lib/slop.rb:235:in `parse!'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/slop-3.6.0/lib/slop.rb:65:in `parse!'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/slop-3.6.0/lib/slop.rb:54:in `parse'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/gems/i18n-tasks-0.7.7/bin/i18n-tasks:32:in `<top (required)>'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/bin/i18n-tasks:23:in `load'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/bin/i18n-tasks:23:in `<main>'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
    from /Users/rngadam/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

Context:

    def parse_value(untranslated, each_translated)
      case untranslated
        when Array
          # implode array
          untranslated.map { |from| parse_value(from, each_translated) }
        when String
          restore_interpolations untranslated, each_translated.next

from debugging, it looks like each_translated is an Enumerator from nil.

If I change this:

   def from_values(list, translated_values)
      keys                = list.map(&:first)
      untranslated_values = list.map(&:last)
      keys.zip parse_value(untranslated_values, translated_values.to_enum) unless translated_values.nil?
    end

I get the error message instead:

i18n-tasks: Google Translate returned no results. Make sure billing information is set at https://code.google.com/apis/console.

easy_translate works with the same string (picked using puts):

2.1.2 :006 > EasyTranslate.translate "有 %{count} 个错误发生导致「%{model}」无法被保存,", :to => :en
 => "There% {count} error occurs that causes \"% {model}\" can not be saved," 
@rngadam rngadam changed the title No translated values returned Fails when non-string values are present in the locale files Oct 10, 2014
@rngadam
Copy link
Author

rngadam commented Oct 10, 2014

After some debugging, one problem seems to be that the values_list array here:

    def fetch_google_translations(list, opts)
      values_list = to_values(list.compact)
      raw_translations = EasyTranslate.translate(values_list, opts)
      from_values(list, raw_translations).tap do |result|
        if result.blank?
          raise CommandError.new(I18n.t('i18n_tasks.google_translate.errors.no_results'))
        end
      end
    end

Contains non-string values such as numbers (3) and boolean (boolean).

If I add a to_s in to_values:

    def to_values(list)
      list.map { |l| dump_value l[1].to_s }.flatten.compact
    end

The translation completes correctly. However, lists of translation strings appear to be empty.

And indeed, the one of the locales file has this:

  number:
    currency:
      format:
        delimiter: ","
        format: "%u %n"
        precision: 2
        separator: "."
        significant: false
        strip_insignificant_zeros: false
        unit: CN¥

...where precision (2) and significant, strip_insignificant_zeros (false) are not strings.

rngadam added a commit to rngadam/i18n-tasks that referenced this issue Oct 10, 2014
glebm added a commit that referenced this issue Oct 10, 2014
@glebm
Copy link
Owner

glebm commented Oct 10, 2014

i18n-tasks really shouldn't try to translate non-string values. In the commit above, I've changed it to skip and return them as is.

glebm added a commit that referenced this issue Oct 10, 2014
@glebm glebm added the bug label Oct 11, 2014
@glebm glebm changed the title Fails when non-string values are present in the locale files Fails when Fixnums are present in the locale files Oct 11, 2014
@glebm glebm added this to the v0.7.8 milestone Oct 11, 2014
@glebm
Copy link
Owner

glebm commented Oct 22, 2014

Released in v0.7.8

@glebm glebm closed this as completed Oct 22, 2014
bartimaeus added a commit to bartimaeus/i18n-tasks that referenced this issue Dec 1, 2014
* upstream/master: (39 commits)
  Tidy up the spec template indentation, use let(:unused_keys).
  Update README.md
  Added Gitter badge
  minor fix ignore_lines pattern
  travis: 2.1.1 -> 2.1.3
  .travis latest jruby
  Bump to 0.7.8
  Google Translate fixnum spec glebm#100
  Improve ignore_lines glebm#96 glebm#102
  initialize @key_filter glebm#101
  Lines in HAML that start with # are skipped
  ignore config: better examples
  ru copy fix
  move error message to i18n
  YARD signatures for GoogleTranslation methods
  Possible fix for glebm#99
  README: small fixes for defining custom commands
  Remove unused keys
  fix typo in gemspec [ci skip]
  Simplify commands DSL i18n
  ...

Conflicts:
	spec/google_translate_spec.rb
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

2 participants