Sha256: be80c2fa19c5b52b8135bb16379c36373b57efb6627d94215de0bd4763473680

Contents?: true

Size: 1.45 KB

Versions: 41

Compression:

Stored size: 1.45 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Transforms
    class Transliterator
      def self.transliterate(text, source_locale, target_locale)
        new(text, source_locale, target_locale).transliterate
      end

      attr_reader :text, :source_locale, :target_locale

      def initialize(text, source_locale, target_locale)
        @text = text
        @source_locale = locale_klass.parse(source_locale)
        @target_locale = locale_klass.parse(target_locale).maximize
      end

      def transliterate
        result = text.dup

        each_source_locale do |source_locale|
          transform_id = TransformId.find(source_locale, target_locale)

          if transform_id
            transformer = Transformer.get(transform_id)
            result = transformer.transform(result)
          end
        end

        result
      end

      private

      def locale_klass
        TwitterCldr::Shared::Locale
      end

      def each_source_locale
        if source_locale.script
          yield source_locale
        else
          scripts.each do |script|
            locale = locale_klass.new(
              source_locale.language, script, source_locale.region
            )

            yield locale.maximize
          end
        end
      end

      def scripts
        @scripts ||=
          TwitterCldr::Utils::ScriptDetector.detect_scripts(text).scripts
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
twitter_cldr-6.12.1 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.12.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.5 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.4 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.3 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.2 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.1 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.11.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.10.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.9.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.8.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.7.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.6.2 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.6.1 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.6.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.5.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.4.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.3.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.2.0 lib/twitter_cldr/transforms/transliterator.rb
twitter_cldr-6.1.0 lib/twitter_cldr/transforms/transliterator.rb