diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/lib/rdoc/i18n |
Fresh start
Diffstat (limited to 'jni/ruby/lib/rdoc/i18n')
-rw-r--r-- | jni/ruby/lib/rdoc/i18n/locale.rb | 101 | ||||
-rw-r--r-- | jni/ruby/lib/rdoc/i18n/text.rb | 125 |
2 files changed, 226 insertions, 0 deletions
diff --git a/jni/ruby/lib/rdoc/i18n/locale.rb b/jni/ruby/lib/rdoc/i18n/locale.rb new file mode 100644 index 0000000..e98732e --- /dev/null +++ b/jni/ruby/lib/rdoc/i18n/locale.rb @@ -0,0 +1,101 @@ +## +# A message container for a locale. +# +# This object provides the following two features: +# +# * Loads translated messages from .po file. +# * Translates a message into the locale. + +class RDoc::I18n::Locale + + @@locales = {} # :nodoc: + + class << self + + ## + # Returns the locale object for +locale_name+. + + def [](locale_name) + @@locales[locale_name] ||= new(locale_name) + end + + ## + # Sets the locale object for +locale_name+. + # + # Normally, this method is not used. This method is useful for + # testing. + + def []=(locale_name, locale) + @@locales[locale_name] = locale + end + + end + + ## + # The name of the locale. It uses IETF language tag format + # +[language[_territory][.codeset][@modifier]]+. + # + # See also {BCP 47 - Tags for Identifying + # Languages}[http://tools.ietf.org/rfc/bcp/bcp47.txt]. + + attr_reader :name + + ## + # Creates a new locale object for +name+ locale. +name+ must + # follow IETF language tag format. + + def initialize(name) + @name = name + @messages = {} + end + + ## + # Loads translation messages from +locale_directory+/+@name+/rdoc.po + # or +locale_directory+/+@name+.po. The former has high priority. + # + # This method requires gettext gem for parsing .po file. If you + # don't have gettext gem, this method doesn't load .po file. This + # method warns and returns +false+. + # + # Returns +true+ if succeeded, +false+ otherwise. + + def load(locale_directory) + return false if @name.nil? + + po_file_candidates = [ + File.join(locale_directory, @name, 'rdoc.po'), + File.join(locale_directory, "#{@name}.po"), + ] + po_file = po_file_candidates.find do |po_file_candidate| + File.exist?(po_file_candidate) + end + return false unless po_file + + begin + require 'gettext/po_parser' + require 'gettext/mo' + rescue LoadError + warn('Need gettext gem for i18n feature:') + warn(' gem install gettext') + return false + end + + po_parser = GetText::POParser.new + messages = GetText::MO.new + po_parser.report_warning = false + po_parser.parse_file(po_file, messages) + + @messages.merge!(messages) + + true + end + + ## + # Translates the +message+ into locale. If there is no tranlsation + # messages for +message+ in locale, +message+ itself is returned. + + def translate(message) + @messages[message] || message + end + +end diff --git a/jni/ruby/lib/rdoc/i18n/text.rb b/jni/ruby/lib/rdoc/i18n/text.rb new file mode 100644 index 0000000..ee5c66a --- /dev/null +++ b/jni/ruby/lib/rdoc/i18n/text.rb @@ -0,0 +1,125 @@ +## +# An i18n supported text. +# +# This object provides the following two features: +# +# * Extracts translation messages from wrapped raw text. +# * Translates wrapped raw text in specified locale. +# +# Wrapped raw text is one of String, RDoc::Comment or Array of them. + +class RDoc::I18n::Text + + ## + # Creates a new i18n supported text for +raw+ text. + + def initialize(raw) + @raw = raw + end + + ## + # Extracts translation target messages and yields each message. + # + # Each yielded message is a Hash. It consists of the followings: + # + # :type :: :paragraph + # :paragraph :: String (The translation target message itself.) + # :line_no :: Integer (The line number of the :paragraph is started.) + # + # The above content may be added in the future. + + def extract_messages + parse do |part| + case part[:type] + when :empty_line + # ignore + when :paragraph + yield(part) + end + end + end + + # Translates raw text into +locale+. + def translate(locale) + translated_text = '' + parse do |part| + case part[:type] + when :paragraph + translated_text << locale.translate(part[:paragraph]) + when :empty_line + translated_text << part[:line] + else + raise "should not reach here: unexpected type: #{type}" + end + end + translated_text + end + + private + def parse(&block) + paragraph = '' + paragraph_start_line = 0 + line_no = 0 + + each_line(@raw) do |line| + line_no += 1 + case line + when /\A\s*\z/ + if paragraph.empty? + emit_empty_line_event(line, line_no, &block) + else + paragraph << line + emit_paragraph_event(paragraph, paragraph_start_line, line_no, + &block) + paragraph = '' + end + else + paragraph_start_line = line_no if paragraph.empty? + paragraph << line + end + end + + unless paragraph.empty? + emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block) + end + end + + def each_line(raw, &block) + case raw + when RDoc::Comment + raw.text.each_line(&block) + when Array + raw.each do |comment, location| + each_line(comment, &block) + end + else + raw.each_line(&block) + end + end + + def emit_empty_line_event(line, line_no) + part = { + :type => :empty_line, + :line => line, + :line_no => line_no, + } + yield(part) + end + + def emit_paragraph_event(paragraph, paragraph_start_line, line_no, &block) + paragraph_part = { + :type => :paragraph, + :line_no => paragraph_start_line, + } + match_data = /(\s*)\z/.match(paragraph) + if match_data + paragraph_part[:paragraph] = match_data.pre_match + yield(paragraph_part) + emit_empty_line_event(match_data[1], line_no, &block) + else + paragraph_part[:paragraph] = paragraph + yield(paragraph_part) + end + end + +end |