Code Monkey home page Code Monkey logo

Comments (15)

bodrovis avatar bodrovis commented on August 30, 2024

As I understand, your Lokalise project already contains the en_GB locale, right? If not, it should be added first because unfortunately API does not create it manually upon uploading. If, instead, you would like to connect this translation file to a regular en locale (after the en.yml filename), some additional setup is needed.

Please open the config/lokalise_rails.rb file and uncomment the c.lang_iso_inferer line. By default it equals to

c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }

However, you can change it to something like

c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first.split('_')[0] }

to take only the en part from the en_GB.

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

In the next update I'll add another path param to this lambda so that it's easier to infer the locale based on the filename instead

from lokalise_rails.

theocodes avatar theocodes commented on August 30, 2024

Thanks for the clarification here, @bodrovis!

I can now see that after creating the locale in the Lokalise project, it no longer complains about the lang_iso being invalid.

The original error (#<ArgumentError: wrong number of arguments (given 1, expected 2)>) that prompted me to open the issue is still happening though. I tried to dig into the issue a bit and it seems lokalise_maanger is what is reporting the error, though it remains unclear to me why. Has that gem been updated recently and potentially broke the API lokalise_rails uses?

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

Please send full stack trace error

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

Also, if you have tweaked any other settings inside lokalise_rails.rb file (except for the)

    c.api_token = ENV['LOKALISE_API_TOKEN']
    c.project_id = ENV['LOKALISE_PROJECT_ID']

please send those as well. I've just tried to re-install the gem into a sample Rails app and re-run the export command, it works fine. I've not published any new releases recently

from lokalise_rails.

theocodes avatar theocodes commented on August 30, 2024

The only other change to the config is the following, in order to ignore top level files.

    c.skip_file_export = ->(file) {
      # Ignore all but english locales
      file.dirname.split.last.to_s == "locales" && file.split.last.to_s.exclude?("en.yml")
    }

The stacktrace:

/Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/3.1.0/psych/exception.rb:10:in `initialize': wrong number of arguments (given 1, expected 2) (ArgumentError)
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:44:in `exception'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:44:in `raise'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:44:in `raise_on_fail'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:22:in `block (2 levels) in export!'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:21:in `each'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:21:in `block in export!'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:20:in `each'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:20:in `each_slice'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_manager-4.0.0/lib/lokalise_manager/task_definitions/exporter.rb:20:in `export!'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_rails-6.0.0/lib/tasks/lokalise_rails_tasks.rake:2:in `block (2 levels) in <main>'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/lokalise_rails-6.0.0/lib/tasks/lokalise_rails_tasks.rake:18:in `block (2 levels) in <main>'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/bugsnag-6.25.2/lib/bugsnag/integrations/rake.rb:20:in `execute'
        from /Users/theocodes/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

It's hard to say for now but I have to mention that exclude? is a ror-only method which might or might not work properly in this case. I'll try to research further

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

Can it be that the locales folder or the folders inside contain non-yaml files by the way? Or some malformed files? Because the latest error is reported by psych but I can't fully understand where it comes from

from lokalise_rails.

theocodes avatar theocodes commented on August 30, 2024

Can it be that the locales folder or the folders inside contain non-yaml files by the way? Or some malformed files? Because the latest error is reported by psych but I can't fully understand where it comes from

I think you might be correct.. I've updated the block to skip all but 1 file and the task was successful. I guess the tricky part is that the error itself doesn't give you much context when it fail, so maybe there's some room for improvement there..

Thanks for looking into it. I'll dig a bit more into what I was doing wrong and will let you know if anything.

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

Well, I actually agree with you. Problem is, the upload happens in parallel in multiple threads and while it tries to preserve the original info about the error, it might fail sometimes. Here's the deal: first this gem tries to detect all entries inside the locales folder or its subfolders. Then, it keeps only those that are actually files, that have proper extensions (controlled by the file_ext_regexp option, defauls to /\.ya?ml\z/i), and only those that pass the skip_file_export check. My guess is that something either slips through the cracks or one of the yaml files is faulty.

The psych exception on line 10 is DisallowedClass that is fired by the psych gem itself (not by lokalise_manager) whenever some operation like dump is performed on a non-supported class (in other words, when it does not know how to process something).

  class DisallowedClass < Exception
    def initialize action, klass_name
      super "Tried to #{action} unspecified class: #{klass_name}"
    end
  end

What bugs me, is why it says this exception cannot be instantiated. It almost if the klass_name cannot be determined or something.

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

In fact, psych comes into play only when inferring the file locale (if we're talking about uploading data to Lokalise). Thus

c.lang_iso_inferer = ->(data) { puts(data.inspect) ; YAML.safe_load(data)&.keys&.first }

this will output every file data it tries to parse. It might help with debugging

from lokalise_rails.

theocodes avatar theocodes commented on August 30, 2024

Thanks for all your support here @bodrovis

Everything working well on my end 👍

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

@theocodes If possible, would you please share what was the fix after all? Was it related to yaml files? If yes, perhaps you could send me a sample file that caused this issue? I wanted to further debug this and perhaps make the gem more bulletproof

from lokalise_rails.

theocodes avatar theocodes commented on August 30, 2024

Hey @bodrovis sorry didn't notice your comment sooner!

The issue was indeed with one the YAML files which had some anchors that weren't playing nice with psych.. I improved the experience for us with:

c.lang_iso_inferer = ->(data) {
  begin
    YAML.safe_load(data)&.keys&.first
  rescue => e
    raise "Invalid YAML (#{e}): \n #{data.lines.take(10).join("")}"
  end
}

I believe I could've done a similar thing in c.translations_loader which is arguably a better place for it, but for now it's good enough, I think. Hope this helps!

from lokalise_rails.

bodrovis avatar bodrovis commented on August 30, 2024

That's interesting, thank you @theocodes Actually I've just released a new version that might streamline this approach a bit. Now this setting also accepts filepath and the locale can be infered based on the filename like this:

lang_iso_inferer: ->(_data, path) { path.basename('.yml').to_s # => will return something like "en" or "en_US" }

from lokalise_rails.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.