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: EngineMethods, TemplateMethods
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
127 128 129 130 |
# File 'lib/haml/magic_translations.rb', line 127 def self.disable EngineMethods.magic_translations_helpers = nil @enabled = false 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’
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/haml/magic_translations.rb', line 105 def self.enable(backend = :i18n) case backend when :i18n require 'i18n' require 'i18n/backend/gettext' require 'i18n/gettext/helpers' I18n::Backend::Simple.send(:include, I18n::Backend::Gettext) EngineMethods.magic_translations_helpers = I18n::Gettext::Helpers when :gettext require 'gettext' EngineMethods.magic_translations_helpers = GetText when :fast_gettext require 'fast_gettext' EngineMethods.magic_translations_helpers = FastGettext::Translation else @enabled = false raise ArgumentError, "Backend #{backend.to_s} is not available in Haml::MagicTranslations" end @enabled = true end |
.enabled? ⇒ Boolean
93 94 95 |
# File 'lib/haml/magic_translations.rb', line 93 def self.enabled? @enabled end |
.included(haml) ⇒ Object
:nodoc:
45 46 47 48 49 50 |
# File 'lib/haml/magic_translations.rb', line 45 def self.included(haml) # :nodoc: haml.send(:include, EngineMethods) 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]" ]
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/haml/magic_translations.rb', line 65 def self.prepare_i18n_interpolation(str, escape_html = nil) args = [] res = '' str = str. gsub(/\n/, '\n'). gsub(/\r/, '\r'). gsub(/\#/, '\#'). gsub(/\"/, '\"'). gsub(/\\/, '\\\\') rest = Haml::Shared.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::Shared.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 |