Module: Haml::MagicTranslations
- Defined in:
- lib/haml/magic_translations.rb
Overview
This plugin provides “magical translations” in your .haml files. What does it mean? It’s mean that all your raw texts in templates will be automatically translated by GetText, FastGettext or Gettext backend from I18n. No more complicated translation keys and ugly translation methods in views. Now you can only write in your language, nothing more. At the end of your work you can easy find all phrases to translate and generate .po files for it. This type of files are also more readable and easier to translate, thanks to it you save your time with translations.
Examples
Now you can write what you want, and at the end of work you will easy found all phrases to translate. Check out following example:
%p This is my simple dummy text.
%p And more lorem ipsum...
%p= link_to _("This will be also translated"), "#"
Those translations are allso allowing you to use standard Haml interpolation. You can easy write:
%p This is my text with #{"interpolation".upcase}... Great, isn't it?
And text from codes above will be stored in .po files as:
# File test1.haml, line 1
msgid "This is my simple dummy text"
msgstr "This is my dummy translation of dummy text"
# File test2.haml, line 1
msgid "This is my text with %s... Great, isn't it?"
msgstr "Next one %s translation!"
Generator for .po files also includes information where your phrases are placed in filesystem. Thanks to it you don’t forget about any even small word to translate.
Defined Under Namespace
Modules: TemplateMethods Classes: Compiler, Parser
Class Method Summary collapse
-
.disable ⇒ Object
Disable magic translations.
-
.enable(backend = :i18n) ⇒ Object
Enable magic translations using the given backend.
- .enabled? ⇒ Boolean
-
.included(haml) ⇒ Object
:nodoc:.
-
.prepare_i18n_interpolation(str, escape_html = nil) ⇒ Object
It discovers all fragments of code embeded in text and replacing with simple string interpolation parameters.
Class Method Details
.disable ⇒ Object
Disable magic translations
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/haml/magic_translations.rb', line 186 def self.disable return unless @enabled @enabled = false Haml::Options.defaults[:compiler_class] = @original_compiler @original_compiler = nil Haml::Options.defaults[:parser_class] = @original_parser @original_parser = nil Compiler.magic_translations_helpers = nil end |
.enable(backend = :i18n) ⇒ Object
Enable magic translations using the given backend
Supported backends:
:i18n
-
Use I18n::Backend::GetText and I18n::GetText::Helpers from the ‘i18n’
:gettext
-
Use GetText from ‘gettext’
:fast_gettext
-
Use FastGettext::Translation from ‘fast_gettext’
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/haml/magic_translations.rb', line 158 def self.enable(backend = :i18n) return if @enabled case backend when :i18n require 'i18n' require 'i18n/backend/gettext' require 'i18n/gettext/helpers' I18n::Backend::Simple.send(:include, I18n::Backend::Gettext) Compiler.magic_translations_helpers = I18n::Gettext::Helpers when :gettext require 'gettext' Compiler.magic_translations_helpers = GetText when :fast_gettext require 'fast_gettext' Compiler.magic_translations_helpers = FastGettext::Translation else @enabled = false raise ArgumentError, "Backend #{backend.to_s} is not available in Haml::MagicTranslations" end @original_parser = Haml::Options.defaults[:parser_class] Haml::Options.defaults[:parser_class] = Parser @original_compiler = Haml::Options.defaults[:compiler_class] Haml::Options.defaults[:compiler_class] = Compiler @enabled = true end |
.enabled? ⇒ Boolean
146 147 148 |
# File 'lib/haml/magic_translations.rb', line 146 def self.enabled? @enabled end |
.included(haml) ⇒ Object
:nodoc:
45 46 47 48 49 |
# File 'lib/haml/magic_translations.rb', line 45 def self.included(haml) # :nodoc: if defined? Haml::Template Haml::Template.send(:extend, TemplateMethods) end end |
.prepare_i18n_interpolation(str, escape_html = nil) ⇒ Object
It discovers all fragments of code embeded in text and replacing with simple string interpolation parameters.
Example:
Following line…
%p This is some #{'Interpolated'.upcase'} text
… will be translated to:
[ "This is some %s text", "['Interpolated'.upcase]" ]
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 |
# File 'lib/haml/magic_translations.rb', line 118 def self.prepare_i18n_interpolation(str, escape_html = nil) args = [] res = '' str = str. gsub(/\n/, '\n'). gsub(/\r/, '\r'). gsub(/\#/, '\#'). gsub(/\"/, '\"'). gsub(/\\/, '\\\\') rest = Haml::Util.handle_interpolation '"' + str + '"' do |scan| escapes = (scan[2].size - 1) / 2 res << scan.matched[0...-3 - escapes] if escapes % 2 == 1 res << '#{' else content = eval('"' + Haml::Util.balance(scan, ?{, ?}, 1)[0][0...-1] + '"') content = "Haml::Helpers.html_escape(#{content.to_s})" if escape_html args << content res << '%s' end end value = res+rest.gsub(/\\(.)/, '\1').chomp value = value[1..-2] unless value.to_s == '' args = "[#{args.join(', ')}]" [value, args] end |