Skip to content

Commit

Permalink
Initial module with tests and basic docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m10rten committed Aug 16, 2024
1 parent 754cf20 commit ec0194b
Show file tree
Hide file tree
Showing 23 changed files with 6,109 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/honest-lamps-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"i4n": minor
---

Initial functionality release with `t` & `switch` function, including docs and tests.
47 changes: 47 additions & 0 deletions .github/workflows/main_pr_packages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: PR Workflow
on:
pull_request:
branches:
- main
- dev
paths:
- ".github/workflows/main_pr_packages.yaml"

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
ci:
runs-on: ubuntu-latest
# needs: build
strategy:
fail-fast: true
matrix:
node-version: [20.x, 22.x]
steps:
- uses: actions/[email protected]
- uses: pnpm/[email protected]
with:
version: 8
- uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
cache: pnpm
# install
- name: Install dependencies 📦
run: pnpm i --frozen-lockfile
# generate
- name: Generate deps 🍪
run: pnpm run generate
env:
CI: true
# lint
- name: Typecheck 🧷
run: pnpm run typecheck
- name: Lint 🛫
run: pnpm run lint
# build
- name: Build 🏗
run: pnpm run build
# test
- name: Test 🧪
run: pnpm run test
59 changes: 59 additions & 0 deletions .github/workflows/main_push_packages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Push Workflow
on:
push:
branches:
- main
paths:
- "./src/**"
- ".github/workflows/main_push_packages.yaml"
- ".changeset/**"
- pnpm-lock.yaml
- package.json

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
node-version: [20.x, 22.x]
steps:
- uses: actions/[email protected]
- uses: pnpm/[email protected]
with:
version: 8
- uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
cache: pnpm
# install
- run: pnpm install
name: Install dependencies

- name: Generate deps 🍪
run: pnpm run generate
env:
CI: true

- name: Verify Packages 🧷📦,⚡
run: pnpm run precheck

- name: Check packages and create build 🧪,🔨,
run: pnpm run verify
env:
CI: true

# publish
- name: Create release PR and publish packages
if: matrix.node-version == '22.x'
id: changesets
uses: changesets/[email protected]
with:
commit: "chore(release): 📦 version packages"
title: "chore(release): 📦 version packages"
publish: pnpm exec changeset publish
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!dist/*
!package.json
!README.md
!LICENSE
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
auto-install-peers = true
save-exact = true

3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
LICENSE
27 changes: 27 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"tabWidth": 2,
"useTabs": false,
"bracketSameLine": true,
"printWidth": 119,
"singleQuote": false,
"trailingComma": "all",
"semi": true,
"arrowParens": "always",
"bracketSpacing": true,
"jsxSingleQuote": false,
"quoteProps": "as-needed",
"endOfLine": "lf",
"importOrder": [
"^(react/(.*)$)|^(react$)",
"^(next/(.*)$)|^(next$)",
"<THIRD_PARTY_MODULES>",
"",
"^types$",
"^@/(.*)$",
"#/(.*)$",
"~/(.*)$",
"",
"^[./]"
],
"plugins": ["@ianvs/prettier-plugin-sort-imports"]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Maarten
Copyright (c) 2024 m10rten

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
132 changes: 131 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,132 @@
# i4n
🌐🪶 Lightweight, Zero dependencies, Typesafe and Simple to use translations for TypeScript.

🌐🪶 Lightweight, Zero dependencies, Typesafe and Simple to use translations for TypeScript.

## Installation

To use this package, install with npm:

```bash
npm install i4n
```

## Usage

To get started with translating, import the I4n class:

```ts
import { I4n } from "i4n";
```

Then create a set of translations:

```ts
const translations = {
en: {
hi: "Hello",
earth: "World",
what: {
happened: "What happened?",
},
},
es: {
hi: "Ola",
earth: "Mundo",
what: {
happened: "¿Qué pasó?",
},
},
} as const;
```

> This package will not validate this structure or types inbetween translations(!), but having an object with languages is required for this module to work.
This will **not** work:

```ts
// BAD:
const translations = {
hi: "Hello",
earth: "World",
what: {
happened: "What happened?",
},
} as const;
```

> ⚠️ Warning
Watch out when making bulk translations, in that case be sure to type it yourself as this may cause the `i4n` package to not work as intended or expected.

For example:

```ts
const translations = {
en: {
earth: "World",
},
es: {
aerth: "Mundo",
},
} as const;
```

This does not raise typescript errors because it is a valid object.

To overcome this, implement your own types:

```ts
type TranslationData = {
earth: string;
};
type TranslationSet = Record<string, TranslationData>;

const translations: TranslationSet = {
en: {
earth: "world",
},
es: {
aerth: "Mundo",
// ^ Typescript error
},
};
```

## Init

Then initialize the class with your translations and default language:

```ts
const i4n = new I4n({ translations, language: "en" });
```

- **translations**: the translations you just created having `hi`, `earth`, `what.happened` in it.
- **language**: the default language the translation function will start with.

## Functions

To use it there are 2 functions: `t` and `switch`.

### `t`

The `t` function allows you to call your translations with a `dot.notation` so you may get nested fields.

```ts
console.log(i4n.t("hi")); // "Hello"
```

Calling the `t` method for just an object (not the nested key), will result in the object being returned.

```ts
console.log(i4n.t("what")); // { happened: "What happened?" }
```

### `switch`

The `switch` method enables the application to switch languages at runtime, making it very useful for web translations.

```ts
i4n.switch("es");

console.log(i4n.t("hi")); // "Ola"
```
12 changes: 12 additions & 0 deletions dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
versioning-strategy: increase
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended);
11 changes: 11 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from "jest";

const config: Config = {
verbose: true,
preset: "ts-jest",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
};

export default config;
Loading

0 comments on commit ec0194b

Please sign in to comment.