Class: Shipeasy::I18n::LabelFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shipeasy/i18n/label_fetcher.rb

Constant Summary collapse

LABEL_KEY_PREFIX =
"i18n:strings:"

Instance Method Summary collapse

Constructor Details

#initialize(config = Shipeasy.config) ⇒ LabelFetcher

Returns a new instance of LabelFetcher.



10
11
12
# File 'lib/shipeasy/i18n/label_fetcher.rb', line 10

def initialize(config = Shipeasy.config)
  @config = config
end

Instance Method Details

#fetch(profile: @config.profile) ⇒ Object

Fetch a profile's published strings.

Publishing is PROFILE-WIDE: the whole profile is snapshotted into one blob and served from a single endpoint, so there is no sub-profile unit to select. This used to fetch a manifest.json and index it by "chunk" — an endpoint the worker never served, so SSR i18n always came back empty and pages rendered raw {{var}} templates and key fallbacks. (sdk-ts hit the same bug and moved to this endpoint; see its fetchLabelsForSSR.)

Returns the { "locale" => ..., "strings" => { key => value } } blob, or nil when it cannot be fetched.



26
27
28
29
30
31
32
33
34
35
# File 'lib/shipeasy/i18n/label_fetcher.rb', line 26

def fetch(profile: @config.profile)
  cache_key = "#{LABEL_KEY_PREFIX}#{@config.public_key}:#{profile}"
  cache_fetch(cache_key, @config.label_file_cache_ttl) do
    url = "#{@config.cdn_base_url}/sdk/i18n/strings?profile=#{URI.encode_www_form_component(profile)}"
    http_get_json(url, { "X-SDK-Key" => @config.public_key.to_s })
  end
rescue => e
  ::Rails.logger.warn("[Shipeasy::I18n] Failed to fetch labels: #{e.message}") if defined?(::Rails)
  nil
end