class GetText::Tools::Task

Attributes

domain[RW]

It is a required parameter.

@return [String] Text domain

enable_description[W]

@return [Bool] @see enable_description? For details.

enable_po[W]

@return [Bool] @see enable_po? For details.

files[RW]

It is a required parameter.

@return [Array<String>] Files that have messages.

locales[RW]

It is a required parameter.

@return [Array<String>] Supported locales. It is filled from

{#po_base_directory} by default.
mo_base_directory[RW]

@return [String] Base directory that has generated MOs. MOs

are generated into
`#{mo_base_directory}/#{locale}/LC_MESSAGES/#{domain}.mo`.
msgmerge_options[RW]

@return [Array<String>] Command line options for merging PO with the

latest POT.

@see GetText::Tools::MsgMerge @see `rmsgmerge –help`

namespace_prefix[RW]
package_name[RW]

@return [String, nil] Package name for messages.

package_version[RW]

@return [String, nil] Package version for messages.

po_base_directory[RW]
spec[R]

@return [Gem::Specification, nil] Package information associated

with the task.
xgettext_options[RW]

@return [Array<String>] Command line options for extracting messages

from sources.

@see GetText::Tools::XGetText @see `rxgettext –help`

Public Class Methods

define() { |task| ... } click to toggle source

Define gettext related Rake tasks. Normally, use this method to define tasks because this method is a convenient API.

See accessor APIs how to configure this task.

See {#define} for what task is defined.

@example Recommended usage

require "gettext/tools/task"
# Recommended usage
GetText::Tools::Task.define do |task|
  task.spec = spec
  # ...
end
# Low level API
task = GetText::Tools::Task.new
task.spec = spec
# ...
task.define

@yield [task] Gives the newely created task to the block. @yieldparam [GetText::Tools::Task] task The task that should be

configured.

@see {#define} @return [void]

# File lib/gettext/tools/task.rb, line 70
def define
  task = new
  yield(task)
  task.define
end
new(spec=nil) { |self| ... } click to toggle source

@param [Gem::Specification, nil] spec Package information associated

with the task. Some information are extracted from the spec.

@see spec= What information are extracted from the spec.

# File lib/gettext/tools/task.rb, line 129
def initialize(spec=nil)
  initialize_variables
  self.spec = spec
  if spec
    yield(self) if block_given?
    warn("Use #{self.class.name}.define instead of #{self.class.name}.new(spec).")
    define
  end
end

Public Instance Methods

define() click to toggle source

Define tasks from configured parameters.

TODO: List defined Rake tasks.

# File lib/gettext/tools/task.rb, line 162
def define
  ensure_variables
  validate

  define_file_tasks
  if namespace_prefix
    namespace_recursive namespace_prefix do
      define_named_tasks
    end
  else
    define_named_tasks
  end
end
enable_description?() click to toggle source

If it is true, each task has description. Otherwise, all tasks doesn't have description.

@return [Bool] @since 3.0.1

# File lib/gettext/tools/task.rb, line 181
def enable_description?
  @enable_description
end
enable_po?() click to toggle source

If it is true, PO related tasks are defined. Otherwise, they are not defined.

This parameter is useful to manage PO written by hand.

@return [Bool] @since 3.0.1

# File lib/gettext/tools/task.rb, line 192
def enable_po?
  @enable_po
end
spec=(spec) click to toggle source

Sets package infromation by Gem::Specification. Here is a list for information extracted from the spec:

* {#package_name}
* {#package_version}
* {#domain}
* {#files}

@param [Gem::Specification] spec package information for the

i18n application.
# File lib/gettext/tools/task.rb, line 149
def spec=(spec)
  @spec = spec
  return if @spec.nil?

  @package_name = spec.name
  @package_version = spec.version.to_s
  @domain ||= spec.name
  @files += target_files
end

Private Instance Methods

current_scope() click to toggle source
# File lib/gettext/tools/task.rb, line 418
def current_scope
  scope = Rake.application.current_scope
  if scope.is_a?(Array)
    scope
  else
    if scope.empty?
      []
    else
      [scope.path]
    end
  end
end
define_file_tasks() click to toggle source
# File lib/gettext/tools/task.rb, line 236
def define_file_tasks
  define_pot_file_task

  locales.each do |locale|
    define_po_file_task(locale)
    define_mo_file_task(locale)
  end
end
define_mo_file_task(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 295
def define_mo_file_task(locale)
  _po_file  = po_file(locale)
  mo_dependencies = [_po_file]
  _mo_directory = mo_directory(locale)
  unless File.exist?(_mo_directory)
    directory _mo_directory
    mo_dependencies << _mo_directory
  end
  _mo_file = mo_file(locale)
  file _mo_file => mo_dependencies do
    GetText::Tools::MsgFmt.run(_po_file, "--output", _mo_file)
  end
end
define_mo_tasks() click to toggle source
# File lib/gettext/tools/task.rb, line 358
def define_mo_tasks
  namespace :mo do
    update_tasks = []
    @locales.each do |locale|
      namespace locale do
        desc "Update #{mo_file(locale)}"
        task :update => mo_file(locale)
        update_tasks << (current_scope + ["update"]).join(":")
      end
    end

    desc "Update *.mo"
    task :update => update_tasks
  end
end
define_named_tasks() click to toggle source
# File lib/gettext/tools/task.rb, line 309
def define_named_tasks
  namespace :gettext do
    if @enable_po
      define_pot_tasks
      define_po_tasks
    end

    define_mo_tasks
  end

  desc "Update *.mo"
  task :gettext => (current_scope + ["gettext", "mo", "update"]).join(":")
end
define_po_file_task(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 269
def define_po_file_task(locale)
  return unless @enable_po

  _po_file = po_file(locale)
  po_dependencies = [pot_file]
  _po_directory = po_directory(locale)
  unless File.exist?(_po_directory)
    directory _po_directory
    po_dependencies << _po_directory
  end
  file _po_file => po_dependencies do
    if File.exist?(_po_file)
      command_line = [
        "--update",
      ]
      command_line.concat(@msgmerge_options)
      command_line.concat([_po_file, pot_file])
      GetText::Tools::MsgMerge.run(*command_line)
    else
      GetText::Tools::MsgInit.run("--input", pot_file,
                                  "--output", _po_file,
                                  "--locale", locale.to_s)
    end
  end
end
define_po_tasks() click to toggle source
# File lib/gettext/tools/task.rb, line 330
def define_po_tasks
  namespace :po do
    desc "Add a new locale"
    task :add, [:locale] do |_task, args|
      locale = args.locale || ENV["LOCALE"]
      if locale.nil?
        raise "specify locale name by " +
          "'rake #{_task.name}[${LOCALE}]' or " +
          "rake #{_task.name} LOCALE=${LOCALE}'"
      end
      define_po_file_task(locale)
      Rake::Task[po_file(locale)].invoke
    end

    update_tasks = []
    @locales.each do |locale|
      namespace locale do
        desc "Update #{po_file(locale)}"
        task :update => po_file(locale)
        update_tasks << (current_scope + ["update"]).join(":")
      end
    end

    desc "Update *.po"
    task :update => update_tasks
  end
end
define_pot_file_task() click to toggle source
# File lib/gettext/tools/task.rb, line 245
def define_pot_file_task
  return unless @enable_po

  pot_dependencies = files.dup
  unless File.exist?(po_base_directory)
    directory po_base_directory
    pot_dependencies << po_base_directory
  end
  file pot_file => pot_dependencies do
    command_line = [
      "--output", pot_file,
    ]
    if package_name
      command_line.concat(["--package-name", package_name])
    end
    if package_version
      command_line.concat(["--package-version", package_version])
    end
    command_line.concat(@xgettext_options)
    command_line.concat(files)
    GetText::Tools::XGetText.run(*command_line)
  end
end
define_pot_tasks() click to toggle source
# File lib/gettext/tools/task.rb, line 323
def define_pot_tasks
  namespace :pot do
    desc "Create #{pot_file}"
    task :create => pot_file
  end
end
desc(*args) click to toggle source
Calls superclass method
# File lib/gettext/tools/task.rb, line 231
def desc(*args)
  return unless @enable_description
  super
end
detect_locales() click to toggle source
# File lib/gettext/tools/task.rb, line 404
def detect_locales
  locales = []
  return locales unless File.exist?(po_base_directory)

  Dir.open(po_base_directory) do |dir|
    dir.each do |entry|
      next unless /\A[a-z]{2}(?:_[A-Z]{2})?\z/ =~ entry
      next unless File.directory?(File.join(dir.path, entry))
      locales << entry
    end
  end
  locales
end
ensure_variables() click to toggle source
# File lib/gettext/tools/task.rb, line 213
def ensure_variables
  @locales = detect_locales if @locales.empty?
end
initialize_variables() click to toggle source
# File lib/gettext/tools/task.rb, line 197
def initialize_variables
  @spec = nil
  @package_name = nil
  @package_version = nil
  @locales = []
  @po_base_directory = "po"
  @mo_base_directory = "locale"
  @files = []
  @domain = nil
  @namespace_prefix = nil
  @xgettext_options = []
  @msgmerge_options = []
  @enable_description = true
  @enable_po = true
end
mo_directory(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 386
def mo_directory(locale)
  File.join(mo_base_directory, locale.to_s, "LC_MESSAGES")
end
mo_file(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 390
def mo_file(locale)
  File.join(mo_directory(locale), "#{domain}.mo")
end
namespace_recursive(namespace_spec, &block) click to toggle source
# File lib/gettext/tools/task.rb, line 431
def namespace_recursive(namespace_spec, &block)
  first, rest = namespace_spec.split(/:/, 2)
  namespace first do
    if rest.nil?
      block.call
    else
      namespace_recursive(rest, &block)
    end
  end
end
po_directory(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 378
def po_directory(locale)
  File.join(po_base_directory, locale.to_s)
end
po_file(locale) click to toggle source
# File lib/gettext/tools/task.rb, line 382
def po_file(locale)
  File.join(po_directory(locale), "#{domain}.po")
end
pot_file() click to toggle source
# File lib/gettext/tools/task.rb, line 374
def pot_file
  File.join(po_base_directory, "#{domain}.pot")
end
target_files() click to toggle source
# File lib/gettext/tools/task.rb, line 394
def target_files
  files = @spec.files.find_all do |file|
    /\A\.(?:rb|erb|glade)\z/ =~ File.extname(file)
  end
  files += @spec.executables.collect do |executable|
    "bin/#{executable}"
  end
  files
end
validate() click to toggle source
# File lib/gettext/tools/task.rb, line 217
def validate
  reasons = {}
  if @locales.empty?
    reasons["locales"] = "must set one or more locales"
  end
  if @enable_po and @files.empty?
    reasons["files"] = "must set one or more files"
  end
  if @domain.nil?
    reasons["domain"] = "must set domain"
  end
  raise ValidationError.new(reasons) unless reasons.empty?
end