aboutsummaryrefslogtreecommitdiffstats
path: root/dist/utils.js
blob: d89663e82ae6b6f18667958b28f62a965e5f72c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Replaces the locale slug in a URL.
//
// If the `full` argument is set to `true`, the full URL is returned as a string.
// e.g. https://mywebsite.com/en/blog/article-1 => https://mywebsite.com/de/blog/article-1
//
// Otherwise (default) the URL relative to the base is returned.
// e.g. https://mywebsite.com/en/blog/article-1 => /de/blog/article-1
export const replaceLocaleInUrl = (url, locale, full = false) => {
    const [, , ...rest] = url.pathname.split('/');
    const new_pathname = `/${[locale, ...rest].join('/')}`;
    if (!full) {
        return `${new_pathname}${url.search}`;
    }
    const newUrl = new URL(url.toString());
    newUrl.pathname = new_pathname;
    return newUrl.toString();
};
export function fromLocalizedString(localizedString, locale) {
    if (typeof localizedString === "string")
        return localizedString;
    // @ts-ignore
    if (localizedString[locale])
        return localizedString[locale];
    // @ts-ignore
    if (localizedString["nb"])
        return localizedString["nb"];
    // @ts-ignore
    if (localizedString["en"])
        return localizedString["en"];
}