class Jekyll::Task::I18n

Constants

VERSION

Attributes

custom_translator[RW]
files[RW]
locales[RW]
translator_email[RW]
translator_name[RW]

Public Class Methods

define() { |task| ... } click to toggle source
# File lib/jekyll/task/i18n.rb, line 28
def define(&block)
  task = new
  yield(task) if block_given?
  task.define
end
new() click to toggle source
# File lib/jekyll/task/i18n.rb, line 42
def initialize
  @po_dir_path = Pathname.new("_po")
  @base_dir_path = Pathname.new(".")
  @locales = []
  @files = []
  @translator_name = nil
  @translator_email = nil
  @yard_locales = {}
  @custom_translator = nil
end

Public Instance Methods

define() click to toggle source
# File lib/jekyll/task/i18n.rb, line 53
def define
  namespace :jekyll do
    namespace :i18n do
      namespace :internal do
        task :force
      end

      namespace :po do
        namespace :edit do
          define_edit_po_update_task
        end

        define_po_update_task
      end

      define_translate_task
    end
  end
end

Private Instance Methods

create_yard_locale(locale) click to toggle source
# File lib/jekyll/task/i18n.rb, line 271
def create_yard_locale(locale)
  yard_locale = YARD::I18n::Locale.new(locale)
  messages = GetText::MO.new
  po_parser = GetText::POParser.new
  po_parser.parse_file(@po_dir_path + "#{locale}.po", messages)
  yard_locale.instance_variable_get("@messages").merge!(messages)
  yard_locale
end
define_edit_po_locale_update_task(locale) click to toggle source
# File lib/jekyll/task/i18n.rb, line 82
def define_edit_po_locale_update_task(locale)
  edit_po_files = []
  @files.each do |target_file|
    path = Path.new(@po_dir_path, locale, Pathname(target_file))
    edit_po_file = path.edit_po_file.to_s
    edit_po_files << edit_po_file
    CLEAN << edit_po_file if path.edit_po_file.exist?

    po_dir = path.po_dir.to_s
    directory po_dir
    dependencies = [target_file, po_dir]
    dependencies << "i18n:internal:force" if po_file_is_updated?(path)
    file edit_po_file => dependencies do
      relative_base_path = @base_dir_path.relative_path_from(path.po_dir)
      generator = YARD::I18n::PotGenerator.new(relative_base_path.to_s)
      yard_file = YARD::CodeObjects::ExtraFileObject.new(target_file)
      generator.parse_files([yard_file])
      path.pot_file.open("w") do |pot_file|
        pot_file.puts(generator.generate)
      end
      if po_file_is_updated?(path)
        rm_f(path.edit_po_file.to_s)
        rm_f(path.all_po_file.to_s)
      end
      unless path.edit_po_file.exist?
        if path.po_file.exist?
          msgcat("--output", path.edit_po_file.to_s,
                 "--update-po-revision-date",
                 path.po_file.to_s)
        else
          msginit("--input", path.pot_file.to_s,
                  "--output", path.edit_po_file.to_s,
                  "--locale", locale)
        end
      end

      edit_po_file_mtime = path.edit_po_file.mtime
      msgmerge("--update",
               "--sort-by-file",
               "--no-wrap",
               path.edit_po_file.to_s,
               path.pot_file.to_s)
      if path.po_file.exist? and path.po_file.mtime > edit_po_file_mtime
        msgmerge("--output", path.edit_po_file.to_s,
                 "--sort-by-file",
                 "--no-obsolete-entries",
                 path.po_file.to_s,
                 path.edit_po_file.to_s)
        msgcat("--output", path.edit_po_file.to_s,
               "--update-po-revision-date",
               path.edit_po_file.to_s)
      end
      if path.all_po_file.exist?
        msgmerge("--output", path.edit_po_file.to_s,
                 "--sort-by-file",
                 "--no-fuzzy-matching",
                 "--no-obsolete-entries",
                 path.all_po_file.to_s,
                 path.edit_po_file.to_s)
        msgcat("--output", path.edit_po_file.to_s,
               "--update-po-revision-date",
               path.edit_po_file.to_s)
      end
    end
  end

  desc "Update .edit.po files for [#{locale}] locale"
  task :update => edit_po_files
end
define_edit_po_update_task() click to toggle source
# File lib/jekyll/task/i18n.rb, line 74
def define_edit_po_update_task
  @locales.each do |locale|
    namespace locale do
      define_edit_po_locale_update_task(locale)
    end
  end
