Sha256: 9aabc2170e0a542fea1624dfa70ef20f6011e1c2fcc92fcc1e7b143c0b44c4ba

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  module SystemTranslations
    extend self

    def translations
      @translations ||= YAML.load(File.read("#{__dir__}/../../../data/shopify_translation_keys.yml")).to_set
    end

    def include?(key)
      translations.include?(key)
    end
  end

  class TranslationKeyExists < LiquidCheck
    severity :error
    category :translation
    doc docs_url(__FILE__)

    def on_variable(node)
      return unless @theme.default_locale_json&.content&.is_a?(Hash)

      return unless node.value.filters.any? { |name, _| name == "t" || name == "translate" }
      return unless (key_node = node.children.first)
      return unless key_node.value.is_a?(String)

      unless key_exists?(key_node.value) || SystemTranslations.include?(key_node.value)
        add_offense(
          "'#{key_node.value}' does not have a matching entry in '#{@theme.default_locale_json.relative_path}'",
          node: node,
          markup: key_node.value,
        )
      end
    end

    private

    def key_exists?(key)
      pointer = @theme.default_locale_json.content
      key.split(".").each do |token|
        return false unless pointer.key?(token)
        pointer = pointer[token]
      end

      true
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
theme-check-1.5.2 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.5.1 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.5.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.4.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.3.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.2.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.1.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.0.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.10.2 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.10.1 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.10.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.9.1 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.9.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.8.3 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.8.2 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.8.1 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.8.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.7.3 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.7.2 lib/theme_check/checks/translation_key_exists.rb
theme-check-0.7.1 lib/theme_check/checks/translation_key_exists.rb