• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Push your Laravel translations to the front-end and use them easily with JavaScript.

I18n

Push your Laravel translations to the front-end and use them easily with JavaScript.

A nice tool for SPAs and front-end heavy applications.

If you have any question how the package works, we suggest to read this post: Using Laravel’s Localization in JS.

Getting started

You can install the package with composer, running the composer require conedevelopment/i18n command.

Translations in view files

You can use the @translations blade directive. This directive automatically wraps the translations to a <script> tag.

@translations

<!-- The result -->
<script>window['translations'] = { auth: {...}, validation: {...} }</script>

You may override the default key for the translations. You can do that by passing a string to the blade directive.

@translations ('myTranslations')

<!-- The result -->
<script>window['myTranslations'] = { auth: {...}, validation: {...} }</script>

Publishing and using the JavaScript library

Use the php artisan vendor:publish command and choose the Pine\I18n\I18nServiceProvider provider. After publishing you can find your fresh copy in the resources/js/vendor folder.

Using the I18n.js

Then you can import the I18n class and assign it to the window object.

import I18n from './vendor/I18n';
window.I18n = I18n;

Initializing a translation instance

From this point you can initialize the translation service anywhere from your application.

let translator = new I18n;

By default, it uses the translations key in the window object. If you want to use the custom one you set in the blade directive, pass the same key to the constructor.

let translator = new I18n('myTranslations');

Using it as a Vue service

If you want to use it from Vue templates directly you can extend Vue with this easily.

Vue.prototype.$I18n = new I18n;

You can call it from your template or the script part of your component like below:

<template>
    <div>{{ $I18n.trans('some.key') }}</div>
</template>
computed: {
    translations: {
        something: this.$I18n.trans('some.key')
    }
}

Methods

The package comes with two methods on JS side. The trans() and the trans_choice().

trans()

The trans method accepts the key of the translation and the attributes what we want to replace, but it's optional.

translator.trans('auth.failed');

// These credentials do not match our records.

translator.trans('auth.throttle', { seconds: 60 });

// Too many login attempts. Please try again in 60 seconds.

trans_choice()

The trans_choice method determines if the translation should be pluralized or nor by the given cout. Also, it accepts the attributes we want to replace.

Let's say we have the following translation line:

[
    'attempts' => 'Be careful, you have :attempts attempt left.|You still have :attempts attempts left.',
]

Note, the plural and the singular verions are separated with the | character!

translator.trans_choice('auth.attempts', 1, { attempts: 'only one' });

// Be careful, you have only one attempt left.

translator.trans_choice('auth.attempts', 4, { attempts: 'less than five' });

// You still have less than five attempts left.

Like in Laravel, you have the ability to set ranges for the pluralization. Also, you can replace placeholders like before.

[
    'apples' => '{0} There are none|[1,19] There are some (:number)|[20,*] There are many (:number)',
]

You can separate more than two choices with the | character.

translator.trans_choice('messages.apples', 0);

// There are none

translator.trans_choice('auth.attempts', 8, { number: 8 });

// There are some (8)

translator.trans_choice('auth.attempts', 25, { number: 25 });

// There are many (25)

Transforming replacement parameters

Like in Laravel's functionality, you can transform your parameters to upper case, or convert only the first character to capital letter. All you need to do, to modify your placeholders.

[
    'welcome' => 'Welcome, :NAME',
    'goodbye' => 'Goodbye, :Name',
]

If you want, you can pass the same parameter with different modifiers in one line as well, like :NAME, :name or :Name.

translator.trans('messages.welcome', { name: 'pine' });

// Welcome, PINE

translator.trans('messages.goodbye', { name: 'pine' });

// Goodbye, Pine

Package translations

Thanks to the idea of Jonathan, package translations are supported by default. You can access to the translations as in Laravel, using the predefined namespace.

translator.trans('courier::messages.message');

Multiple locales

Multiple locales are supported. You can change the application's locale anytime. Behind the scenes the proper translations will be rendered, if it exists.

Fallback locales

If there are no translations is not available in the current langauge, the package will look for the fallback locale's translations. If there is no translations available in the fallback locale, the missing translations won't appear.

Performance

The translations are generated when the views are compiled. It means they are cached and stored as strings in the compiled views. It's much more performance friendly than generating them on runtime or running and AJAX request to fetch the translations.

Behind the scenes there is a switch - case that determines which translations should be present, based on the current locale. This way only the current translations are pushed to the window object and not all of them.

Note: On local environment the cached views are getting cleared to keep translations fresh.

Limitations

The JSON file translations are not supported! Only the PHP array based key - value translations can be used. Also, please pay attention to place and use the tranlations the way Laravel requires it.

Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.

More Repositories

1

blade-filters

Use filters easily in your blade templates.
PHP
487
star
2

bazar

Bazar is an e-commerce package for Laravel applications.
PHP
421
star
3

sprucecss

An open-source, lightweight and modern CSS framework, design system built on Sass. Give your project a solid foundation.
SCSS
212
star
4

canvi

A simple vanilla JavaScript off-canvas menu.
JavaScript
108
star
5

policy

Using Laravel's authorization on the front-end.
PHP
97
star
6

simplepay-gateway

OTP SimplePay bővítmény WooCommerce (WordPress) áruházak részére.
PHP
57
star
7

sprucecss-eleventy-documentation-template

A simple documentation template made with Eleventy and Spruce CSS.
SCSS
50
star
8

bite-sized-a11y

Quick and Small Accessibility - Learn something new about accessibility with short and solid articles (less than 150 words).
CSS
28
star
9

root

Root is an admin package for Laravel applications.
PHP
20
star
10

wp-theme-alpha

Alpha is a WordPress blog and magazine theme with clean design, nice code, and Gutenberg support.
CSS
18
star
11

sprucecss-root-admin-template

A static HTML/CSS/JS administration tempalte built on Spruce CSS.
HTML
8
star
12

accessibility-path

Accessibility Path is a collection of resources that can help you to learn web accessibility and inclusive design.
7
star
13

sprucecss-starter-kit

The “Spruce CSS Starter Kit” repository contains a simple foundation for starting your next project with ease using Spruce CSS.
SCSS
6
star
14

billingo-php

Billingo PHP SDK.
PHP
5
star
15

sprucecss-site

The documentation of Spruce CSS.
MDX
5
star
16

bazar-docs

The Bazar Laravel e-commerce package documentation.
4
star
17

wordpress-plugin-template

WordPress plugin skeleton template.
PHP
4
star
18

qkie

Simple cookie management.
JavaScript
3
star
19

laravel-package-template

Laravel package skeleton template.
PHP
3
star
20

sprucecss-eleventy-starter

A minimalistic Eleventy boilerplate setup with Spruce CSS to help you start with your next project.
CSS
3
star
21

cone-site

The new 11ty-based portfolio website of Cone.
CSS
3
star
22

sprucecss-site-eleventy

The documentation of Spruce CSS.
CSS
3
star
23

sprucecss-examples

Simple examples for how to use and how to get started with Spruce CSS.
CSS
2
star
24

bazar-stripe

The Stripe Payment gateway for Bazar.
PHP
2
star