Skip to content

Commit

Permalink
Dealing with multiple locales
Browse files Browse the repository at this point in the history
Basically the idea is to just fetch the things in all the languages by
looping over the available locales.

The results are stored into fields like `en_items` or `es_item`, so we
can just fetch the right ones by checking the current locale.

There's a bit of a mess in the model itself because the
multi-languageness might happen at the single item level or at the
collection level, depending on which query has been used. The check
there is ugly, but restrucuring the collection to match the individual
items is even uglier.

The upshot is that we now pretty much get localized results for free in
bridgetown; we don't need to do anything beside setting up the available
locales like we'd have done anyway.
  • Loading branch information
psguazz committed Feb 26, 2024
1 parent a8bf86f commit 0f7ce40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions lib/dato_cms_graphql/graphql_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,33 @@ def parse(query)
end

def query_for
parse <<~GRAPHQL
query($skip: IntType) {
items: all#{plural_name}(first: #{graphql_page_size}, skip: $skip) {
localized_items = I18n.available_locales.map do |locale|
<<~GRAPHQL
#{locale}_items: all#{plural_name}(locale: #{locale}, first: #{graphql_page_size}, skip: $skip) {
#{fields}
}
GRAPHQL
end

parse <<~GRAPHQL
query($skip: IntType) {
#{localized_items.join("\n")}
}
GRAPHQL
end

def query_for_single
parse <<~GRAPHQL
query {
item: #{single_name} {
localized_item = I18n.available_locales.map do |locale|
<<~GRAPHQL
#{locale}_item: #{single_name}(locale: #{locale}) {
#{fields}
}
GRAPHQL
end

parse <<~GRAPHQL
query {
#{localized_item.join("\n")}
}
GRAPHQL
end
Expand Down Expand Up @@ -109,16 +121,24 @@ def render?
render(true)

def initialize(attributes)
@attributes = JSON.parse(attributes.to_json, object_class: OpenStruct)
@attributes = attributes
end

def localized_attributes
if @attributes.respond_to?(:"#{I18n.locale}_item")
@attributes.send(:"#{I18n.locale}_item")
else
@attributes
end
end

def respond_to_missing?(method, *args)
@attributes.respond_to?(method)
localized_attributes.respond_to?(method)
end

def method_missing(method, *a, &block)
if @attributes.respond_to?(method)
@attributes.send(method, *a, &block)
if localized_attributes.respond_to?(method)
localized_attributes.send(method, *a, &block)
else
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dato_cms_graphql/model_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def each
0.upto(@pages) do |page|
@results = DatoCmsGraphql.query(@query, variables: {skip: @page_size * page})

@results.each do |element|
@results.send(:"#{I18n.locale}_items").each do |element|
yield @model.new(element)
end
end
Expand Down

0 comments on commit 0f7ce40

Please sign in to comment.