Skip to content

lib/i18n

type UiComponentKeys = keyof UiTranslations["en-us"]["translations"];

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:42

Represents the keys of the UI component translations for the ‘en-us’ locale. This type is derived from the ‘translations’ property of the ‘UiTranslations’ interface for the ‘en-us’ locale (Source of truth), ensuring that only valid translation keys are used.


type UiLanguageKeys = keyof UiTranslations;

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:35

Represents the keys of the UiTranslations type. This type is used to define the possible keys for UI language translations.


type UiTranslations = typeof uiTranslations;

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:29

Represents the type of UI translations. This type is derived from the structure of the uiTranslations object.

const languageSelectorOptions: {
key: "en-us";
value: string;
}[];

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:103

Generates an array of language selector options from the uiTranslations object. Each option contains a key and a value where:

  • key is a language key from UiLanguageKeys.
  • value is the display name of the language.
key: "en-us";
value: string;

An array of objects representing language selector options.

function getCurrentURLPath(Astro: AstroGlobal): string

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:135

Retrieves the current URL path, adjusting for language settings.

This function checks if the URL path includes ’/_server-islands’. If it does, it extracts the referer URL from the request headers and determines the current language from that URL. If the current language is the default language, it returns the pathname as is. Otherwise, it replaces the language segment in the pathname with ’/’.

If the URL path does not include ’/_server-islands’, it uses the Astro URL directly to determine the current language and adjust the pathname accordingly.

AstroGlobal

The global Astro object containing URL and request information.

string


function getLangFromUrl(url: URL): "en-us"

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:116

Extracts the language key from the given URL’s pathname.

URL

The URL object from which to extract the language key.

"en-us"

The language key if it exists in the uiTranslations, otherwise returns the default language key.


function staticPaths(): {
params: {
locale: undefined | string;
};
}[]

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:177

Example of how to use this i18n utils on a Static page

{ params: { locale: undefined | string; }; }[]

An array of paths for all languages

export async function getStaticPaths() {
const paths = staticPaths();
return paths;
}

If the default language is hidden, the paths for the default language will be generated without the language prefix while all extra languages will have the prefix. (e.g. When showDefaultLang is false: /en/page will be /page and spanish will be /es/page)


function switchLanguage(Astro: AstroGlobal): (languageKey: "en-us") => string

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:155

Function to switch the language of the current page.

AstroGlobal

The global Astro object.

Function

"en-us"

string


function useTranslatedPath(lang: "en-us"): (path: string, l?: string) => string

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:89

Returns a function that translates a given path based on the provided language.

"en-us"

The language key to be used for translation.

Function

A function that takes a path and an optional language key, and returns the translated path. If the language key is not provided, the default language key is used. If the language is the default language and showDefaultLang is false, the original path is returned. Otherwise, the path is prefixed with the language key.

string

string

string


function useTranslations<L, T>(lang: L, component: T): (key: keyof UiTranslationComponent<L, T>) => UiTranslationComponent<L, T>[keyof UiTranslationComponent<L, T>]

Defined in: studiocms/packages/studiocms/src/lib/i18n/index.ts:62

Retrieves a translation function for a given language and component.

L extends "en-us"

T extends | "@studiocms/auth:login" | "@studiocms/auth:signup" | "@studiocms/auth:logout" | "@studiocms/auth:oauth-stack" | "@studiocms/dashboard:index" | "@studiocms/dashboard:sidebar" | "@studiocms/dashboard:profile" | "@studiocms/dashboard:password-reset" | "@studiocms/dashboard:configuration" | "@studiocms/dashboard:user-mngmt-sidebar" | "@studiocms/dashboard:user-mngmt-index" | "@studiocms/dashboard:user-mngmt-edit" | "@studiocms/dashboard:plugin-settings" | "@studiocms/dashboard:content-sidebar" | "@studiocms/dashboard:content-header" | "@studiocms/dashboard:content-index" | "@studiocms/dashboard:content-diff" | "@studiocms/dashboard:content-folder" | "@studiocms/dashboard:content-page" | "@studiocms/dashboard:404"

L

The language key to use for translations.

T

The component key to use for translations.

Function

A function that takes a translation key and returns the corresponding translated string.

keyof UiTranslationComponent<L, T>

UiTranslationComponent<L, T>[keyof UiTranslationComponent<L, T>]