Sha256: c050e4fe342e26566b10059ab5e35544ce15b508bb20e5795d25b18eb531700e

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 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,
        ) do |corrector|
          corrector.add_default_translation_key(@theme.default_locale_json, key_node.value.split("."), "TODO")
        end
      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

4 entries across 4 versions & 1 rubygems

Version Path
theme-check-1.7.0 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.6.2 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.6.1 lib/theme_check/checks/translation_key_exists.rb
theme-check-1.6.0 lib/theme_check/checks/translation_key_exists.rb