Skip to content

Commit

Permalink
Merge pull request #21 from BKWLD/ssg-translations
Browse files Browse the repository at this point in the history
Load static translations from json files if they were generated
  • Loading branch information
weotch authored Feb 29, 2024
2 parents b6e4d89 + 421945d commit 203a540
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 15 additions & 0 deletions plugins/fetch-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ export default async function({ i18n, $craft, $config }, localeCode) {
// Get the iso for the selected locale
const locale = i18n.locales.find(locale => locale.code == localeCode)

// If JSON files are generated and this is being invoked on the client, use
// those JSON files.
if ($config.cloak.i18n.generateJson && process.client) {
try {
const generateDir = $config._app.basePath || "/";
const response = await fetch(`${generateDir}i18n/${localeCode}.json`);
if (!response.ok) throw "Static JSON not found";
return await response.json();

// If not found, fallback to the CMS
} catch (e) {
console.error(e);
}
}

// Lookup translations given the CMS
if ($craft) return await fetchCraftTranslations({
$craft,
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4805,11 +4805,6 @@ find-up@^4.0.0, find-up@^4.1.0:
locate-path "^5.0.0"
path-exists "^4.0.0"

flag-icons@^6.2.0:
version "6.4.2"
resolved "https://registry.yarnpkg.com/flag-icons/-/flag-icons-6.4.2.tgz#c608770f55cecf0904ffd17230ca1ec44fa65d53"
integrity sha512-1zlIYDVcn5jQpF5aYWEAeO4WXuLHj4i6lQIZNjn8Kl6rcZperkUj79uHC9OUi2s+IEl9AYskW8JCx83/6KcmLw==

flat@^5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241"
Expand Down Expand Up @@ -10852,7 +10847,7 @@ webpack-dev-middleware@^4.2.0:
range-parser "^1.2.1"
schema-utils "^3.0.0"

"webpack-graphql-loader@github:gpoitch/graphql-loader#gp/refresh":
webpack-graphql-loader@gpoitch/graphql-loader#gp/refresh:
version "1.0.2"
resolved "https://codeload.github.com/gpoitch/graphql-loader/tar.gz/42f0f990a6132ccd5e284d0e6cfeade12728dba2"
dependencies:
Expand Down

0 comments on commit 203a540

Please sign in to comment.