Language handler question

Dan,
this is weird, but I thought I had better report it for anyone else wanting to code German characters. Doing what you suggested unfortunately did not work for me so I had a bit of a play around to see what I could come up with. First, when I checked the UTF-8 option under Encoding in Notepad++ (for my German strings file) the umlaut characters changed to strange 3 character codes coloured white on a black background. I thought this was maybe OK and tried running the script but it wouldn’t load, giving an Error with UTF-8 as the reason. So I tried something else. The strange 3 character codes coloured white on a black background didn’t seem right to me. I marked the entire file by selecting all and then checked Convert to UTF-8 under the Encoding menu in Notepad++. The umlaut characters returned and best of all my Ruby script loaded and worked! The whole thing is very odd and not in the least user friendly. It’s not even easy to know where the problem lies. Possibly with Notepad++?
Hope this is of interest.
Francis

You must fix the file’s encoding by converting it to UTF-8, then any special characters which are/were encoded as the Unicode weird-things should swap - or at least if they don’t you can see them and replace them with corrected encoded equivalents…
The issue is not with Notepad++, but the fact that you originally typed the text in another encoding regime.
Ensure Notepad++ Preferences are set to always open/create files as the UTF-8 format…

All files used by SketchUp should be UTF-8 [without BOM] encoded.

1 Like

TIG,
I’ll definitely keep that in mind in all future work I do.
Thanks,
Francis

I wish there was one good example of 2016 LH files showing the proper typing and naming,
like :
examp-extentio.rb
examp-extentio folder
resources folder with examp.strings in it.

All those bad examples got me all mixed up. To say the least !!

The most recent Trimble connect extension that ship with SketchUp is decent enough examples. It looks like the work of @tt_su.

I’m fairly sure @DanRathbun has posted extension templates - probably at SketchUcation.

If not, I have an extension template i could post. I even thought about making a Ruby command-line extension generator, but am not lazy enough yet.

1 Like

Hi Jim,
Please do post a template. I’m afraid one is not enough.
Been studying Trimble_connect and again the naming of files and constants is variable, so it is hard to see to which file a constant is referring. Beside it is not a really good template since the extension is restricted to Pro and it uses a loader.rb file. To me a second “loader” is not really straight forward.
Anyway, here is what I have so far for the file in the plugins folder.
What is to put in the extension file(.rb) which is in the subfolder, the containing the Resources folder ?

module F3D

require ‘sketchup.rb’
require ‘extensions.rb’
require ‘langhandler.rb’

##Put translation object where the extension can find it. We assign it to a
# constant so it’s accessible from child modules and classes.
# noinspection RubyConstantNamingConvention

f3DLH = LanguageHandler.new("f3D_Ouv.strings")

#load the extension.
extension_name = f3DLH[“f3D_Ouvertures”]

path = File.dirname(_FILE).freeze
loader = File.join(path, “McF3D”, “f3D_Ouvertures.rb”)
extension = SketchupExtension.new(extension_name, loader)

extension.description = "Percement des ouvertures pour les portes et fenêtres F3D. "
extension.version = “1.0”
extension.creator = “Mario Chabot”
extension.copyright = “2016, Format-3D inc.”
Sketchup.register_extension extension, true
end # module

f3DLH = LanguageHandler.new("f3D_Ouv.strings")

needs to be a constant or variable i.e to work in the other rubies…

i.e. F3DLH or @f3DLH or @@f3DLH

john

Ok, corrected.
Now how do I tell the main file, (in the subfolder) to use the LH ?

To easily make it accessible to the whole namespace, define it as a contant in your extension module

module Example

  # ...

  # Now LH is available to the whole of the Example module.
  LH = LanguageHandler.new("f3D_Ouv.strings")

  # ...

end # module
1 Like

I thought I had sorted LH out.
1- Make a .string file, like
“Hello World!”=“Allo le monde!” 2- Put it in a subfolder named FR in a subfolder named Resources in a folder named the same as the .rb file 3- Define a constant referring to that .string file, like Lang = LanguageHandler.new("mc.strings") 4- In the code, add that constant in front of strings you want translated, like UI.messagebox (Lang["Hello World!"])

is missing the trailing ;

is missing the directory… i.e (“my_dir/mc.strings”)

would be LANG… i.e all UPPERCASE

also be aware of “curly quotes” that many text editors will use, but ruby doesn’t like…

set it in your editor preferences…

john [ I PM’d you at SCF ]

1 Like

The directory should not be there. LanguageHandler assumes you initialize an instance in your root RB file - the one in the root of the Plugins folder. Then it further assumes you have a support folder with the same base name as the RB file.

So, if you have a root RB file Example.rb then you need to create a support folder Example where you add a sub-folder Resources. Within Resources you add new folders with the corresponding language codes you support. In each of those you have your .strings file.

1 Like

So just the line
LH = LanguageHandler.new(“name.strings”) in the root .rb
does it, right ?
And LH is the instance, in this case.

Let’s say I set a LanguageHandler constant to @LH.
I’m trying to get this line to translate :
@mesg = “#{NAME}: Hit RETURN”
I thought it should have been @mesg = (@LH["#{NAME}: Hit RETURN"])
no luck, meaning it breaks the script.

you need to split out the dynamic element…

try

@mesg = (NAME + ': '  +  @LH['Hit RETURN'])

john

1 Like

Ok that is fine.
Bur now I’m presented with an extension having a root .rb and folder but the main rb is in a sub of that folder, same level as Resources should be. Is that rb file going to find the strings file ?
An example of this is Tig’s Mirror Selection v.6.
(studying ruby pathname and such…)

Update:
Found solution. Put the constant at module level. (trust in Ruby) :wink:

@mesg = "NAME : #{@LH['Hit RETURN']}"

?

Dan, I believe NAME is a constant in the snippet supplied…

Actually what is working now is.
@msg = “#{NAME}:” + ToolLH["Hit RETURN]

just out of quriosity… cos I don’t expect many people still care about backward compatibilety more than 5 years…
why did You choose to make a Singleton method and not create an alias inside of the class… (actually not sure how its called but it worked on my sk8)

class LanguageHandler
  def [](key)
    self.GetString(key)
  end
end

I guess not big difference since we create only one instance of that class per extention…
ok… I might have answered to myself… Is it not to mess with sKetchup API which could influence other’s extentions?

and there is other question but more rubyish… ? is a "[ sth]"method somehow special case… it gots extra treatment?
I mean both

say['word']
and
say.[]('word')

works…
cos I guess if I write

(def say.xy(key); self.GetString(key); end)

this

sayx'word'y

ok tested has no chanse to work other special signs don’t work too…