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-1.14.1 lib/worldwide/names.rb
worldwide-1.14.0 lib/worldwide/names.rb
worldwide-1.13.0 lib/worldwide/names.rb
worldwide-1.12.0 lib/worldwide/names.rb
worldwide-1.11.1 lib/worldwide/names.rb
worldwide-1.11.0 lib/worldwide/names.rb
worldwide-1.10.0 lib/worldwide/names.rb
worldwide-1.9.0 lib/worldwide/names.rb
worldwide-1.8.0 lib/worldwide/names.rb
worldwide-1.7.5 lib/worldwide/names.rb
worldwide-1.7.4 lib/worldwide/names.rb
worldwide-1.7.3 lib/worldwide/names.rb
worldwide-1.7.2 lib/worldwide/names.rb
worldwide-1.7.1 lib/worldwide/names.rb
worldwide-1.7.0 lib/worldwide/names.rb
worldwide-1.6.2 lib/worldwide/names.rb
worldwide-1.6.1 lib/worldwide/names.rb
worldwide-1.6.0 lib/worldwide/names.rb
worldwide-1.5.0 lib/worldwide/names.rb
worldwide-1.4.1 lib/worldwide/names.rb