Undefined method `[]' for #<LanguageHandler:0xaee4ea4 @strings={}>

I’m testing a plugin under development which works fine on SU 2016 64 bit and 2014 64 bit on Mac and Win10.

But running under SU2013 on Win 10 I get the Ruby console error shown in the topic title when SU starts.

Is this something to do with a difference between Ruby 1.8 and 2.0 and initializing an empty array?

It seems to be triggered by a call to MyLH = LanguageHandler.new (‘MyLH’).

Is this error in my call to the function, or in LanguageHandler itself? Or is it a consequence of not having given a French equivalent of all the English Language strings in the plugin? There are a few strings in a new part of the plugin which haven’t got translations yet.

In either case, how can I fix it so the plugin will run under older versions of SU?

The LanguageHandler was an internal feature for a long time. It was … SU2014 that it was made part of the public API. You can see in the docs that it’s denoted with SU2014+ (Homepage | SketchUp Developer)

Thank you.

I guess that means it won’t work with SU < 2014 then? Or is there a workaround? As crude as not having non-English language available for earlier versions, or perhaps an alternative way of translating the prompt strings?

John, the exact error is obvious. The [] instance method (which I myself had requested for the public version, since LanguageHandler is just a Hash wrapper class,) did not exist before v 2014.

Before that, the name of the method was a un-Rubyish GetString(). (It still exists as a backward compatibility method alias for [].)

(Note: You need to pay heed to the version numbers for each method in the API documentation.)

So it is triggered by the attempt to call an instance method named "[]".

For this specific NoMethodError, yes there is a workaround. It is a singleton wrapper method.

Within your code just after creating the instance of LanguageHandler, you “duck type” it,… ie, just ask the instance if it responds to :[], and if not define a singleton method upon the instance, ie:

if !MyLH.respond_to?(:[])
  def MyLH.[](key)
    GetString(key)
  end
end

… or the more obscure:

if !MyLH.respond_to?(:[])
  class << MyLH
    def [](key)
      GetString(key)
    end
  end
end

:warning: However, this will not work around other issues of finding the actual .strings files, that were fixed in the v2014 editions.

It is best to actual read the “Tools/langhandler.rb” file in old and new versions, to understand the changes that occurred.


(Moved to the Developers > Ruby API category. [Was in Extensions.])

Many thanks, Dan, for the clear explanation. Once you explain it, I see why it is failing, But sorry, my understanding of Ruby is still patchy - I have mostly succeeded by modelling my code on similar examples generously provided elsewhere, particularly on this forum and SketchUcation, and through working with ThomThom on SU Shapes and extending it, and working with Steve Baumgartner to update one of his plugins (but he’s on holiday at the moment with limited or no internet connection, or I’d have asked him via PM).

I’ll look as you suggest at the code in different versions of Tools/langhandler.rb for further info.

Sorry I posted in the wrong forum - I knew I had seen a forum Ruby category, but for some reason couldn’t find it when posting originally.

Before the LangHandler I made my own translation class. (And I still think the LangHandler is flawed btw.) You can find it in my TT_Lib library: Bitbucket

You are free to use it if you want - just remember to correct the namespace to your own off course.

Thank you, ThomThom.