It is a required parameter.
@return [String] Text domain
@return [Bool] @see enable_description? For details.
@return [Bool] @see enable_po? For details.
It is a required parameter.
@return [Array<String>] Files that have messages.
It is a required parameter.
@return [Array<String>] Supported locales. It is filled from
{#po_base_directory} by default.
@return [String] Base directory that has generated MOs. MOs
are generated into `#{mo_base_directory}/#{locale}/LC_MESSAGES/#{domain}.mo`.
@return [Array<String>] Command line options for creating PO from POT. @see GetText::Tools::MsgInit @see `rmsginit –help`
@return [Array<String>] Command line options for merging PO with the
latest POT.
@see GetText::Tools::MsgMerge @see `rmsgmerge –help`
It is useful when you have multiple domains. You can define tasks for each domains by using different namespace prefix.
It is `nil` by default. It means that tasks are defined at top level.
TODO: example
@return [String] Namespace prefix for tasks defined by this class.
@return [String, nil] Package name for messages.
@return [String, nil] Package version for messages.
@return [Gem::Specification, nil] Package information associated
with the task.
@return [Array<String>] Command line options for extracting messages
from sources.
@see GetText::Tools::XGetText @see `rxgettext –help`
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
@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 145 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
Define tasks from configured parameters.
TODO: List defined Rake tasks.
# File lib/gettext/tools/task.rb, line 178 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
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 197 def enable_description? @enable_description end
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 165 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
# File lib/gettext/tools/task.rb, line 440 def current_scope scope = Rake.application.current_scope if scope.is_a?(Array) scope else if scope.empty? [] else [scope.path] end end end
# File lib/gettext/tools/task.rb, line 253 def define_file_tasks define_pot_file_task locales.each do |locale| define_po_file_task(locale) define_mo_file_task(locale) end end
# File lib/gettext/tools/task.rb, line 317 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
# File lib/gettext/tools/task.rb, line 380 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
# File lib/gettext/tools/task.rb, line 331 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
# File lib/gettext/tools/task.rb, line 286 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 command_line = [ "--input", pot_file, "--output", _po_file, "--locale", locale.to_s, "--no-translator", ] command_line.concat(@msginit_options) GetText::Tools::MsgInit.run(*command_line) end end end
# File lib/gettext/tools/task.rb, line 352 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
# File lib/gettext/tools/task.rb, line 262 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
# File lib/gettext/tools/task.rb, line 345 def define_pot_tasks namespace :pot do desc "Create #{pot_file}" task :create => pot_file end end
# File lib/gettext/tools/task.rb, line 248 def desc(*args) return unless @enable_description super end
# File lib/gettext/tools/task.rb, line 426 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
# File lib/gettext/tools/task.rb, line 230 def ensure_variables @locales = detect_locales if @locales.empty? end
# File lib/gettext/tools/task.rb, line 213 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 = [] @msginit_options = [] @msgmerge_options = [] @enable_description = true @enable_po = true end
# File lib/gettext/tools/task.rb, line 408 def mo_directory(locale) File.join(mo_base_directory, locale.to_s, "LC_MESSAGES") end
# File lib/gettext/tools/task.rb, line 412 def mo_file(locale) File.join(mo_directory(locale), "#{domain}.mo") end
# File lib/gettext/tools/task.rb, line 453 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
# File lib/gettext/tools/task.rb, line 400 def po_directory(locale) File.join(po_base_directory, locale.to_s) end
# File lib/gettext/tools/task.rb, line 404 def po_file(locale) File.join(po_directory(locale), "#{domain}.po") end
# File lib/gettext/tools/task.rb, line 396 def pot_file File.join(po_base_directory, "#{domain}.pot") end
# File lib/gettext/tools/task.rb, line 416 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
# File lib/gettext/tools/task.rb, line 234 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