Skip to content

Commit

Permalink
feat: add astro integration --> initialize i18next upon astro:config:…
Browse files Browse the repository at this point in the history
…setup

- update src structure to split integration from components
- update package.json to export subpath
for components
- bundle project using esbuild
- add typescript type declarations to package
- add
baseLanguage property to LanguageSelector + update readme docs
  • Loading branch information
yassinedoghri committed Apr 30, 2022
1 parent 8882712 commit 78ec744
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ import { LanguageSelector } from "astro-i18next";

#### LanguageSelector Props

| Propname | Type | Description |
| --------- | ------ | -------------------------------------- |
| className | string | class attribute for the `<select>` tag |
| Propname | Type | Description |
| ------------ | ------ | ----------------------------------------------------------------------------------------------------- |
| baseLanguage | string | language code that translations are based off of (will redirect to `/` instead of `/[language-code]`) |
| className | string | class attribute for the `<select>` tag to customize it |

## License

Expand Down
23 changes: 23 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

require("esbuild")
.build({
bundle: true,
entryPoints: ["src/index.mts", "src/utils.ts", "src/components/index.mts"],
outdir: "dist",
outExtension: {
".js": ".mjs",
},
external: ["i18next", "country-code-to-flag-emoji"],
minify: false,
format: "esm",
platform: "node",
target: "node14",
sourcemap: "inline",
sourcesContent: false,
loader: {
".astro": "file",
},
assetNames: "[dir]/[name]",
})
.catch(() => process.exit(1));
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"test": "jest",
"dev": "astro dev",
"preview": "astro preview",
"build": "./build.js && npm run typecheck:emit",
"lint": "eslint --ext js,ts,mts src",
"prettier": "prettier --check --ignore-path .gitignore .",
"prettier:fix": "prettier --write --ignore-path .gitignore .",
"typecheck": "tsc --noEmit",
"typecheck:emit": "tsc --declaration --emitDeclarationOnly --outDir dist",
"commit": "cz",
"prepare": "husky install",
"semantic-release": "semantic-release"
Expand All @@ -19,12 +21,16 @@
"url": "https://github.com/yassinedoghri/astro-i18next.git"
},
"files": [
"src/index.mts",
"src/Trans.astro",
"src/LanguageSelector.astro",
"src/utils.ts"
"src",
"!src/index.d.ts",
"!src/**/*.test.ts",
"dist"
],
"exports": "./src/index.mts",
"main": "./dist/index.mjs",
"exports": {
".": "./dist/index.mjs",
"./components": "./dist/components/index.d.mts"
},
"keywords": [
"astro",
"i18next",
Expand Down Expand Up @@ -61,6 +67,7 @@
"@typescript-eslint/parser": "^5.21.0",
"astro": "^1.0.0-beta.19",
"cz-conventional-changelog": "^3.3.0",
"esbuild": "^0.14.38",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import ISO6391 from "iso-639-1";
import countryCodeToFlagEmoji from "country-code-to-flag-emoji";
export interface Props {
baseLanguage: string;
className?: string;
}
const supportedLanguages = i18next.languages;
const currentLanguage = i18next.language;
const baseLanguage = "en";
const { className } = Astro.props;
const { baseLanguage, className } = Astro.props;
---

<select onchange="location = this.value;" class={className}>
Expand Down
2 changes: 1 addition & 1 deletion src/Trans.astro → src/components/Trans.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import { t } from "i18next";
import { interpolate } from "./utils";
import { interpolate } from "../utils";
export interface Props {
i18nKey: string;
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Trans } from "./Trans.astro";
export { default as LanguageSelector } from "./LanguageSelector.astro";
22 changes: 20 additions & 2 deletions src/index.mts
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
export { default as Trans } from "./Trans.astro";
export { default as LanguageSelector } from "./LanguageSelector.astro";
import { AstroIntegration } from "astro";
import { InitOptions } from "i18next";

export default (options: InitOptions): AstroIntegration => {
return {
name: "astro-i18next",
hooks: {
"astro:config:setup": async ({ config, injectScript }) => {
// TODO: include other options? Abstract translation files loading?

// init i18next
injectScript(
"page-ssr",
`import i18next from "i18next";
i18next.init(${JSON.stringify(options)});`
);
},
},
};
};
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const interpolate = (
tagsRegex.exec("");
});

if (referenceTags === []) {
if (referenceTags.length === 0) {
console.warn(
"WARNING(astro-i18next): The default slot does not include any html tag to interpolate!"
);
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"skipLibCheck": true,
"moduleResolution": "node",
"baseUrl": "src",
"lib": ["ES2021"]
},
"include": ["src/**/*.ts", "src/**/*.mts", "src/**/*.astro", "src/index.mjs"],
"exclude": []
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit 78ec744

Please sign in to comment.