end
define_locale_translate_task(locale) click to toggle source
# File lib/jekyll/task/i18n.rb, line 230
def define_locale_translate_task(locale)
  translated_files = []
  @files.each do |target_file|
    path = Path.new(@po_dir_path, locale, Pathname(target_file))
    translated_file = path.translated_file.to_s
    translated_files << translated_file

    translated_file_dir = path.translated_file.parent.to_s
    directory translated_file_dir
    dependencies = [
      target_file,
      "i18n:po:#{locale}:update",
      translated_file_dir,
    ]
    file translated_file => dependencies do
      File.open(target_file) do |input|
        text = translate(input, locale, path)
        File.open(translated_file, "w") do |output|
          output.puts(text)
        end
      end
    end
  end

  desc "Translate files for [#{locale}] locale"
  task :translate => translated_files
end
define_po_locale_update_task(locale) click to toggle source
# File lib/jekyll/task/i18n.rb, line 172
def define_po_locale_update_task(locale)
  po_files = []
  @files.each do |target_file|
    path = Path.new(@po_dir_path, locale, Pathname(target_file))
    po_file = path.po_file.to_s
    po_files << po_file

    CLEAN << path.time_stamp_file.to_s if path.time_stamp_file.exist?
    file po_file => [path.edit_po_file.to_s] do
      msgcat("--output", po_file,
             "--no-fuzzy",
             "--sort-by-file",
             "--no-all-comments",
             "--no-report-warning",
             "--no-obsolete-entries",
             "--remove-header-field=Report-Msgid-Bugs-To",
             "--remove-header-field=Last-Translator",
             "--remove-header-field=Language-Team",
             "--remove-header-field=POT-Creation-Date",
             "--remove-header-field=PO-Revision-Date",
             path.edit_po_file.to_s)
      touch(path.time_stamp_file.to_s)
    end
  end

  all_po_file_path = Path.new(@po_dir_path, locale).all_po_file
  all_po_file = all_po_file_path.to_s
  CLEAN << all_po_file if all_po_file_path.exist?
  file all_po_file => po_files do
    sorted_po_files = po_files.sort_by do |po_file|
      -File.mtime(po_file).to_f
    end
    msgcat("--output", all_po_file,
           "--no-fuzzy",
           "--no-all-comments",
           "--sort-by-msgid",
           "--no-obsolete-entries",
           *sorted_po_files)
  end

  desc "Update .po files for [#{locale}] locale"
  task :update => all_po_file
end
define_po_update_task() click to toggle source
# File lib/jekyll/task/i18n.rb, line 158
def define_po_update_task
  @locales.each do |locale|
    namespace locale do
      define_po_locale_update_task(locale)
    end
  end

  all_update_tasks = @locales.collect do |locale|
    "i18n:po:#{locale}:update"
  end
  desc "Update .po files for all locales"
  task :update => all_update_tasks
end
define_translate_task() click to toggle source
# File lib/jekyll/task/i18n.rb, line 216
def define_translate_task
  @locales.each do |locale|
    namespace locale do
      define_locale_translate_task(locale)
    end
  end

  all_translate_tasks = @locales.collect do |locale|
    "i18n:#{locale}:translate"
  end
  desc "Translate files for all locales"
  task :translate => all_translate_tasks
end
msgcat(*arguments) click to toggle source
# File lib/jekyll/task/i18n.rb, line 303
def msgcat(*arguments)
  GetText::Tools::MsgCat.run(*arguments)
end
msginit(*arguments) click to toggle source
# File lib/jekyll/task/i18n.rb, line 280
def msginit(*arguments)
  GetText::Tools::MsgInit.run(*(msginit_options + arguments))
end
msginit_options() click to toggle source
# File lib/jekyll/task/i18n.rb, line 284
def msginit_options
  options = []
  if @translator_name or @translator_email
    if @translator_name
      options.concat(["--translator-name", @translator_name])
    end
    if @translator_email
      options.concat(["--translator-email", @translator_email])
    end
  else
    options << "--no-translator"
  end
  options
end
msgmerge(*arguments) click to toggle source
# File lib/jekyll/task/i18n.rb, line 299
def msgmerge(*arguments)
  GetText::Tools::MsgMerge.run(*arguments)
end
po_file_is_updated?(path) click to toggle source
# File lib/jekyll/task/i18n.rb, line 152
def po_file_is_updated?(path)
  return false unless path.po_file.exist?
  return false unless path.time_stamp_file.exist?
  path.po_file.mtime > path.time_stamp_file.mtime
end
translate(input, locale, path) click to toggle source
# File lib/jekyll/task/i18n.rb, line 258
def translate(input, locale, path)
  text = YARD::I18n::Text.new(input)
  translated_text = text.translate(yard_locale(locale))
  if @custom_translator
    translated_text = @custom_translator.call(input, translated_text, path)
  end
  translated_text
end
yard_locale(locale) click to toggle source
# File lib/jekyll/task/i18n.rb, line 267
def yard_locale(locale)
  @yard_locales[locale] ||= create_yard_locale(locale)
end