Module: LucidTranslation::Mixin

Defined in:
lib/lucid_translation/mixin.rb

Defined Under Namespace

Classes: InternalTranslationProxy

Constant Summary collapse

CONTEXT_SEPARATOR =
"\004"
NAMESPACE_SEPARATOR =
'|'
NIL_BLOCK =
-> { nil }
TRANSLATION_METHODS =
[:_, :n_, :np_, :ns_, :p_, :s_]

Instance Method Summary collapse

Instance Method Details

#_(*keys, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/lucid_translation/mixin.rb', line 9

def _(*keys, &block)
  domain = Isomorfeus.i18n_domain
  locale = Isomorfeus.locale
  Isomorfeus.raise_error(message: "I18n _(): no key given!") if keys.empty?
  result = Redux.fetch_by_path(:i18n_state, domain, locale, '_', keys)
  return result if result
  _promise_send_i18n_method(domain, locale, '_', keys)
  block_given? ? block.call : ''
end

#n_(*keys, count, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/lucid_translation/mixin.rb', line 19

def n_(*keys, count, &block)
  domain = Isomorfeus.i18n_domain
  locale = Isomorfeus.locale
  Isomorfeus.raise_error(message: "I18n n_(): no key given!") if keys.empty?
  result = Redux.fetch_by_path(:i18n_state, domain, locale, 'n_', keys + [count])
  return result if result
  _promise_send_i18n_method(domain, locale, 'n_', keys + [count])
  block_given? ? block.call : ''
end

#N_(translate) ⇒ Object



66
67
68
# File 'lib/lucid_translation/mixin.rb', line 66

def N_(translate)
  translate
end

#Nn_(*keys) ⇒ Object



70
71
72
# File 'lib/lucid_translation/mixin.rb', line 70

def Nn_(*keys)
  keys
end

#np_(context, plural_one, *args, separator: nil, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/lucid_translation/mixin.rb', line 29

def np_(context, plural_one, *args, separator: nil, &block)
  nargs = ["#{context}#{separator || CONTEXT_SEPARATOR}#{plural_one}"] + args
  translation = n_(*nargs, &NIL_BLOCK)
  return translation if translation
  block_given? ? block.call : n_(plural_one, *args)
end

#ns_(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lucid_translation/mixin.rb', line 36

def ns_(*args, &block)
  domain = Isomorfeus.i18n_domain
  locale = Isomorfeus.locale
  Isomorfeus.raise_error(message: "I18n ns_(): no args given!") if args.empty?
  result = Redux.fetch_by_path(:i18n_state, domain, locale, 'ns_', args)
  return result if result
  _promise_send_i18n_method(domain, locale, 'ns_', args)
  block_given? ? block.call : n_(*args).split(NAMESPACE_SEPARATOR).last
end

#p_(namespace, key, separator = nil, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/lucid_translation/mixin.rb', line 46

def p_(namespace, key, separator = nil, &block)
  domain = Isomorfeus.i18n_domain
  locale = Isomorfeus.locale
  args = separator ? [namespace, key, separator] : [namespace, key]
  result = Redux.fetch_by_path(:i18n_state, domain, locale, 'p_', args)
  return result if result
  _promise_send_i18n_method(domain, locale, 'p_', args)
  block_given? ? block.call : ''
end

#s_(key, separator = nil, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/lucid_translation/mixin.rb', line 56

def s_(key, separator = nil, &block)
  domain = Isomorfeus.i18n_domain
  locale = Isomorfeus.locale
  args = separator ? [key, separator] : [key]
  result = Redux.fetch_by_path(:i18n_state, domain, locale, 's_', args)
  return result if result
  _promise_send_i18n_method(domain, locale, 's_', args)
  block_given? ? block.call : ''
end