Class: AMS::Translate

Inherits:
Object
  • Object
show all
Defined in:
RubyExtension/ams_Lib/translate.rb

Overview

Translate allows loading and controlling localization of strings.

Since:

  • 2.0.0

Instance Method Summary collapse

Constructor Details

#initialize(toolname, dir, locale = nil) ⇒ Translate

Note:

The extension of a translation file must be .strings. Toolname and locale must be separated with an underscore. Example: commands_ru.strings or commands_pt-BR.strings. File names are not case sensitive.

Note:

The commands within a translate file must following the "base lang" = "locale lang" syntax. A key-value pair cannot expand to multiple lines. Line comments (//) are supported too. If, however, comments are found within a string, they will be interpreted as a string. Spaces and newlines don't affect the syntax. Escape characters within strings don't affect the syntax either.

Note:

No errors will be raised if a translation file is invalid.

Load translation strings.

Parameters:

  • toolname (String)

    Base name of the translation file.

  • dir (String)

    Path to the directory with translation files

  • locale (String, nil) (defaults to: nil)

    Specific language to look for or nil to relent on current localization.

Since:

  • 2.0.0


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'RubyExtension/ams_Lib/translate.rb', line 24

def initialize(toolname, dir, locale = nil)
  @strings = {}
  locale = ::Sketchup.get_locale unless locale
  # Configure path
  fpath = ::File.join(dir.to_s, toolname.to_s + '_' + locale.to_s + '.strings')
  fpath.force_encoding("UTF-8") unless AMS::IS_RUBY_VERSION_18
  return unless ::File.exists?(fpath)
  # Parse from the file
  ::File.open(fpath, 'r') { |fp|
    fp.each { |line|
      ri = []
      besc = false
      in_str = false
      fslash = false
      for i in 0...line.length
        x = line[i, 1]
        if in_str
          if besc
            besc = false
          elsif x == "\\"
            besc = true
          elsif x == "\""
            ri << i
            in_str = false
          end
        elsif x == "\""
          ri << i + 1
          in_str = true
          fslash = false
        elsif x == "/"
          if fslash
            break
          else
            fslash = true
          end
        end
      end
      if ri.size == 4
        key = line[ri[0], ri[1] - ri[0]]
        val = line[ri[2], ri[3] - ri[2]]
        @strings[key] = val
      end
    }
  }
end

Instance Method Details

#convert_text(text, *si) ⇒ String

Note:

If a translation doesn't exist, the expression is replaced with the string itself.

Replace all TRANSLATE[some string] within a text to the associated translation.

Parameters:

  • text (String)
  • si (*String)

    Optional strings for substitution of %0 … %n.

Returns:

  • (String)

Since:

  • 3.6.0


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'RubyExtension/ams_Lib/translate.rb', line 100

def convert_text(text, *si)
  text2 = text.dup
  text2.force_encoding("UTF-8") unless AMS::IS_RUBY_VERSION_18
  i = nil
  offset = 0
  while (i = text2.index("TRANSLATE[", offset))
    j = i + 10
    while true
      x = text2[j, 1]
      if x == "]"
        break
      elsif x.nil?
        raise(SyntaxError, "The \"TRANSLATE\" string, starting at index #{i}, is missing a closing bracket!")
      end
      j += 1
    end
    key = text2[i + 10, j - i - 10]
    val = (@strings[key] || key)
    text2[i, j - i + 1] = val
    offset = i + val.length
  end
  # Substitute additional strings
  si.compact.each_with_index { |s, i|
    text2.gsub!(/\%#{i}/, s.to_s)
  }
  return text2
end

#get(key, *si) ⇒ String Also known as: []

Get a single translation.

Parameters:

  • key (String)

    A string to translate

  • si (*String)

    Optional strings for substitution of %0 … %n.

Returns:

  • (String)

    Translated string

Since:

  • 2.0.0


74
75
76
77
78
79
80
81
# File 'RubyExtension/ams_Lib/translate.rb', line 74

def get(key, *si)
  value = (@strings[key.to_s] || key.to_s).dup
  # Substitute additional strings
  si.compact.each_with_index { |s, i|
    value.gsub!(/\%#{i}/, s.to_s)
  }
  return value
end

#get_allHash{String => String}

Get all translations as hash.

Returns:

  • (Hash{String => String})

    key/value pairs of original and translated strings.

Since:

  • 2.0.0


88
89
90
# File 'RubyExtension/ams_Lib/translate.rb', line 88

def get_all
  return @strings
end