@tnet-i18n/i18n
v1.332.0
Published
> Shared **i18n package** cho hệ thống web & mobile apps của Trinity Net Technology. > Dùng để đồng bộ key/value translations (multi-language) và cung cấp API/React hook để thao tác thuận tiện.
Downloads
12,713
Readme
@tnet-i18n/i18n
Shared i18n package cho hệ thống web & mobile apps của Trinity Net Technology.
Dùng để đồng bộ key/value translations (multi-language) và cung cấp API/React hook để thao tác thuận tiện.
✨ Tính năng
- 📦 Centralized translations: dùng chung
locales/*.jsoncho cả web (React) và app (React Native). - 🔑 Type-safe keys: auto-generate type từ
en.json→ autocomplete khi gọit(). - ⚡ React hooks: có sẵn
useT()để dễ dùng trong component. - 🔄 Hot language switch: hỗ trợ
i18next.changeLanguage()runtime. - 🚀 CI/CD ready: publish tự động qua GitHub Actions khi release.
📥 Cài đặt
Package được publish public trên npm:
yarn add @tnet-i18n/i18n
# hoặc npm
npm install @tnet-i18n/i18n🛠 Cách dùng
1. Khởi tạo i18n
Trong entry file của app (ví dụ main.tsx với React, App.tsx với React Native):
import { initI18n } from "@tnet-i18n/i18n";
// Khởi tạo với ngôn ngữ mặc định
initI18n({ lng: "en" });2. Dùng hàm t()
import { t } from "@tnet-i18n/i18n";
function Example() {
return <h1>{t("button.viewQuotes")}</h1>;
}3. Dùng hook useT()
import { useT } from "@tnet-i18n/i18n";
export function Example() {
const { t, i18n, lng } = useT();
return (
<>
<p>Current language: {lng}</p>
<p>{t("common.loading")}</p>
<button onClick={() => i18n.changeLanguage(lng === "en" ? "cn" : "en")}>
Switch Language
</button>
</>
);
}🌍 Cấu trúc locales/
locales/
├─ en.json # English
└─ cn.json # ChineseVí dụ en.json:
{
"button": {
"login": "Login",
"logout": "Logout",
"viewQuotes": "View Quotes"
},
"common": {
"loading": "Loading..."
}
}🔑 Type-safe Keys
Sau mỗi lần build, file dist/keys.d.ts sẽ được sinh tự động từ en.json.
import { t } from "@tnet-i18n/i18n";
import type { TranslationKey } from "@tnet-i18n/i18n/dist/keys";
function safeT<K extends TranslationKey>(key: K) {
return t(key);
}
safeT("button.login"); // ✅
safeT("button.notExists"); // ❌ lỗi compile🚀 Release quy trình
1. Tự động (qua GitHub Actions)
- Bump version trong
package.json(ví dụ"1.0.1"). - Commit & tạo tag theo format:
git commit -am "chore(release): v1.0.1" git tag i18n-v1.0.1 git push origin main --tags - GitHub Actions sẽ tự build & publish lên npm.
2. Thủ công (Run workflow)
- Push code với version mới trong
package.json. - Vào tab Actions → Publish to npm → Run workflow.
- CI sẽ build & publish version mới.
📦 Scripts có sẵn
yarn build– build TypeScript →dist/yarn check:placeholders– check placeholders giữa các file localeyarn gen:keys– generate type keys từen.jsonyarn prepublishOnly– chạy full build + check + gen trước khi publish
📜 License
MIT © Trinity Net Technology
