Home / Function/ computeCurrentLocale() — astro Function Reference

computeCurrentLocale() — astro Function Reference

Architecture documentation for the computeCurrentLocale() function in render-context.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  a1e64bf7_a3cd_6346_6068_fe2858acbe2e["computeCurrentLocale()"]
  b7425a46_8c1f_1122_7ae3_a00e52db1d90["RenderContext"]
  a1e64bf7_a3cd_6346_6068_fe2858acbe2e -->|defined in| b7425a46_8c1f_1122_7ae3_a00e52db1d90
  d91ba7de_e6ec_e3f2_d698_49f283edc9ab["createActionAPIContext()"]
  d91ba7de_e6ec_e3f2_d698_49f283edc9ab -->|calls| a1e64bf7_a3cd_6346_6068_fe2858acbe2e
  0007f436_547e_7fd2_f341_1a05d84a40a6["createAstroPagePartial()"]
  0007f436_547e_7fd2_f341_1a05d84a40a6 -->|calls| a1e64bf7_a3cd_6346_6068_fe2858acbe2e
  style a1e64bf7_a3cd_6346_6068_fe2858acbe2e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/render-context.ts lines 803–849

	computeCurrentLocale() {
		const {
			url,
			pipeline: { i18n },
			routeData,
		} = this;
		if (!i18n) return;

		const { defaultLocale, locales, strategy } = i18n;

		const fallbackTo =
			strategy === 'pathname-prefix-other-locales' || strategy === 'domains-prefix-other-locales'
				? defaultLocale
				: undefined;

		if (this.#currentLocale) {
			return this.#currentLocale;
		}

		let computedLocale;
		if (isRouteServerIsland(routeData)) {
			let referer = this.request.headers.get('referer');
			if (referer) {
				if (URL.canParse(referer)) {
					referer = new URL(referer).pathname;
				}
				computedLocale = computeCurrentLocale(referer, locales, defaultLocale);
			}
		} else {
			// For SSG we match the route naively, for dev we handle fallback on 404, for SSR we find route from fallbackRoutes
			let pathname = routeData.pathname;
			if (!routeData.pattern.test(url.pathname)) {
				for (const fallbackRoute of routeData.fallbackRoutes) {
					if (fallbackRoute.pattern.test(url.pathname)) {
						pathname = fallbackRoute.pathname;
						break;
					}
				}
			}
			pathname = pathname && !isRoute404or500(routeData) ? pathname : url.pathname;
			computedLocale = computeCurrentLocale(pathname, locales, defaultLocale);
		}

		this.#currentLocale = computedLocale ?? fallbackTo;

		return this.#currentLocale;
	}

Domain

Subdomains

Frequently Asked Questions

What does computeCurrentLocale() do?
computeCurrentLocale() is a function in the astro codebase, defined in packages/astro/src/core/render-context.ts.
Where is computeCurrentLocale() defined?
computeCurrentLocale() is defined in packages/astro/src/core/render-context.ts at line 803.
What calls computeCurrentLocale()?
computeCurrentLocale() is called by 2 function(s): createActionAPIContext, createAstroPagePartial.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free