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

v0.1.0

Published

Yet another npm package, which allows you to add translation capabilities to your vue3 components. It pairs best with the composer package: antonioprimera/laravel-js-localization, but can also be used stand-alone.

Downloads

2

Readme

Laravel Vue3 Translator

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

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

While the two packages were built to work together, this package can be easily used as a stand-alone package if you can provide it with a dictionary of translations.

Installation

You can install the package via npm:

npm install laravel-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-vue-translator'

// Get the dictionary of translations as a JS object (see also the section about lazy loading dictionaries)
const dictionary = {
    en: {
		greetings: {
			welcome: 'Hello :name! Welcome to our application!',
        },
        apples: '{0} :name has no apples|{1} :name has one apple|[2,10] :name has :count apples|[11,*] :name has too many apples!',
    },
    de: {
        greetings: {
            welcome: 'Hallo :name! Willkommen in unserer Anwendung!',
        },
        apples: '{0} :name hat keine Äpfel|{1} :name hat einen Apfel|[2,10] :name hat :count Äpfel|[11,*] :name hat zu viele Äpfel!',
    }
}

// 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')

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-vue-translator";
    const {txt, txts} = useTranslator();

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

Using a lazy loaded dictionary

You can provide the dictionary also as a function, which returns the dictionary object. 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 provide a function, which returns the dictionary. This function will be called lazily, when the translations are needed.

import { createApp } from 'vue'
import {translatorPlugin} from 'laravel-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