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

vite-plugin-laravel-translations

v0.2.3

Published

Vite Plugin to make Laravel Translations globally available as JSON field.

Downloads

855

Readme

npm Last Commit npm Total Downloads Vulnerabilities Issues npm peer dependency version

Installation

With pnpm:

pnpm i vite-plugin-laravel-translations

with npm:

npm i vite-plugin-laravel-translations

or with yarn:

yarn add vite-plugin-laravel-translations

Setup with Vite

import laravelTranslations from 'vite-plugin-laravel-translations';

export default defineConfig({
	...
	plugins: [
		laravelTranslations({
			// # TBC: To include JSON files
			includeJson: false,
			// # Declare: namespace (string|false)
			namespace: false,
		}),
	],
});

Usage in Vue 3.x.x

For more information on usage with vue-i18n refer to https://vue-i18n.intlify.dev/guide/#javascript.

// app.js
// 1. Create i18n instance with options
const i18n = VueI18n.createI18n({
  locale: 'ja', // set locale
  fallbackLocale: 'en', // set fallback locale
  messages: LARAVEL_TRANSLATIONS, // set locale messages
  // If you need to specify other options, you can set other options
  // ...
})


// 2. Create a vue root instance
const app = Vue.createApp({
  // set something options
  // ...
})

// 3. Install i18n instance to make the whole app i18n-aware
app.use(i18n)

// 4. Mount
app.mount('#app')

// Now the app has started!
...

Usage in Vue 2.x.x

// app.js
import VueI18n from 'vue-i18n';
Vue.use(VueI18n);

Vue.config.productionTip = false;

var i18n = new VueI18n({
  locale: 'en',
  fallbackLocale: 'en-gb',
  messages: LARAVEL_TRANSLATIONS
});

...
new Vue({
  router,
  i18n,
  render: (h) => h(App),
}).$mount('#app');
...

Usage in React

This example uses i18nnext and react-i18next packages. Refer to https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb#getting-started for extended example.

Vite Config

import laravelTranslations from 'vite-plugin-laravel-translations';

export default defineConfig({
	...
	plugins: [
		laravelTranslations({
			// # Declare: namespace
			namespace: 'translation',
		}),
	],
});

Javascript

// i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    resources: LARAVEL_TRANSLATIONS
  });

export default i18n;


// index.js (React >= 18.0.0)
import React from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';

import './i18n';

const root = createRoot(document.getElementById('root'))
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Usage in JavaScript/NodeJS

const translations = LARAVEL_TRANSLATIONS;
console.log(translations);
...

Custom language directory

By default, the plugin will look for language files in the resources/lang directory if the laravel version is minor than v9, otherwise it will look for language files in the lang directory. You can specify a custom directory by passing the absoluteLanguageDirectory option to the plugin.

...
plugins: [
    laravelTranslations({
        absoluteLanguageDirectory: 'custom/path/to/lang',
    }),
],

Hot-Module Replacement (HMR)

When running vite with dev server running, any changes on any detected lang/ folder for .{php,json} files will restart vite dev server so that the language configurations can be updated.

Known Issues/Caveats

As the LARAVEL_TRANSLATIONS variable is globally available and read by Vite, if it's wrapped into a string it will cause issues on build. DON'T DO: "LARAVEL_TRANSLATIONS" or 'LARAVEL_TRANSLATIONS' E.g. console.log("LARAVEL_TRANSLATIONS")