Sha256: 3681cfe4302674717ad68061d708fbfc59b9b9c3fa67091697b5e6cf91f10da7

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  module SystemTranslations
    extend self

    def translations
      @translations ||= begin
        # loaded as a Set because the include? lookup will be much faster.
        YAML.load(File.read("#{__dir__}/../../../data/shopify_translation_keys.yml")).to_set
      end
    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

1 entries across 1 versions & 1 rubygems

Version Path
theme-check-0.6.0 lib/theme_check/checks/translation_key_exists.rb