translations.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Translations < BaseQueryWithPayload
  4. description 'Translations for a given locale'
  5. argument :locale, String, description: 'The locale to fetch translations for, e.g. "de-de".'
  6. argument :cache_key, String, required: false,
  7. description: 'Cache identifier that the front end used to store the translations when fetching last time. If this is still up-to-date, no data will be returned and the front end should use its cached data.'
  8. field :is_cache_still_valid, Boolean, null: false, description: "If this is true, then the front end's translation cache is still valid and should be used, cacheKey and translation will not be returned."
  9. field :cache_key, String, description: 'Cache key that the front end should use to cache the new translation data.'
  10. field :translations, GraphQL::Types::JSON, description: 'The actual translation data as Hash where keys are source and values target strings (excluding untranslated strings).'
  11. def self.authorize(...)
  12. true # This query should be available for all (including unauthenticated) users.
  13. end
  14. def resolve(locale:, cache_key: nil)
  15. base_query = Translation.where(locale: locale).where('target != source').where.not(target: '')
  16. new_cache_key = base_query.order(updated_at: :desc).take&.updated_at.to_s
  17. if new_cache_key.empty?
  18. raise ActiveRecord::RecordNotFound, "No translations found for locale #{locale}."
  19. end
  20. if new_cache_key == cache_key
  21. return { is_cache_still_valid: true }
  22. end
  23. {
  24. is_cache_still_valid: false,
  25. cache_key: new_cache_key,
  26. translations: base_query.all.pluck(:source, :target).to_h
  27. }
  28. end
  29. end
  30. end