Class: Multilocale::Phrase

Inherits:
Object
  • Object
show all
Defined in:
lib/multilocale/phrase.rb

Overview

One {key, value, language} row. A key translated into 12 locales is 12 phrases, not one phrase with 12 values — every API call and every error message counts rows, not keys.

A row can belong to several projects at once (projects). Editing it edits it for all of them, and deleting a key deletes every row it matches including the shared ones.

Constant Summary collapse

ATTRIBUTE_NAMES =
{
  id: "_id",
  key: "key",
  value: "value",
  language: "language",
  projects: "projects",
  projects_ids: "projectsIds",
  machine_translated: "machineTranslated",
  model: "model"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Phrase

Returns a new instance of Phrase.



14
15
16
# File 'lib/multilocale/phrase.rb', line 14

def initialize(attributes)
  @to_h = attributes.is_a?(Phrase) ? attributes.to_h : (attributes || {})
end

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



12
13
14
# File 'lib/multilocale/phrase.rb', line 12

def to_h
  @to_h
end

Class Method Details

.to_api(attributes) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/multilocale/phrase.rb', line 95

def self.to_api(attributes)
  return attributes.to_h if attributes.is_a?(Phrase)

  attributes.each_with_object({}) do |(key, value), document|
    document[ATTRIBUTE_NAMES[key] || key.to_s] = value
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



71
72
73
# File 'lib/multilocale/phrase.rb', line 71

def ==(other)
  other.is_a?(Phrase) && other.to_h == to_h
end

#[](key) ⇒ Object



67
68
69
# File 'lib/multilocale/phrase.rb', line 67

def [](key)
  @to_h[key.to_s]
end

#creation_timeObject



59
60
61
# File 'lib/multilocale/phrase.rb', line 59

def creation_time
  @to_h["creationTime"]
end

#hashObject



76
77
78
# File 'lib/multilocale/phrase.rb', line 76

def hash
  to_h.hash
end

#idObject



18
19
20
# File 'lib/multilocale/phrase.rb', line 18

def id
  @to_h["_id"]
end

#inspectObject



80
81
82
# File 'lib/multilocale/phrase.rb', line 80

def inspect
  "#<Multilocale::Phrase key=#{key.inspect} language=#{language.inspect} value=#{value.inspect}>"
end

#keyObject



22
23
24
# File 'lib/multilocale/phrase.rb', line 22

def key
  @to_h["key"]
end

#languageObject



30
31
32
# File 'lib/multilocale/phrase.rb', line 30

def language
  @to_h["language"]
end

#last_edit_timeObject



63
64
65
# File 'lib/multilocale/phrase.rb', line 63

def last_edit_time
  @to_h["lastEditTime"]
end

#machine_translated?Boolean

googleTranslate is the pre-2024 spelling and is still the only provenance flag on older rows, so a reader that only looks at machineTranslated reports human-written text that never was.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/multilocale/phrase.rb', line 49

def machine_translated?
  value = @to_h.fetch("machineTranslated") { @to_h["googleTranslate"] }
  !!value
end

#modelObject

Which engine produced it, when it was machine translated.



55
56
57
# File 'lib/multilocale/phrase.rb', line 55

def model
  @to_h["model"]
end

#organization_idObject



42
43
44
# File 'lib/multilocale/phrase.rb', line 42

def organization_id
  @to_h["organizationId"]
end

#projectsObject



34
35
36
# File 'lib/multilocale/phrase.rb', line 34

def projects
  @to_h["projects"] || []
end

#projects_idsObject



38
39
40
# File 'lib/multilocale/phrase.rb', line 38

def projects_ids
  @to_h["projectsIds"] || []
end

#valueObject



26
27
28
# File 'lib/multilocale/phrase.rb', line 26

def value
  @to_h["value"]
end