npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

laravel-translator

v1.1.1

Published

Laravel localization bridge for your frontend.

Downloads

291

Readme

Laravel Translator for Frontend

Laravel Translator is a package that allows you to use Laravel's localization features in your frontend code, with the same syntax you would use in your backend code and zero configuration.

This package was inspired by laravel-vue-i18n and lingua.

🧩 Features

  • Frontend framework-agnostic, works with any framework or even plain javascript (and even without Laravel)
  • Use the same translation files you use in your backend code (both php and json files are supported)
  • No extra configuration required: install, register and use
  • Zero SSR configuration required
  • No export step required, translations are parsed and bundled directly from your backend code by Vite
  • Support for hot reloading
  • Minimal and lightweight

🚀 Installation

ViteJS is required to use this package. Install the package via npm or yarn:

npm install -D laravel-translator

In your vite.config.js file, register the plugin:

import {defineConfig} from 'vite'
import laravelTranslator from 'laravel-translator/vite'

export default defineConfig({
    plugins: [
        // ...
        laravelTranslator()
    ]
})

Run npm run dev to start the development server, or npm run build to build your assets for production.

Remember to set the language in your html, for example in your app.blade.php file:


<html lang="{{ app()->getLocale() }}">

If you want to also pass the fallback locale to your frontend code, you can do so by adding the following line to your app.blade.php file:


<script>
    window.fallbackLocale = "{{ config('app.fallback_locale') }}"
</script>

🧑‍💻Usage

You can import the usual Laravel translation functions from the laravel-translator package:

import {__, trans, t, trans_choice} from 'laravel-translator'

__('user.welcome', {name: 'John'}) // Welcome, John!
trans('auth.failed') // These credentials do not match our records.
t('auth.failed') // ...

trans_choice('user.count', 1) // User
trans_choice('user.count', 2) // Users

Svelte


<script>
    import {__} from "laravel-translator"
</script>

<h1>{__('page.title')}</h1>

<p>{__('page.content')}</p>

Vue

Register the plugin:

...
.use(LaravelTranslatorVue, {
    locale: 'it'
    // fallbackLocale: 'en', optional
})
...

Use the functions function in your components:

<template>
    <div>
        <h1>{{ __('page.title') }}</h1>

        <p>{{ __('page.content') }}</p>
        <p>{{ t('page.content') }}</p>
        <p>{{ trans('page.content') }}</p>
        <p>{{ trans_choice('page.content') }}</p>
    </div>
</template>

Advanced usage

It's possible to set the locale and the fallback locale manually, by using the setLocale function:

import {setLocale} from "laravel-translator"

setLocale('it') // Set the locale to 'it'
setLocale('it', 'en') // Set the locale to 'it' and the fallback locale to 'en'

You can add additional path where to look for translation files on the Vite plugin options:

import {defineConfig} from 'vite'
import laravelTranslator from 'laravel-translator/vite'

export default defineConfig({
    plugins: [
        // ...
        laravelTranslator({
            langPath: 'resources/js/translations', // By default, the package looks for translations in the 'lang' folder
            additionalLangPaths: ['vendor/my-package/lang'] // You can add additional paths where to look for translations
        })
    ]
})

⚙️ How it works

This package uses Vite Virtual Modules feature to parse your translations files and make them available in your frontend code, without the need to export them to a separate file.

In development mode, the translations are parsed and bundled on the fly, and hot reloaded when the files change.

In production mode, the translations are parsed and bundled automatically when you run npm run build.

⚖️ License

The MIT License (MIT). Please see License File for more information.

🏅 Credits