Skip to content

Commit

Permalink
Merge branch 'main' into refactor/content-collections
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Nov 9, 2023
2 parents bf46f4c + 5b16619 commit 3d61b49
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .changeset/rude-lizards-scream.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Organize your content folders by locale depending on your `i18n.routingStrategy`
│ │ │ ├── index.astro
```

Compute relative URLs for your links with `getLocaleRelativeURL` from the new `astro:i18n` module:
Compute relative URLs for your links with `getRelativeLocaleUrl` from the new `astro:i18n` module:

```astro
---
import {getLocaleRelativeUrl} from "astro:i18n";
const aboutUrl = getLocaleRelativeUrl("pt-br", "about");
import {getRelativeLocaleUrl} from "astro:i18n";
const aboutUrl = getRelativeLocaleUrl("pt-br", "about");
---
<p>Learn more <a href={aboutURL}>About</a> this site!</p>
```
Expand Down
32 changes: 16 additions & 16 deletions packages/astro/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ declare module 'astro:i18n' {
* ## Examples
*
* ```js
* import { getLocaleRelativeUrl } from "astro:i18n";
* getLocaleRelativeUrl("es"); // /es
* getLocaleRelativeUrl("es", "getting-started"); // /es/getting-started
* getLocaleRelativeUrl("es_US", "getting-started", { prependWith: "blog" }); // /blog/es-us/getting-started
* getLocaleRelativeUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // /blog/es_US/getting-started
* import { getRelativeLocaleUrl } from "astro:i18n";
* getRelativeLocaleUrl("es"); // /es
* getRelativeLocaleUrl("es", "getting-started"); // /es/getting-started
* getRelativeLocaleUrl("es_US", "getting-started", { prependWith: "blog" }); // /blog/es-us/getting-started
* getRelativeLocaleUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // /blog/es_US/getting-started
* ```
*/
export const getLocaleRelativeUrl: (
export const getRelativeLocaleUrl: (
locale: string,
path?: string,
options?: GetLocaleOptions
Expand All @@ -176,14 +176,14 @@ declare module 'astro:i18n' {
* If `site` is `https://example.com`:
*
* ```js
* import { getLocaleAbsoluteUrl } from "astro:i18n";
* getLocaleAbsoluteUrl("es"); // https://example.com/es
* getLocaleAbsoluteUrl("es", "getting-started"); // https://example.com/es/getting-started
* getLocaleAbsoluteUrl("es_US", "getting-started", { prependWith: "blog" }); // https://example.com/blog/es-us/getting-started
* getLocaleAbsoluteUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // https://example.com/blog/es_US/getting-started
* import { getAbsoluteLocaleUrl } from "astro:i18n";
* getAbsoluteLocaleUrl("es"); // https://example.com/es
* getAbsoluteLocaleUrl("es", "getting-started"); // https://example.com/es/getting-started
* getAbsoluteLocaleUrl("es_US", "getting-started", { prependWith: "blog" }); // https://example.com/blog/es-us/getting-started
* getAbsoluteLocaleUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // https://example.com/blog/es_US/getting-started
* ```
*/
export const getLocaleAbsoluteUrl: (
export const getAbsoluteLocaleUrl: (
locale: string,
path?: string,
options?: GetLocaleOptions
Expand All @@ -194,17 +194,17 @@ declare module 'astro:i18n' {
* @param {import('./dist/i18n/index.js').GetLocaleOptions} options Customise the generated path
* @return {string[]}
*
* Works like `getLocaleRelativeUrl` but it emits the relative URLs for ALL locales:
* Works like `getRelativeLocaleUrl` but it emits the relative URLs for ALL locales:
*/
export const getLocaleRelativeUrlList: (path?: string, options?: GetLocaleOptions) => string[];
export const getRelativeLocaleUrlList: (path?: string, options?: GetLocaleOptions) => string[];
/**
* @param {string} [path=""] An optional path to add after the `locale`.
* @param {import('./dist/i18n/index.js').GetLocaleOptions} options Customise the generated path
* @return {string[]}
*
* Works like `getLocaleAbsoluteUrl` but it emits the absolute URLs for ALL locales:
* Works like `getAbsoluteLocaleUrl` but it emits the absolute URLs for ALL locales:
*/
export const getLocaleAbsoluteUrlList: (path?: string, options?: GetLocaleOptions) => string[];
export const getAbsoluteLocaleUrlList: (path?: string, options?: GetLocaleOptions) => string[];
}

declare module 'astro:middleware' {
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function astroInternationalization({
const site = ${JSON.stringify(settings.config.site)};
const i18n = ${JSON.stringify(settings.config.experimental.i18n)};
export const getLocaleRelativeUrl = (locale, path = "", opts) => _getLocaleRelativeUrl({
export const getRelativeLocaleUrl = (locale, path = "", opts) => _getLocaleRelativeUrl({
locale,
path,
base,
Expand All @@ -45,7 +45,7 @@ export default function astroInternationalization({
...i18n,
...opts
});
export const getLocaleAbsoluteUrl = (locale, path = "", opts) => _getLocaleAbsoluteUrl({
export const getAbsoluteLocaleUrl = (locale, path = "", opts) => _getLocaleAbsoluteUrl({
locale,
path,
base,
Expand All @@ -56,9 +56,9 @@ export default function astroInternationalization({
...opts
});
export const getLocaleRelativeUrlList = (path = "", opts) => _getLocaleRelativeUrlList({
export const getRelativeLocaleUrlList = (path = "", opts) => _getLocaleRelativeUrlList({
base, path, trailingSlash, format, ...i18n, ...opts });
export const getLocaleAbsoluteUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, trailingSlash, format, site, ...i18n, ...opts });
export const getAbsoluteLocaleUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, trailingSlash, format, site, ...i18n, ...opts });
`;
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import { getRelativeLocaleUrl } from "astro:i18n";
let about = getRelativeLocaleUrl("pt", "about");
---

<html>
<head>
<title>Astro</title>
</head>
<body>
Virtual module doesn't break

About: {about}
</body>
</html>
25 changes: 25 additions & 0 deletions packages/astro/test/i18-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ import { expect } from 'chai';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';

describe('astro:i18n virtual module', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-routing/',
});
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('correctly imports the functions', async () => {
const response = await fixture.fetch('/virtual-module');
expect(response.status).to.equal(200);
const text = await response.text();
expect(text).includes("Virtual module doesn't break");
expect(text).includes('About: /pt/about');
});
});
describe('[DEV] i18n routing', () => {
describe('i18n routing', () => {
/** @type {import('./test-utils').Fixture} */
Expand Down

0 comments on commit 3d61b49

Please sign in to comment.