Sha256: 69c037ff21dc79473d862f30b0aae276afe134b00a134ccbf3bae562d47cf923

Contents?: true

Size: 1.15 KB

Versions: 53

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Worldwide
  class Names
    class << self
      SURNAME_FIRST_LOCALES = ["ja", "km", "ko", "vi", "zh"]
      private_constant :SURNAME_FIRST_LOCALES

      def surname_first?(locale)
        return false if locale.nil?

        SURNAME_FIRST_LOCALES.include?(language_subtag(locale))
      end

      def full(given:, surname:)
        format_name("full", given, surname)
      end

      def greeting(given:, surname:)
        format_name("greeting", given, surname)
      end

      def initials(given:, surname:)
        return if given.nil? && surname.nil?

        names = [given, surname].reject(&:blank?).select do |name|
          I18n.transliterate(name[0]) =~ /[a-zA-Z]/
        end

        names.map { |name| name[0] }
      end

      private

      def format_name(format, given, surname)
        parts = [given, surname].reject(&:blank?)

        case parts.length
        when 2
          Cldr.t("names.#{format}", given: given, surname: surname)
        when 1
          parts[0]
        else
          ""
        end
      end

      def language_subtag(locale)
        locale.to_s.split("-")[0]
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
worldwide-0.6.5 lib/worldwide/names.rb
worldwide-0.6.4 lib/worldwide/names.rb
worldwide-0.6.3 lib/worldwide/names.rb
worldwide-0.6.2 lib/worldwide/names.rb
worldwide-0.6.1 lib/worldwide/names.rb
worldwide-0.6.0 lib/worldwide/names.rb
worldwide-0.5.1 lib/worldwide/names.rb
worldwide-0.5.0 lib/worldwide/names.rb
worldwide-0.4.1 lib/worldwide/names.rb
worldwide-0.4.0 lib/worldwide/names.rb
worldwide-0.3.0 lib/worldwide/names.rb
worldwide-0.2.0 lib/worldwide/names.rb
worldwide-0.1.1 lib/worldwide/names.rb