Skip to content
i18n / 0

Installation & Setup

Last updated:

On this page

Installation

pnpm add @javaabu/i18n

Tell it where the translations are, then use the helpers anywhere:

import { __, trans_choice, configure } from "@javaabu/i18n"

configure({ loader: (locale) => import(`../../lang/${locale}.json`) })

__("Welcome, :name!", { name: "Ali" })
trans_choice("messages.apples", 3)

The locale comes from the page, so a Laravel layout's <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> is all it needs. See Locale and direction.

If the page already includes a Laravel JS Localization script, you can leave out configure(): window.Lang is picked up automatically. See Loading translations.

Next, see how to use it with React, Vue, jQuery or vanilla JS.

Without a bundler

The dist folder has a ready-made build. It puts __, trans and trans_choice on window, the rest of the API on window.I18n, and the helpers on $ if jQuery has already loaded.

<script src="https://unpkg.com/@javaabu/i18n/dist/i18n.iife.js"></script>
<script>
  I18n.load({ dv: { Message: "މެސެޖު" } })

  console.log(__("Message"))
</script>

Examples

The examples folder has a small Dhivehi and English page for each integration. Run them with pnpm install && pnpm examples.

examples/script-tag.html needs no build step or server: run pnpm build, then open the file.

Documentation
All packages

javaabu/i18n

Source on GitHub