Skip to content

Commit

Permalink
Merge branch 'main' into 931-docs-add-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvlach authored Jan 13, 2024
2 parents a5a0491 + 9b06b09 commit d208e7e
Show file tree
Hide file tree
Showing 15 changed files with 2,360 additions and 3,295 deletions.
132 changes: 132 additions & 0 deletions agdb_web/composables/useI18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { computed, ref } from "vue";
import localeData from "~/locales";
import i18nConfig from "~/i18n.config";

type Locale = {
code: string;
iso: string;
name: string;
};

const localeCode = ref<string>(i18nConfig.defaultLocale);

const fallbackLocaleCode = ref<string>(i18nConfig.defaultLocale);

const locales: Locale[] = i18nConfig.locales;

const localeExists = (locale: string): boolean => {
return locales.some((localeItem) => localeItem.code === locale);
};

const currentLocale = computed(() =>
locales.find((locale) => locale.code === localeCode.value),
);

const fallbackLocale = computed(() =>
locales.find((locale) => locale.code === fallbackLocaleCode.value),
);

const messages = ref(new Map<string, string>());

const fallbackMessages = ref(new Map<string, string>());

const currentPagePath = ref("/");

type MessagesStructure = { [key: string]: string | MessagesStructure };

const iterateMessages = (
prefix: string | null,
obj: MessagesStructure,
map: Map<string, string>,
): void => {
for (const [key, value] of Object.entries(obj)) {
const keyName = prefix ? `${prefix}.${key}` : key;
if (typeof value === "string") {
map.set(keyName, value);
} else {
iterateMessages(keyName, value, map);
}
}
};

const loadMessages = (): void => {
messages.value = new Map(fallbackMessages.value);
if (!Object.prototype.hasOwnProperty.call(localeData, localeCode.value)) {
return;
}
const localeMessage: MessagesStructure =
localeData[localeCode.value as keyof typeof localeData];
iterateMessages(null, localeMessage, messages.value);
};

const loadFallbackMessages = (): void => {
if (
!Object.prototype.hasOwnProperty.call(
localeData,
fallbackLocaleCode.value,
)
) {
return;
}
const fallbackLocaleMessages: MessagesStructure =
localeData[fallbackLocaleCode.value as keyof typeof localeData];
iterateMessages(null, fallbackLocaleMessages, fallbackMessages.value);
};

const t = (key: string): string => {
return messages.value.get(key) || "";
};

const hasPathLocale = (path: string): boolean => {
return locales.some(
(locale) =>
path.startsWith(`/${locale.code}/`) || path === `/${locale.code}`,
);
};

const getLocalePath = (path: string): string => {
const hasLocale = hasPathLocale(path);
if (localeCode.value === fallbackLocale.value?.code) {
const newPath = hasLocale
? path.replace(`/${localeCode.value}`, "")
: path;
return newPath === "" ? "/" : newPath;
}
return hasLocale ? path : `/${localeCode.value}${path}`;
};

const setLocaleCode = (code: string): void => {
if (code === localeCode.value || !localeExists(code)) {
return;
}
localeCode.value = code;
loadMessages();
};

const setDefaultLocaleCode = (): void => {
localeCode.value = fallbackLocale.value?.code || "en";
messages.value = new Map(fallbackMessages.value);
};

const initI18n = (): void => {
if (fallbackMessages.value.size > 0) return;
loadFallbackMessages();
};

export default function useI18n() {
return {
localeCode,
fallbackLocaleCode,
localeExists,
currentLocale,
fallbackLocale,
loadMessages,
loadFallbackMessages,
t,
getLocalePath,
setLocaleCode,
setDefaultLocaleCode,
currentPagePath,
initI18n,
};
}
1 change: 1 addition & 0 deletions agdb_web/content/en/enterprise/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Enterprise
Empty file added agdb_web/content/index.md
Empty file.
3 changes: 0 additions & 3 deletions agdb_web/i18n.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ export default {
name: "Čeština",
},
],
langDir: "locales/",
fallbackLocale: "en",
strategy: "prefix_and_default",
};
2 changes: 1 addition & 1 deletion agdb_web/locales/cs-CZ.json → agdb_web/locales/cs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"urls": {
"url": {
"about": "/o-agdb",
"contact": "/kontakt"
},
Expand Down
2 changes: 1 addition & 1 deletion agdb_web/locales/en-US.json → agdb_web/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"urls": {
"url": {
"about": "/about",
"contact": "/contact"
},
Expand Down
7 changes: 7 additions & 0 deletions agdb_web/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import en from "./en.json";
import cs from "./cs.json";

export default {
en,
cs,
};
18 changes: 14 additions & 4 deletions agdb_web/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { resolve } from "node:path";
import { fileURLToPath, URL } from "node:url";
import i18nConfig from "./i18n.config";

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devServer: {
port: 5001,
},
devtools: { enabled: true },
modules: [
["@nuxt/content", { documentDriven: true }],
[
"@nuxt/content",
{
documentDriven: { injectPage: false },
sources: {
en: {
base: resolve(__dirname, "content/en"),
driver: "fs",
prefix: "/",
},
},
},
],
"@nuxt/test-utils/module",
["@nuxtjs/i18n", i18nConfig],
],
vite: {
resolve: {
Expand Down
Loading

0 comments on commit d208e7e

Please sign in to comment.