Skip to content

Commit

Permalink
fix: import localizeUrl in HeadHrefLangs from index for access to ast…
Browse files Browse the repository at this point in the history
…ro-i18next runtime config

expose AstroI18next global variable to access runtime config

fixes #65
  • Loading branch information
yassinedoghri committed Nov 27, 2022
1 parent 4f5ef44 commit 5e3b96c
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 4,427 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ describe("localizeUrl(...)", () => {
"https://example.com/base/fr/about/"
);
});

it("generates the correct locale given a locale", () => {
expect(localizeUrl("https://example.com/fr/a-propos", "fr")).toBe(
"https://example.com/fr/a-propos/"
);
expect(localizeUrl("https://example.com/fr/a-propos", "en")).toBe(
"https://example.com/about/"
);
});
});

describe("detectLocaleFromPath(...)", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/HeadHrefLangs.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import i18next from "i18next";
import { localizeUrl } from "../utils";
import { localizeUrl } from "../..";
const supportedLanguages = i18next.languages;
const currentUrl = Astro.url.href;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Trans.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { interpolate, createReferenceStringFromHTML } from "../utils";
import { interpolate, createReferenceStringFromHTML } from "../..";
export interface Props {
i18nKey?: string;
Expand Down
30 changes: 15 additions & 15 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { AstroI18nextConfig, Routes } from "./types";
import { AstroI18nextConfig, AstroI18nextGlobal, Routes } from "./types";

var astroI18nextConfig: AstroI18nextConfig = {
defaultLocale: "cimode",
locales: [],
namespaces: "translation",
defaultNamespace: "translation",
load: ["server"],
routes: {},
flatRoutes: {},
showDefaultLocale: false,
export const AstroI18next: AstroI18nextGlobal = {
config: {
defaultLocale: "cimode",
locales: [],
namespaces: "translation",
defaultNamespace: "translation",
load: ["server"],
routes: {},
flatRoutes: {},
showDefaultLocale: false,
},
};

export const getAstroI18nextConfig = () => astroI18nextConfig;

/* c8 ignore start */
export const setAstroI18nextConfig = (config: AstroI18nextConfig) => {
let flatRoutes = {};
Expand All @@ -21,17 +21,17 @@ export const setAstroI18nextConfig = (config: AstroI18nextConfig) => {
flatRoutes = flattenRoutes(config[key]);
}

astroI18nextConfig[key] = config[key];
AstroI18next.config[key] = config[key];
}

// @ts-ignore
astroI18nextConfig["flatRoutes"] = flatRoutes;
AstroI18next.config.flatRoutes = flatRoutes;
};

export const astroI18nextConfigBuilder = (
config: AstroI18nextConfig
): AstroI18nextConfig => {
return { ...astroI18nextConfig, ...config };
return { ...AstroI18next.config, ...config };
};
/* c8 ignore stop */

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (options?: AstroI18nextOptions): AstroIntegration => {
astroI18nextConfig.defaultLocale === ""
) {
throw new Error(
"[astro-i18next]: you must set a `defaultLocale` in your astroI18nextConfig!"
"[astro-i18next]: you must set a `defaultLocale` in your astro-i18next config!"
);
}

Expand Down Expand Up @@ -167,11 +167,14 @@ export function initAstroI18next(config: AstroI18nextConfig) {
setAstroI18nextConfig(config);
}

export { AstroI18next } from "./config";

export {
createReferenceStringFromHTML,
detectLocaleFromPath,
interpolate,
localizePath,
localizeUrl,
detectLocaleFromPath,
} from "./utils";

export { AstroI18nextConfig, AstroI18nextOptions } from "./types";
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { InitOptions } from "i18next";

export interface AstroI18nextGlobal {
config: AstroI18nextConfig;
}

export interface AstroI18nextOptions {
/**
* Path to your astro-i18next config file
Expand Down
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileURLToPath } from "url";
import load from "@proload/core";
import { AstroI18nextConfig } from "./types";
import typescript from "@proload/plugin-tsm";
import { getAstroI18nextConfig } from "./config";
import { AstroI18next } from "./config";

/**
* Adapted from astro's tailwind integration:
Expand Down Expand Up @@ -204,8 +204,7 @@ export const localizePath = (
base = base.replace(/^\/+|\/+$/g, "");
base = base === "" ? "/" : "/" + base + "/";

const { flatRoutes, showDefaultLocale, defaultLocale } =
getAstroI18nextConfig();
const { flatRoutes, showDefaultLocale, defaultLocale } = AstroI18next.config;

// remove base path if found
path = path.startsWith(base) ? path.slice(base.length) : path.slice(1);
Expand Down Expand Up @@ -289,7 +288,7 @@ export const detectLocaleFromPath = (path: string) => {
// remove all leading slashes
path = path.replace(/^\/+/g, "");

const { defaultLocale, locales } = getAstroI18nextConfig();
const { defaultLocale, locales } = AstroI18next.config;

const pathSegments = path.split("/");

Expand Down
Loading

0 comments on commit 5e3b96c

Please sign in to comment.