Module: LucidI18n::Mixin
- Defined in:
- lib/lucid_i18n/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
- #_(*keys, &block) ⇒ Object
- #current_locale ⇒ Object
- #l(object, format = :standard, _options = {}) ⇒ Object
- #n_(*keys, count, &block) ⇒ Object
- #N_(translate) ⇒ Object
- #Nn_(*keys) ⇒ Object
- #np_(context, plural_one, *args, separator: nil, &block) ⇒ Object
- #ns_(*args, &block) ⇒ Object
- #p_(namespace, key, separator = nil, &block) ⇒ Object
- #s_(key, separator = nil, &block) ⇒ Object
Instance Method Details
#_(*keys, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/lucid_i18n/mixin.rb', line 13 def _(*keys, &block) domain = Isomorfeus.i18n_domain locale = Isomorfeus.current_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 |
#current_locale ⇒ Object
8 9 10 |
# File 'lib/lucid_i18n/mixin.rb', line 8 def current_locale Isomorfeus.current_locale end |
#l(object, format = :standard, _options = {}) ⇒ Object
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lucid_i18n/mixin.rb', line 100 def l(object, format = :standard, = {}) c_name = object.class.to_s locale = .delete(:locale) { Isomorfeus.current_locale } = .transform_keys { |k| `Opal.Preact.lower_camelize(k)` } if object.is_a?(Numeric) # options for number formatting: # locale: locale string like 'de' # currency: any currency code (like "EUR", "USD", "INR", etc.) # currency_display or currencyDisplay: "symbol"(default) "code" "name" # locale_matcher or localeMatcher: "best-fit"(default) "lookup" # maximum_fraction_digits or maximumFractionDigits: A number from 0 to 20 (default is 3) # maximum_significant_digits or maximumSignificantDigits: A number from 1 to 21 (default is 21) # minimum_fraction_digits or minimumFractionDigits: A number from 0 to 20 (default is 3) # minimum_integer_digits or minimumIntegerDigits: A number from 1 to 21 (default is 1) # minimum_significant_digits or minimumSignificantDigits: A number from 1 to 21 (default is 21) # style: "decimal"(default) "currency" "percent" # use_grouping or useGrouping: true(default) false `(object).toLocaleString(locale, #{.to_n})` elsif c_name == 'Date' || c_name == 'DateTime' # options for date/time formating: # format: "standard" "full" # locale: locale string like 'de' # time_zone or timeZone: timezone string like 'CET' # time_zone_name or timeZoneName: "long" "short" # date_style or dateStyle: "full" "long" "medium" "short" # time_style or timeStyle: "full" "long" "medium" "short" # format_matcher or formatMatcher: "best-fit"(default) "basic" # locale_matcher or localeMatcher: "best-fit"(default) "lookup" # hour12: false true # hour_cycle hourCycle: "h11" "h12" "h23" "h24" # hour: "2-digit" "numeric" # minute: "2-digit" "numeric" # second: "2-digit" "numeric" # day "2-digit" "numeric" # month: "2-digit" "numeric" "long" "short" "narrow" # weekday: "long" "short" "narrow" # year: "2-digit" "numeric" native_object = object.to_n case format when :standard `native_object.toLocaleDateString(locale, #{.to_n})` when :full [:dateStyle] = 'long' `native_object.toLocaleDateString(locale, #{.to_n})` when :custom `native_object.toLocaleString(locale, #{.to_n})` else `native_object.toLocaleDateString(locale, #{.to_n})` end elsif c_name == 'Time' native_object = object.to_n case format when :standard `native_object.toLocaleString(locale, #{.to_n})` when :full [:dateStyle] = 'long' [:timeStyle] = 'short' `native_object.toLocaleString(locale, #{.to_n})` when :custom `native_object.toLocaleString(locale, #{.to_n})` else `native_object.toLocaleString(locale, #{.to_n})` end else raise "Unknown object type #{object.class} given to #l!" end end |
#n_(*keys, count, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/lucid_i18n/mixin.rb', line 23 def n_(*keys, count, &block) domain = Isomorfeus.i18n_domain locale = Isomorfeus.current_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
70 71 72 |
# File 'lib/lucid_i18n/mixin.rb', line 70 def N_(translate) translate end |
#Nn_(*keys) ⇒ Object
74 75 76 |
# File 'lib/lucid_i18n/mixin.rb', line 74 def Nn_(*keys) keys end |
#np_(context, plural_one, *args, separator: nil, &block) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/lucid_i18n/mixin.rb', line 33 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
40 41 42 43 44 45 46 47 48 |
# File 'lib/lucid_i18n/mixin.rb', line 40 def ns_(*args, &block) domain = Isomorfeus.i18n_domain locale = Isomorfeus.current_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
50 51 52 53 54 55 56 57 58 |
# File 'lib/lucid_i18n/mixin.rb', line 50 def p_(namespace, key, separator = nil, &block) domain = Isomorfeus.i18n_domain locale = Isomorfeus.current_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
60 61 62 63 64 65 66 67 68 |
# File 'lib/lucid_i18n/mixin.rb', line 60 def s_(key, separator = nil, &block) domain = Isomorfeus.i18n_domain locale = Isomorfeus.current_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 |