Class: Jekyll::PotLocalizationPlugin
- Inherits:
-
Generator
- Object
- Generator
- Jekyll::PotLocalizationPlugin
- Defined in:
- lib/jekyll/gettext/plugin.rb
Instance Attribute Summary collapse
-
#site ⇒ Object
readonly
Returns the value of attribute site.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Instance Method Summary collapse
- #add_translated_sites ⇒ Object
- #generate(site) ⇒ Object
- #languages ⇒ Object
- #po_file(language) ⇒ Object
- #pot_file ⇒ Object
- #renew_translations ⇒ Object
- #save_translations(translations) ⇒ Object
- #setup_translations ⇒ Object
- #text_domain ⇒ Object
-
#translations_folder ⇒ Object
use the “translations_folder” tag from the _config.yml or default “_i18n”.
Instance Attribute Details
#site ⇒ Object (readonly)
Returns the value of attribute site.
77 78 79 |
# File 'lib/jekyll/gettext/plugin.rb', line 77 def site @site end |
#translations ⇒ Object (readonly)
Returns the value of attribute translations.
77 78 79 |
# File 'lib/jekyll/gettext/plugin.rb', line 77 def translations @translations end |
Instance Method Details
#add_translated_sites ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/jekyll/gettext/plugin.rb', line 141 def add_translated_sites default_language = languages.first translated_pages = [] site.pages.reject! do |page| to_translate = page.data["translate"] if to_translate languages.each do |language| translated_pages << TranslatedPage.new(page, language, default_language) end end to_translate end site.pages.concat(translated_pages) end |
#generate(site) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/jekyll/gettext/plugin.rb', line 101 def generate(site) @site = site site.pot_localization_plugin = self setup_translations add_translated_sites end |
#languages ⇒ Object
90 91 92 93 94 95 |
# File 'lib/jekyll/gettext/plugin.rb', line 90 def languages unless site.config.include? "languages" site.config["languages"] = ["en"] end site.config["languages"] end |
#po_file(language) ⇒ Object
97 98 99 |
# File 'lib/jekyll/gettext/plugin.rb', line 97 def po_file(language) translations_folder + "/" + language + "/" + text_domain + ".po" end |
#pot_file ⇒ Object
86 87 88 |
# File 'lib/jekyll/gettext/plugin.rb', line 86 def pot_file translations_folder + "/" + text_domain + ".pot" end |
#renew_translations ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/jekyll/gettext/plugin.rb', line 124 def renew_translations languages.each do |language| repos = [ FastGettext::TranslationRepository.build(text_domain, :type=>:logger, :callback=>translations), FastGettext::TranslationRepository.build(text_domain, :path => translations_folder, :type => :po) ] FastGettext.add_text_domain(text_domain, :type=>:chain, :chain=>repos) end FastGettext.text_domain = text_domain end |
#save_translations(translations) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/jekyll/gettext/plugin.rb', line 156 def save_translations(translations) if translations.empty? return end site.config["exclude"] ||= [] unless site.config["exclude"].include? pot_file site.config["exclude"] << pot_file end File.open(pot_file, 'w') do |file| file.write('msgid "" msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: ' + languages.first + '\n" "X-Generator: ' + self.class.name + '\n" ') file.print(GetPomo::PoFile.to_text(translations)) end end |
#setup_translations ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/jekyll/gettext/plugin.rb', line 108 def setup_translations @translations = TranslationLogger.new renew_translations Hooks.register(:site, :pre_render) do |_site, payload| renew_translations end Hooks.register(:site, :post_write) do |_site, payload| if _site == site and not translations.nil? save_translations translations.translations end end end |
#text_domain ⇒ Object
79 80 81 82 83 84 |
# File 'lib/jekyll/gettext/plugin.rb', line 79 def text_domain unless site.config.include? "text_domain" site.config["text_domain"] = "website" end site.config["text_domain"] end |
#translations_folder ⇒ Object
use the “translations_folder” tag from the _config.yml or default “_i18n”
137 138 139 |
# File 'lib/jekyll/gettext/plugin.rb', line 137 def translations_folder site.in_source_dir(site.config["translations_folder"] || "_i18n") end |