Module: AnnoTranslate
- Defined in:
- lib/annotranslate.rb,
lib/version.rb,
lib/import_export.rb
Overview
Extentions to make internationalization (i18n) of a Rails application simpler. Support the method translate (or shorter t) in models/view/controllers/mailers.
Defined Under Namespace
Modules: Assertions, I18nExtensions Classes: AnnoTranslateError, TagHelper, TranslationsExporter, TranslationsImporter
Constant Summary collapse
- VERSION =
"0.2.0"- @@log_file =
Define empty logger until instanced
nil- @@logger =
nil- @@pseudo_translate =
Whether to pseudo-translate all fetched strings
false- @@pseudo_prepend =
Pseudo-translation text to prend to fetched strings. Used as a visible marker. Default is “[”
"["- @@pseudo_append =
Pseudo-translation text to append to fetched strings. Used as a visible marker. Default is “]”
"]"- @@missing_translation_callback =
An optional callback to be notified when there are missing translations in views
nil
Class Method Summary collapse
-
.fallback(enable = true) ⇒ Object
When fallback mode is enabled if a key cannot be found in the set locale, it uses the default locale.
-
.fallback? ⇒ Boolean
If fallback mode is enabled.
-
.log ⇒ Object
Plugin-specific Rails logger.
-
.missing_translation_callback(exception, key, options = {}) ⇒ Object
Invokes the missing translation callback, if it is defined.
-
.pseudo_append ⇒ Object
Pseudo-translation text to append to fetched strings.
-
.pseudo_append=(v) ⇒ Object
Set the pseudo-translation text to append to fetched strings.
-
.pseudo_prepend ⇒ Object
Pseudo-translation text to prepend to fetched strings.
-
.pseudo_prepend=(v) ⇒ Object
Set the pseudo-translation text to prepend to fetched strings.
-
.pseudo_translate(enable = true) ⇒ Object
Toggle a pseudo-translation mode that will prepend / append special text to all fetched strings.
-
.pseudo_translate? ⇒ Boolean
If pseudo-translated is enabled.
- .scope_key_by_partial(key, path) ⇒ Object
-
.set_missing_translation_callback(&block) ⇒ Object
Set an optional block that gets called when there’s a missing translation within a view.
-
.strict_mode(enable_strict = true) ⇒ Object
Toggle whether to true an exception on all
MissingTranslationDataexceptions Useful during testing to ensure all keys are found. -
.strict_mode? ⇒ Boolean
Get if it is in strict mode.
- .tag_helper ⇒ Object
-
.translate(key, options = {}) ⇒ Object
(also: t)
Generic translate method that mimics
I18n.translate(e.g. no automatic scoping) but includes locale fallback and strict mode behavior. - .translate_with_annotation(scope, path, key, options = {}) ⇒ Object
Class Method Details
.fallback(enable = true) ⇒ Object
When fallback mode is enabled if a key cannot be found in the set locale, it uses the default locale. So, for example, if an app is mostly localized to Spanish (:es), but a new page is added then Spanish users will continue to see mostly Spanish content but the English version (assuming the default_locale is :en) for the new page that has not yet been translated to Spanish.
168 169 170 |
# File 'lib/annotranslate.rb', line 168 def self.fallback(enable = true) @@fallback_mode = enable end |
.fallback? ⇒ Boolean
If fallback mode is enabled
173 174 175 |
# File 'lib/annotranslate.rb', line 173 def self.fallback? @@fallback_mode end |
.log ⇒ Object
Plugin-specific Rails logger
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/annotranslate.rb', line 37 def self.log # Create logger if it doesn't exist yet if @@logger.nil? log_file = Rails.root.join('log', 'annotranslate.log').to_s puts "AnnoTranslate is logging to: #{log_file}" @@logger = Logger.new(File.open(log_file, "w", encoding: 'UTF-8')) @@logger.info "Started new AnnoTranslate logging session!" end # Return the logger instance @@logger end |
.missing_translation_callback(exception, key, options = {}) ⇒ Object
Invokes the missing translation callback, if it is defined
60 61 62 |
# File 'lib/annotranslate.rb', line 60 def self.missing_translation_callback(exception, key, = {}) #:nodoc: @@missing_translation_callback.call(exception, key, ) if !@@missing_translation_callback.nil? end |
.pseudo_append ⇒ Object
Pseudo-translation text to append to fetched strings. Used as a visible marker. Default is “]]”
223 224 225 |
# File 'lib/annotranslate.rb', line 223 def self.pseudo_append @@pseudo_append end |
.pseudo_append=(v) ⇒ Object
Set the pseudo-translation text to append to fetched strings. Used as a visible marker.
229 230 231 |
# File 'lib/annotranslate.rb', line 229 def self.pseudo_append=(v) @@pseudo_append = v end |
.pseudo_prepend ⇒ Object
Pseudo-translation text to prepend to fetched strings. Used as a visible marker. Default is “[[”
211 212 213 |
# File 'lib/annotranslate.rb', line 211 def self.pseudo_prepend @@pseudo_prepend end |
.pseudo_prepend=(v) ⇒ Object
Set the pseudo-translation text to prepend to fetched strings. Used as a visible marker.
217 218 219 |
# File 'lib/annotranslate.rb', line 217 def self.pseudo_prepend=(v) @@pseudo_prepend = v end |
.pseudo_translate(enable = true) ⇒ Object
Toggle a pseudo-translation mode that will prepend / append special text to all fetched strings. This is useful during testing to view pages and visually confirm that strings have been fully extracted into locale bundles.
200 201 202 |
# File 'lib/annotranslate.rb', line 200 def self.pseudo_translate(enable = true) @@pseudo_translate = enable end |
.pseudo_translate? ⇒ Boolean
If pseudo-translated is enabled
205 206 207 |
# File 'lib/annotranslate.rb', line 205 def self.pseudo_translate? @@pseudo_translate end |
.scope_key_by_partial(key, path) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/annotranslate.rb', line 138 def self.scope_key_by_partial(key, path) if key.to_s.first == "." if path path.gsub(%r{/_?}, ".") + key.to_s else error = "Cannot use t(#{key.inspect}) shortcut because path is not available" AnnoTranslate.log.error error raise error end else key end end |
.set_missing_translation_callback(&block) ⇒ Object
Set an optional block that gets called when there’s a missing translation within a view. This can be used to log missing translations in production.
Block takes two required parameters:
-
exception (original I18n::MissingTranslationData that was raised for the failed translation)
-
key (key that was missing)
-
options (hash of options sent to annotranslate)
Example:
set_missing_translation_callback do |ex, key, |
logger.info("Failed to find #{key}")
end
75 76 77 |
# File 'lib/annotranslate.rb', line 75 def self.set_missing_translation_callback(&block) @@missing_translation_callback = block end |
.strict_mode(enable_strict = true) ⇒ Object
Toggle whether to true an exception on all MissingTranslationData exceptions Useful during testing to ensure all keys are found. Passing true enables strict mode, false installs the default exception handler which does not raise on MissingTranslationData
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/annotranslate.rb', line 181 def self.strict_mode(enable_strict = true) @@strict_mode = enable_strict if enable_strict # Switch to using contributed exception handler I18n.exception_handler = :strict_i18n_exception_handler else I18n.exception_handler = :default_exception_handler end end |
.strict_mode? ⇒ Boolean
Get if it is in strict mode
193 194 195 |
# File 'lib/annotranslate.rb', line 193 def self.strict_mode? @@strict_mode end |
.tag_helper ⇒ Object
55 56 57 |
# File 'lib/annotranslate.rb', line 55 def self.tag_helper TagHelper.instance end |
.translate(key, options = {}) ⇒ Object Also known as: t
Generic translate method that mimics I18n.translate (e.g. no automatic scoping) but includes locale fallback and strict mode behavior.
156 157 158 |
# File 'lib/annotranslate.rb', line 156 def translate(key, ={}) AnnoTranslate.translate_with_annotation(key, @virtual_path, ) end |
.translate_with_annotation(scope, path, key, options = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 |
# File 'lib/annotranslate.rb', line 79 def self.translate_with_annotation(scope, path, key, ={}) AnnoTranslate.log.info "translate_with_annotation(scope=#{scope}, path=#{path}, key=#{key}, options=#{.inspect})" scope ||= [] # guard against nil scope # Let Rails 2.3 handle keys starting with "." # raise AnnoTranslateError, "Skip keys with leading dot" if key.to_s.first == "." # Keep the original options clean original_scope = scope.dup = {}.merge() # Raise to know if the key was found [:raise] = true # Remove any default value when searching with scope .delete(:default) str = nil # the string being looked for # Apply scoping to partial keys key = AnnoTranslate.scope_key_by_partial(key, path) # Loop through each scope until a string is found. # Example: starts with scope of [:blog_posts :show] then tries scope [:blog_posts] then # without any automatically added scope ("[]"). while str.nil? # Set scope to use for search [:scope] = scope begin # try to find key within scope (dup the options because I18n modifies the hash) str = I18n.translate(key, .dup) rescue I18n::MissingTranslationData => exc # did not find the string, remove a layer of scoping. # break when there are no more layers to remove (pop returns nil) break if scope.pop.nil? end end # If a string is not yet found, potentially check the default locale if in fallback mode. if str.nil? && AnnoTranslate.fallback? && (I18n.locale != I18n.default_locale) && [:locale].nil? # Recurse original request, but in the context of the default locale str ||= AnnoTranslate.translate_with_scope(original_scope, key, .merge({:locale => I18n.default_locale})) end # If a string was still not found, fall back to trying original request (gets default behavior) str ||= I18n.translate(key, ) # If pseudo-translating, prepend / append marker text if AnnoTranslate.pseudo_translate? && !str.nil? str = AnnoTranslate.pseudo_prepend + str + AnnoTranslate.pseudo_append end tag = tag_helper.content_tag('span', str, :class => 'translation_annotated', :title => key) AnnoTranslate.log.info " => full_key=#{key}, translation=#{str}, tag=#{tag.inspect}" tag end |