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-inertia-vue-translator

v0.1.3

Published

Yet another npm package, which allows you to add translation capabilities to your inertia + vue3 components, while integrating with a Laravel backend. It pairs best with the composer package: antonioprimera/laravel-js-localization

Downloads

6

Readme

Laravel Inertia Vue3 Translator

The laravel-inertia-vue-translator package was primarily built, to be paired with the antonioprimera/laravel-js-localization package, to provide a seamless localization experience for Laravel Inertia Vue3 apps.

The Laravel package provides the necessary backend functionality to expose the Laravel localization files to the frontend, this package provides the necessary frontend functionality to access the localized strings in your Vue3 components.

While the two packages were built to work together, you can also use this package as a standalone package in your Inertia + Vue3 apps, without the Laravel package, by using the exposed Translator class directly and providing the dictionary of translations manually (either from a JS Object or from an API).

Installation

You can install the package via npm:

npm install laravel-inertia-vue-translator

If you are using the antonioprimera/laravel-js-localization package, this step is included in the php artisan js-localization:install command, so you don't need to install it manually.

Usage

Setting up the vue plugin

When creating your Vue3 app, you should import the plugin directly from the package and use it in your app:

import { createApp } from 'vue'
import {translatorPlugin} from 'laravel-inertia-vue-translator'

// Add the plugin to your Vue3 app
return createApp({ render: () => h(App, props) })
    //add the plugin to your Vue3 app
    .use(translatorPlugin)
    .mount('#app')

Using the txt and txts helper functions

In order to use txt and txts helper functions in your Vue3 script sections, you can easily inject them into your components, by using the useTranslator function, which basically injects the functions into your component's context.

//in any of your Vue3 components
<script setup>
    import {useTranslator} from "laravel-inertia-vue-translator";
    const {txt, txts} = useTranslator();

    //now you can use the txt and txts functions in your component
    const greeting = txt('Hello, :name', {name: 'John'});
    const apples = txts('apples', 5, {name: 'John'});
</script>

Using a custom dictionary

If you are not using the antonioprimera/laravel-js-localization package, you can provide the dictionary of translations manually, by injecting it into the options of the translatorPlugin. If the dictionary is already available as a JS object, you can simply pass it to the plugin. If you are fetching the translations from an API, or if the dictionary is not available at the time of the plugin registration, you can pass a function, which returns the dictionary, to the plugin. This function will be called lazily, when the translations are needed.

import { createApp } from 'vue'
import {translatorPlugin} from 'laravel-inertia-vue-translator'

//fetch the translations from an API
const dictionary = async () => {
    const response = await fetch('/api/translations');
    return await response.json();
}

// Add the plugin to your Vue3 app
return createApp({ render: () => h(App, props) })
    //add the plugin to your Vue3 app
    .use(translatorPlugin, {dictionary})
    .mount('#app')

You can get creative with the way you provide the translations, as long as the dictionary can be resolved to an object with the translations.

Related packages