From a74eb22871f366aebe2211bbe844d6c85fb925a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Tue, 7 Apr 2026 12:23:40 +0000 Subject: [PATCH] Fix implementation of trans_choice and improve README --- CHANGELOG.md | 5 +++ README.md | 67 +++++++++++++++++---------------- resources/js/I18n.js | 84 +++++++++++++++++++++++++++++++----------- tests/I18n.test.mjs | 88 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+), 53 deletions(-) create mode 100644 tests/I18n.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index a51d617..ebef2bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +# Unreleased +### Fixed +- Make the JavaScript `trans_choice()` implementation match Laravel's selector behavior +- Fix JavaScript `trans_choice()` handling for literal strings and explicit pluralization selectors + # v1.2.0 (2022-02-11) ### Added - Laravel 9 compatibility [#33](https://github.com/conedevelopment/i18n/pull/33) diff --git a/README.md b/README.md index 10abe3b..e50c568 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # I18n -Push your Laravel translations to the front-end and use them easily with JavaScript. +Push your Laravel translations to the front end and use them easily with JavaScript. -A nice tool for SPAs and front-end heavy applications. +A useful tool for SPAs and front-end-heavy applications. -If you have any question how the package works, we suggest to read this post: +If you have any questions about how the package works, we suggest reading this post: [Using Laravel’s Localization in JS](https://pineco.de/using-laravels-localization-js/). ## Getting started -You can install the package with composer, running the `composer require conedevelopment/i18n` command. +Install the package with Composer by running `composer require conedevelopment/i18n`. ## Translations in view files @@ -34,12 +34,12 @@ You may override the default key for the translations. You can do that by passin ## 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. +Run `php artisan vendor:publish` and choose the `Pine\I18n\I18nServiceProvider` provider. +After publishing, you can find the generated file in the `resources/js/vendor` directory. ### Using the I18n.js -Then you can import the `I18n` class and assign it to the `window` object. +You can then import the `I18n` class and assign it to the `window` object. ```js import I18n from './vendor/I18n'; @@ -48,28 +48,31 @@ window.I18n = I18n; ### Initializing a translation instance -From this point you can initialize the translation service anywhere from your application. +You can initialize the translation service anywhere in your application. ```js 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. +If you want to use the custom key you set in the Blade directive, pass the same key to the constructor. ```js let translator = new I18n('myTranslations'); ``` +`trans_choice()` falls back to the first form when the count is `1` and to the second form otherwise, +unless an explicit selector like `{0}` or `[2,9]` matches first. + ### Using it as a Vue service -If you want to use it from Vue templates directly you can extend Vue with this easily. +If you want to use it directly in Vue templates, you can extend Vue like this: ```js Vue.prototype.$I18n = new I18n; ``` -You can call it from your template or the script part of your component like below: +You can call it from your template or from the script section of your component: ```html