Module: ShipEasyI18n::Rails::ViewHelpers

Defined in:
lib/i18n/rails/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#i18n_head_tags(profile: nil, chunk: nil) ⇒ Object



4
5
6
7
8
9
# File 'lib/i18n/rails/view_helpers.rb', line 4

def i18n_head_tags(profile: nil, chunk: nil)
  safe_join([
    i18n_inline_data(profile: profile, chunk: chunk),
    i18n_script_tag,
  ], "\n")
end

#i18n_inline_data(profile: nil, chunk: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/i18n/rails/view_helpers.rb', line 11

def i18n_inline_data(profile: nil, chunk: nil)
  config = ShipEasyI18n.configuration
  label_file = ShipEasyI18n::Rails::LabelFetcher.new.fetch(
    profile: profile || config.profile,
    chunk:   chunk   || config.default_chunk,
  )
  return "".html_safe unless label_file

  json_content = JSON.generate(label_file)
  (:script, json_content.html_safe, id: "i18n-data", type: "application/json")
end

#i18n_script_tag(hide_until_ready: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/i18n/rails/view_helpers.rb', line 23

def i18n_script_tag(hide_until_ready: false)
  config = ShipEasyI18n.configuration
  attrs  = {
    src: config.loader_url,
    "data-key":     config.public_key,
    "data-profile": config.profile,
    async: true,
  }
  attrs[:"data-hide-until-ready"] = "true" if hide_until_ready
  tag(:script, attrs)
end

#i18n_t(key, variables = {}, profile: nil, chunk: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/i18n/rails/view_helpers.rb', line 35

def i18n_t(key, variables = {}, profile: nil, chunk: nil)
  config = ShipEasyI18n.configuration
  label_file = ShipEasyI18n::Rails::LabelFetcher.new.fetch(
    profile: profile || config.profile,
    chunk:   chunk   || config.default_chunk,
  )
  return key unless label_file && label_file["strings"]

  value = label_file["strings"][key] || key
  variables.each { |k, v| value = value.gsub("{{#{k}}}", v.to_s) }
  value
end