From d50c17d9fdc59e3175d06f40be619cbda2e6826e Mon Sep 17 00:00:00 2001 From: Tschoatscho Date: Thu, 10 Jun 2021 17:06:11 +0000 Subject: [PATCH 01/13] Prettified Code! --- docs/README.md | 25 +++-- docs/SUMMARY.md | 29 +++--- docs/changelog.md | 241 +++++++++++++++++++++---------------------- docs/faq.md | 9 +- docs/installation.md | 27 +++-- docs/issues.md | 1 - 6 files changed, 170 insertions(+), 162 deletions(-) diff --git a/docs/README.md b/docs/README.md index 21bd6510..d019e205 100644 --- a/docs/README.md +++ b/docs/README.md @@ -59,25 +59,25 @@ echo $post->translate('fr')->title; // Mon premier post ## Tutorials -* [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent) -* [How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application](https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application) -* [How to Add Multi-Language Models to Laravel QuickAdminPanel](https://quickadminpanel.com/blog/how-to-add-multi-language-models-to-laravel-quickadminpanel/) +- [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent) +- [How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application](https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application) +- [How to Add Multi-Language Models to Laravel QuickAdminPanel](https://quickadminpanel.com/blog/how-to-add-multi-language-models-to-laravel-quickadminpanel/) ## Credits -* [Tom Witkowski](https://github.com/Gummibeer) _current maintainer_ -* [Dimitrios Savvopoulos](https://github.com/dimsav) _original author_ -* [David Llop](https://github.com/Lloople) _refactoring testsuite_ -* [Caneco](https://github.com/caneco) _artwork_ -* [All Contributors](https://github.com/Astrotomic/laravel-translatable/graphs/contributors) +- [Tom Witkowski](https://github.com/Gummibeer) _current maintainer_ +- [Dimitrios Savvopoulos](https://github.com/dimsav) _original author_ +- [David Llop](https://github.com/Lloople) _refactoring testsuite_ +- [Caneco](https://github.com/caneco) _artwork_ +- [All Contributors](https://github.com/Astrotomic/laravel-translatable/graphs/contributors) ## Versions -| Package | Laravel | PHP | -| :--- | :--- | :--- | -| **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` | +| Package | Laravel | PHP | +| :---------------- | :---------------------------- | :-------- | +| **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` | | **v11.4 - v11.5** | `5.6.* / 5.7.* / 5.8.* / 6.*` | `>=7.1.3` | -| **v11.0 - v11.3** | `5.6.* / 5.7.* / 5.8.*` | `>=7.1.3` | +| **v11.0 - v11.3** | `5.6.* / 5.7.* / 5.8.*` | `>=7.1.3` | ## Treeware @@ -88,4 +88,3 @@ It’s now common knowledge that one of the best tools to tackle the climate cri You can buy trees at [https://offset.earth/treeware](https://plant.treeware.earth/Astrotomic/laravel-translatable) Read more about Treeware at [https://treeware.earth](https://treeware.earth) - diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index e6ed883a..c82963f5 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -1,23 +1,22 @@ # Table of contents -* [Introduction](README.md) -* [Issues](issues.md) -* [Changelog](changelog.md) -* [FAQ](faq.md) -* [Installation](installation.md) +- [Introduction](README.md) +- [Issues](issues.md) +- [Changelog](changelog.md) +- [FAQ](faq.md) +- [Installation](installation.md) ## Package -* [Interface](package/interface.md) -* [Methods](package/methods.md) -* [Scopes](package/scopes.md) -* [Fallback locale](package/fallback-locale.md) -* [Locales helper](package/locales-helper.md) -* [Validation Rule Factory](package/validation-rule-factory.md) +- [Interface](package/interface.md) +- [Methods](package/methods.md) +- [Scopes](package/scopes.md) +- [Fallback locale](package/fallback-locale.md) +- [Locales helper](package/locales-helper.md) +- [Validation Rule Factory](package/validation-rule-factory.md) ## Usage -* [Attributes](usage/attributes.md) -* [Forms](usage/forms.md) -* [Pivot Model](usage/pivot-model.md) - +- [Attributes](usage/attributes.md) +- [Forms](usage/forms.md) +- [Pivot Model](usage/pivot-model.md) diff --git a/docs/changelog.md b/docs/changelog.md index 014821ec..4c0fa127 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,96 +4,96 @@ ### v11.9.1 -* Fix all methods visibility to allow customization - [\#198](https://github.com/Astrotomic/laravel-translatable/pull/198) +- Fix all methods visibility to allow customization - [\#198](https://github.com/Astrotomic/laravel-translatable/pull/198) ### v11.9.0 -* Add Laravel 8 support - [\#180](https://github.com/Astrotomic/laravel-translatable/pull/180) +- Add Laravel 8 support - [\#180](https://github.com/Astrotomic/laravel-translatable/pull/180) ### v11.8.3 -* Fix `\Astrotomic\Translatable\Exception\LocalesNotDefinedException` exception message +- Fix `\Astrotomic\Translatable\Exception\LocalesNotDefinedException` exception message ### v11.8.1 -* Fix custom locale column name in `\Astrotomic\Translatable\Traits\Relationship::translation()` method - [\#146](https://github.com/Astrotomic/laravel-translatable/pull/146) +- Fix custom locale column name in `\Astrotomic\Translatable\Traits\Relationship::translation()` method - [\#146](https://github.com/Astrotomic/laravel-translatable/pull/146) ### v11.8.0 -* Add Laravel 7 support - [\#128](https://github.com/Astrotomic/laravel-translatable/pull/128) +- Add Laravel 7 support - [\#128](https://github.com/Astrotomic/laravel-translatable/pull/128) ### v11.7.1 -* Add `.gitattributes` to exclude files from archive - [\#126](https://github.com/Astrotomic/laravel-translatable/pull/126) +- Add `.gitattributes` to exclude files from archive - [\#126](https://github.com/Astrotomic/laravel-translatable/pull/126) ### v11.7.0 -* Add `translateOrFail()` method - [\#114](https://github.com/Astrotomic/laravel-translatable/pull/114) +- Add `translateOrFail()` method - [\#114](https://github.com/Astrotomic/laravel-translatable/pull/114) ### v11.6.1 -* Fix PHP translation cascade deletion - [\#105](https://github.com/Astrotomic/laravel-translatable/pull/105) +- Fix PHP translation cascade deletion - [\#105](https://github.com/Astrotomic/laravel-translatable/pull/105) ### v11.6.0 -* Drop PHP 7.1 support -* Drop Laravel 5.6 & 5.7 support -* Add PHP side translation cascade deletion - [\#98](https://github.com/Astrotomic/laravel-translatable/pull/98) \(_disabled by default_\) +- Drop PHP 7.1 support +- Drop Laravel 5.6 & 5.7 support +- Add PHP side translation cascade deletion - [\#98](https://github.com/Astrotomic/laravel-translatable/pull/98) \(_disabled by default_\) ### v11.5.2 -* Fix scope `orderByTranslation()` to return all translations - [\#72](https://github.com/Astrotomic/laravel-translatable/pull/72) +- Fix scope `orderByTranslation()` to return all translations - [\#72](https://github.com/Astrotomic/laravel-translatable/pull/72) ### v11.5.1 -* Fix `getTranslationByLocaleKey()` to use `translation` relation if possible - [\#49](https://github.com/Astrotomic/laravel-translatable/pull/49) +- Fix `getTranslationByLocaleKey()` to use `translation` relation if possible - [\#49](https://github.com/Astrotomic/laravel-translatable/pull/49) ### v11.5.0 -* Add rule parsing to `\Astrotomic\Translatable\Validation\RuleFactory` - [\#56](https://github.com/Astrotomic/laravel-translatable/pull/56) +- Add rule parsing to `\Astrotomic\Translatable\Validation\RuleFactory` - [\#56](https://github.com/Astrotomic/laravel-translatable/pull/56) ### v11.4.0 -* Add Laravel 6 support - [\#52](https://github.com/Astrotomic/laravel-translatable/pull/52) +- Add Laravel 6 support - [\#52](https://github.com/Astrotomic/laravel-translatable/pull/52) ### v11.3.0 -* Add `\Astrotomic\Translatable\Validation\RuleFactory` - [\#34](https://github.com/Astrotomic/laravel-translatable/pull/34) +- Add `\Astrotomic\Translatable\Validation\RuleFactory` - [\#34](https://github.com/Astrotomic/laravel-translatable/pull/34) ### v11.2.1 -* Fix duplicate checks of same locale - [\#40](https://github.com/Astrotomic/laravel-translatable/pull/40) +- Fix duplicate checks of same locale - [\#40](https://github.com/Astrotomic/laravel-translatable/pull/40) ### v11.2.0 -* Add auto fallback locale finder - [\#36](https://github.com/Astrotomic/laravel-translatable/pull/36) +- Add auto fallback locale finder - [\#36](https://github.com/Astrotomic/laravel-translatable/pull/36) ### v11.1.3 -* Fix conflict between attributes and locales during `fill()` - [\#38](https://github.com/Astrotomic/laravel-translatable/pull/38) +- Fix conflict between attributes and locales during `fill()` - [\#38](https://github.com/Astrotomic/laravel-translatable/pull/38) ### v11.1.2 -* Refactor `translation` relationship - [\#23](https://github.com/Astrotomic/laravel-translatable/pull/23) +- Refactor `translation` relationship - [\#23](https://github.com/Astrotomic/laravel-translatable/pull/23) ### v11.1.1 -* Add missing return type-hints - [\#16](https://github.com/Astrotomic/laravel-translatable/pull/16) +- Add missing return type-hints - [\#16](https://github.com/Astrotomic/laravel-translatable/pull/16) ### v11.1.0 -* Add missing dependencies `illuminate/contracts` and `illuminate/database` - [\#9](https://github.com/Astrotomic/laravel-translatable/pull/9) -* Add `\Astrotomic\Translatable\Contracts\Translatable` interface -* Split `\Astrotomic\Translatable\Translatable` into multiple traits - but use them in the main one -* Add `translation` relationship - [\#3](https://github.com/Astrotomic/laravel-translatable/pull/3) -* Flag methods, not defined in interface as `@internal` -* Rename `getRelationKey()` to `getTranslationRelationKey()` to prevent conflicts - the original one is `@deprecated` and will be dropped in next major release -* Update the where translation scopes to unify them and remove duplicated code - [\#2](https://github.com/Astrotomic/laravel-translatable/pull/2) +- Add missing dependencies `illuminate/contracts` and `illuminate/database` - [\#9](https://github.com/Astrotomic/laravel-translatable/pull/9) +- Add `\Astrotomic\Translatable\Contracts\Translatable` interface +- Split `\Astrotomic\Translatable\Translatable` into multiple traits - but use them in the main one +- Add `translation` relationship - [\#3](https://github.com/Astrotomic/laravel-translatable/pull/3) +- Flag methods, not defined in interface as `@internal` +- Rename `getRelationKey()` to `getTranslationRelationKey()` to prevent conflicts - the original one is `@deprecated` and will be dropped in next major release +- Update the where translation scopes to unify them and remove duplicated code - [\#2](https://github.com/Astrotomic/laravel-translatable/pull/2) ### v11.0.0 -* Add PHP7 type-hints [\#557](https://github.com/dimsav/laravel-translatable/pull/557) -* Move to `Astrotomic` [\#1](https://github.com/Astrotomic/laravel-translatable/pull/1) & [\#4](https://github.com/Astrotomic/laravel-translatable/pull/4) +- Add PHP7 type-hints [\#557](https://github.com/dimsav/laravel-translatable/pull/557) +- Move to `Astrotomic` [\#1](https://github.com/Astrotomic/laravel-translatable/pull/1) & [\#4](https://github.com/Astrotomic/laravel-translatable/pull/4) **migrate from dimsav to astrotomic** @@ -104,220 +104,219 @@ ## v10.0.0 -* Add `Dimsav\Translatable\Locales` helper class [\#574](https://github.com/dimsav/laravel-translatable/pull/574) -* Fix `getRelationKey()` [\#575](https://github.com/dimsav/laravel-translatable/pull/575) -* drop custom save method in favor of saved event listener [\#567](https://github.com/dimsav/laravel-translatable/pull/575) +- Add `Dimsav\Translatable\Locales` helper class [\#574](https://github.com/dimsav/laravel-translatable/pull/574) +- Fix `getRelationKey()` [\#575](https://github.com/dimsav/laravel-translatable/pull/575) +- drop custom save method in favor of saved event listener [\#567](https://github.com/dimsav/laravel-translatable/pull/575) ## v9 ### v9.5.0 -* Add `isEmptyTranslatableAttribute()` method to allow custom empty attribute decision logic [\#576](https://github.com/dimsav/laravel-translatable/pull/576) +- Add `isEmptyTranslatableAttribute()` method to allow custom empty attribute decision logic [\#576](https://github.com/dimsav/laravel-translatable/pull/576) ### v9.4.0 -* Add Laravel 5.8 support [\#550](https://github.com/dimsav/laravel-translatable/pull/550) & [\#556](https://github.com/dimsav/laravel-translatable/pull/556) +- Add Laravel 5.8 support [\#550](https://github.com/dimsav/laravel-translatable/pull/550) & [\#556](https://github.com/dimsav/laravel-translatable/pull/556) ### v9.3.0 -* Fix n+1 queries when updating non-translated model attributes [\#533](https://github.com/dimsav/laravel-translatable/pull/533) +- Fix n+1 queries when updating non-translated model attributes [\#533](https://github.com/dimsav/laravel-translatable/pull/533) ### v9.2.0 -* Add support for Laravel 5.7 [\#518](https://github.com/dimsav/laravel-translatable/pull/518) +- Add support for Laravel 5.7 [\#518](https://github.com/dimsav/laravel-translatable/pull/518) ### v9.1.0 -* Do not call get translation with fallback locale and fallback enabled [\#502](https://github.com/dimsav/laravel-translatable/pull/502) -* Allow `translateOrDefault()` and `translateOrNew()` to default to user app locale [\#500](https://github.com/dimsav/laravel-translatable/pull/500) -* Change autoload translations behavior on runtime [\#501](https://github.com/dimsav/laravel-translatable/pull/501) -* Use fallback in `attributesToArray()` [\#503](https://github.com/dimsav/laravel-translatable/pull/503) -* Added `orderByTranslation()` scope [\#504](https://github.com/dimsav/laravel-translatable/pull/504) -* Example in doc for locale filtering in `whereTranslation()` scope [\#487](https://github.com/dimsav/laravel-translatable/pull/487) -* Fire saving event in every case [\#457](https://github.com/dimsav/laravel-translatable/pull/457) -* Allow to change default translation model namespace from config file [\#508](https://github.com/dimsav/laravel-translatable/pull/508) +- Do not call get translation with fallback locale and fallback enabled [\#502](https://github.com/dimsav/laravel-translatable/pull/502) +- Allow `translateOrDefault()` and `translateOrNew()` to default to user app locale [\#500](https://github.com/dimsav/laravel-translatable/pull/500) +- Change autoload translations behavior on runtime [\#501](https://github.com/dimsav/laravel-translatable/pull/501) +- Use fallback in `attributesToArray()` [\#503](https://github.com/dimsav/laravel-translatable/pull/503) +- Added `orderByTranslation()` scope [\#504](https://github.com/dimsav/laravel-translatable/pull/504) +- Example in doc for locale filtering in `whereTranslation()` scope [\#487](https://github.com/dimsav/laravel-translatable/pull/487) +- Fire saving event in every case [\#457](https://github.com/dimsav/laravel-translatable/pull/457) +- Allow to change default translation model namespace from config file [\#508](https://github.com/dimsav/laravel-translatable/pull/508) ### v9.0.0 -* Added support for Laravel 5.6 [\#435](https://github.com/dimsav/laravel-translatable/pull/435) +- Added support for Laravel 5.6 [\#435](https://github.com/dimsav/laravel-translatable/pull/435) ## v8 ### v8.1.0 -* Fixed error when fallback not available. [\#422](https://github.com/dimsav/laravel-translatable/pull/422) -* Fixed withTranslation query scope performance [\#417](https://github.com/dimsav/laravel-translatable/pull/417) -* Fixed fallback for country-based locales [\#417](https://github.com/dimsav/laravel-translatable/pull/417) -* Fixed empty attribute values [\#410](https://github.com/dimsav/laravel-translatable/pull/410) +- Fixed error when fallback not available. [\#422](https://github.com/dimsav/laravel-translatable/pull/422) +- Fixed withTranslation query scope performance [\#417](https://github.com/dimsav/laravel-translatable/pull/417) +- Fixed fallback for country-based locales [\#417](https://github.com/dimsav/laravel-translatable/pull/417) +- Fixed empty attribute values [\#410](https://github.com/dimsav/laravel-translatable/pull/410) ### v8.0.0 -* Added support for Laravel 5.5 [\#394](https://github.com/dimsav/laravel-translatable/pull/394) +- Added support for Laravel 5.5 [\#394](https://github.com/dimsav/laravel-translatable/pull/394) ## v7 ### v7.3.0 -* Added compatibility with custom db connections. [\#366](https://github.com/dimsav/laravel-translatable/pull/366) +- Added compatibility with custom db connections. [\#366](https://github.com/dimsav/laravel-translatable/pull/366) ### v7.2.1 -* Fixed delete events not fired for translations. [\#361](https://github.com/dimsav/laravel-translatable/pull/361) +- Fixed delete events not fired for translations. [\#361](https://github.com/dimsav/laravel-translatable/pull/361) ### v7.2.0 -* Added `replicateWithTranslations()`. [\#346](https://github.com/dimsav/laravel-translatable/pull/346) -* Added `orWhereTranslation()` and `orWhereTranslationLike()` scopes. [\#338](https://github.com/dimsav/laravel-translatable/pull/338) -* Added support for laravel auto-discovery. [\#359](https://github.com/dimsav/laravel-translatable/pull/359) -* Added tag for publishing the config file. [\#360](https://github.com/dimsav/laravel-translatable/pull/360) +- Added `replicateWithTranslations()`. [\#346](https://github.com/dimsav/laravel-translatable/pull/346) +- Added `orWhereTranslation()` and `orWhereTranslationLike()` scopes. [\#338](https://github.com/dimsav/laravel-translatable/pull/338) +- Added support for laravel auto-discovery. [\#359](https://github.com/dimsav/laravel-translatable/pull/359) +- Added tag for publishing the config file. [\#360](https://github.com/dimsav/laravel-translatable/pull/360) ### v7.1.0 -* Added fallback per attribute. [\#348](https://github.com/dimsav/laravel-translatable/pull/348) -* Added `getTranslationsArray()` [\#347](https://github.com/dimsav/laravel-translatable/pull/347) -* Fixed filling 'property:locale' format was not validating the locale. [\#356](https://github.com/dimsav/laravel-translatable/pull/356) +- Added fallback per attribute. [\#348](https://github.com/dimsav/laravel-translatable/pull/348) +- Added `getTranslationsArray()` [\#347](https://github.com/dimsav/laravel-translatable/pull/347) +- Fixed filling 'property:locale' format was not validating the locale. [\#356](https://github.com/dimsav/laravel-translatable/pull/356) ### v7.0.0 -* Added compatibility with Laravel v5.4. -* Added default locale per model. [\#271](https://github.com/dimsav/laravel-translatable/pull/271) +- Added compatibility with Laravel v5.4. +- Added default locale per model. [\#271](https://github.com/dimsav/laravel-translatable/pull/271) ## v6 ### v6.1.0 -* Filling a model now supports using the 'property:locale' format in keys. [\#314](https://github.com/dimsav/laravel-translatable/pull/314) For example: `$country->fill(['name:en' => 'Belgium'])` -* Added config to skip translations in `toArray()` for better performance when needed. [\#315](https://github.com/dimsav/laravel-translatable/pull/315) +- Filling a model now supports using the 'property:locale' format in keys. [\#314](https://github.com/dimsav/laravel-translatable/pull/314) For example: `$country->fill(['name:en' => 'Belgium'])` +- Added config to skip translations in `toArray()` for better performance when needed. [\#315](https://github.com/dimsav/laravel-translatable/pull/315) ### v6.0.1 -* Fix issue when trying to fetch a translation with a country based locale [\#264](https://github.com/dimsav/laravel-translatable/pull/264) +- Fix issue when trying to fetch a translation with a country based locale [\#264](https://github.com/dimsav/laravel-translatable/pull/264) ### v6.0.0 -* Translated fillable properties should only be defined in the translation model. - * To update from version 5, move all the fillable properties belonging to a translation to the corresponding translation models. -* Added `deleteTranslations()` method for conveniently deleting translations +- Translated fillable properties should only be defined in the translation model. + - To update from version 5, move all the fillable properties belonging to a translation to the corresponding translation models. +- Added `deleteTranslations()` method for conveniently deleting translations ## v5 ### v5.6.1 -* Added support for Lumen without Facades [\#259](https://github.com/dimsav/laravel-translatable/pull/259) -* Added support for Model accessors [\#257](https://github.com/dimsav/laravel-translatable/pull/257) -* Updated code style and added [styleci](https://styleci.io/) to enforce it +- Added support for Lumen without Facades [\#259](https://github.com/dimsav/laravel-translatable/pull/259) +- Added support for Model accessors [\#257](https://github.com/dimsav/laravel-translatable/pull/257) +- Updated code style and added [styleci](https://styleci.io/) to enforce it ### v5.6.0 -* Added scope `notTranslatedIn()` [\#235](https://github.com/dimsav/laravel-translatable/pull/235) +- Added scope `notTranslatedIn()` [\#235](https://github.com/dimsav/laravel-translatable/pull/235) ### v5.5.1 -* Fixed a bug in locale fallback on `toArray()` +- Fixed a bug in locale fallback on `toArray()` ### v5.5.0 -* Added Laravel 5.2 support -* Dropped Laravel 5.0 support -* Added scope `whereTranslationLike()` [\#183](https://github.com/dimsav/laravel-translatable/pull/183) -* Fire 'updated' event when saving translations. [\#190](https://github.com/dimsav/laravel-translatable/pull/190) -* `setAttribute()` returns the model itself, which is now the default in eloquent. [\#201](https://github.com/dimsav/laravel-translatable/issues/201) +- Added Laravel 5.2 support +- Dropped Laravel 5.0 support +- Added scope `whereTranslationLike()` [\#183](https://github.com/dimsav/laravel-translatable/pull/183) +- Fire 'updated' event when saving translations. [\#190](https://github.com/dimsav/laravel-translatable/pull/190) +- `setAttribute()` returns the model itself, which is now the default in eloquent. [\#201](https://github.com/dimsav/laravel-translatable/issues/201) ### v5.4 -* Added compatibility with custom primary key [\#174](https://github.com/dimsav/laravel-translatable/issues/174) +- Added compatibility with custom primary key [\#174](https://github.com/dimsav/laravel-translatable/issues/174) ### v5.3 -* Added `whereTranslation()` scope [\#168](https://github.com/dimsav/laravel-translatable/issues/168) +- Added `whereTranslation()` scope [\#168](https://github.com/dimsav/laravel-translatable/issues/168) ### v5.2 -* Added option to override default locale [\#158](https://github.com/dimsav/laravel-translatable/issues/158) -* Added default value in `translatedIn()` scope [\#148](https://github.com/dimsav/laravel-translatable/issues/148) -* Added new scope `withTranslation()]` to decrease the number of mysql calls made. -* Added [documentation](https://github.com/dimsav/laravel-translatable/blob/0715f46613769570b65b97ac9ffec10f9bf06d8d/readme.md#available-scopes) about scopes. +- Added option to override default locale [\#158](https://github.com/dimsav/laravel-translatable/issues/158) +- Added default value in `translatedIn()` scope [\#148](https://github.com/dimsav/laravel-translatable/issues/148) +- Added new scope `withTranslation()]` to decrease the number of mysql calls made. +- Added [documentation](https://github.com/dimsav/laravel-translatable/blob/0715f46613769570b65b97ac9ffec10f9bf06d8d/readme.md#available-scopes) about scopes. ### v5.1.2 -* Fixed db in tests is dropped and recreated to make tests more stable -* Fixed bug when using syntax `$country->{'name:en'}` and locale doesn't exist [\#150](https://github.com/dimsav/laravel-translatable/issues/150) -* Method isTranslationAttribute\(\) is now public [\#151](https://github.com/dimsav/laravel-translatable/issues/151) +- Fixed db in tests is dropped and recreated to make tests more stable +- Fixed bug when using syntax `$country->{'name:en'}` and locale doesn't exist [\#150](https://github.com/dimsav/laravel-translatable/issues/150) +- Method isTranslationAttribute\(\) is now public [\#151](https://github.com/dimsav/laravel-translatable/issues/151) ### v5.1.1 -* Fixed compatibility with Lumen [\#121](https://github.com/dimsav/laravel-translatable/issues/121) -* Fixed making an attribute on a translatable model hidden does not hide it [\#133](https://github.com/dimsav/laravel-translatable/issues/133) +- Fixed compatibility with Lumen [\#121](https://github.com/dimsav/laravel-translatable/issues/121) +- Fixed making an attribute on a translatable model hidden does not hide it [\#133](https://github.com/dimsav/laravel-translatable/issues/133) ### v5.1.0 -* Added mutator/accessor translations using the format `$country->{'name:de'}` thanks to [@barryvdh](https://github.com/barryvdh) -* Added documentation in readme file +- Added mutator/accessor translations using the format `$country->{'name:de'}` thanks to [@barryvdh](https://github.com/barryvdh) +- Added documentation in readme file ### v5.0.1 -* Applied PSR-2 code style. +- Applied PSR-2 code style. ### v5.0.0 -* Laravel 5 ready -* Added configuration option for returning fallback translations +- Laravel 5 ready +- Added configuration option for returning fallback translations ## v4 ### v4.5.0 -* Added scope to list translated attributes in the current locale. -* Force fire "saved" event when the original model is not saved, but the translation is [\#85](https://github.com/dimsav/laravel-translatable/issues/85) +- Added scope to list translated attributes in the current locale. +- Force fire "saved" event when the original model is not saved, but the translation is [\#85](https://github.com/dimsav/laravel-translatable/issues/85) ### v4.4.0 -* Drops support for laravel 4.0. -* Compatible with laravel 4.1 and laravel 4.2. -* External config file. -* Fallback issue fixed. -* Added translated and translatedIn scopes. -* Changed behavior: getting non existing translations with `getTranslation()` used to return objects, now `null` is returned. -* Translated attributes now shown when converting `toArray()` or `toJson()`. -* Fixed bug: `fill()` created empty translations even when translated attributes were not fillable. -* Added option to make translated attributes always fillable. +- Drops support for laravel 4.0. +- Compatible with laravel 4.1 and laravel 4.2. +- External config file. +- Fallback issue fixed. +- Added translated and translatedIn scopes. +- Changed behavior: getting non existing translations with `getTranslation()` used to return objects, now `null` is returned. +- Translated attributes now shown when converting `toArray()` or `toJson()`. +- Fixed bug: `fill()` created empty translations even when translated attributes were not fillable. +- Added option to make translated attributes always fillable. ### v4.3.0 -* The `Translation` class suffix default can be overridden in the app config. See [7ecc0a75d](https://github.com/dimsav/laravel-translatable/commit/7ecc0a75dfcec58ebf694e0a7feb686294b49847) -* The `app.fallback_locale` setting can be overridden in each model separately. See [\#33](https://github.com/dimsav/laravel-translatable/pull/33) -* Fallback translation is not returned if it is not defined. +- The `Translation` class suffix default can be overridden in the app config. See [7ecc0a75d](https://github.com/dimsav/laravel-translatable/commit/7ecc0a75dfcec58ebf694e0a7feb686294b49847) +- The `app.fallback_locale` setting can be overridden in each model separately. See [\#33](https://github.com/dimsav/laravel-translatable/pull/33) +- Fallback translation is not returned if it is not defined. ### v4.2.0 -* Fallback locale now is taken from `app.fallback_locale` config key. +- Fallback locale now is taken from `app.fallback_locale` config key. ### v4.1.1 -* Fixed issue with saving translations, caused by the update of the laravel core. +- Fixed issue with saving translations, caused by the update of the laravel core. ### v4.1.0 -* Added fallback to default locale if translations is missing. [\#23](https://github.com/dimsav/laravel-translatable/issues/23) -* Added travis environment for laravel 4.2. +- Added fallback to default locale if translations is missing. [\#23](https://github.com/dimsav/laravel-translatable/issues/23) +- Added travis environment for laravel 4.2. ### v4.0.0 -* Removed syntax `$model->en->name` because conflicts may happen if the model has a property named `en`. See [\#18](https://github.com/dimsav/laravel-translatable/issues/18). -* Added method `hasTranslation($locale)`. See [\#19](https://github.com/dimsav/laravel-translatable/issues/19). +- Removed syntax `$model->en->name` because conflicts may happen if the model has a property named `en`. See [\#18](https://github.com/dimsav/laravel-translatable/issues/18). +- Added method `hasTranslation($locale)`. See [\#19](https://github.com/dimsav/laravel-translatable/issues/19). ## v3.0.0 -* Fixed bug [\#7](https://github.com/dimsav/laravel-translatable/issues/7). Model's Translations were deleted when the model delete failed. +- Fixed bug [\#7](https://github.com/dimsav/laravel-translatable/issues/7). Model's Translations were deleted when the model delete failed. ## v2.0.0 -* Translatable is now a trait and can be used as add-on to your models. -* 100% code coverage +- Translatable is now a trait and can be used as add-on to your models. +- 100% code coverage ## v1.0.0 -* Initial version -* Translatable is a class extending Eloquent -* 96% code coverage - +- Initial version +- Translatable is a class extending Eloquent +- 96% code coverage diff --git a/docs/faq.md b/docs/faq.md index f78aa058..d245ab74 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -23,10 +23,10 @@ Please see the installation steps to understand how your database should be stru If your properties are written in english, we recommend using these commands in your migrations: ```php -// We insert the old attributes into the fresh translation table: +// We insert the old attributes into the fresh translation table: \DB::statement("insert into country_translations (country_id, name, locale) select id, name, 'en' from countries"); -// We drop the translation attributes in our main table: +// We drop the translation attributes in our main table: Schema::table('posts', function ($table) { $table->dropColumn('title'); $table->dropColumn('content'); @@ -58,8 +58,8 @@ If you see the following mysql error: ```text [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table 'my_database.#sql-455_63' - (errno: 150) (SQL: alter table `country_translations` - add constraint country_translations_country_id_foreign foreign key (`country_id`) + (errno: 150) (SQL: alter table `country_translations` + add constraint country_translations_country_id_foreign foreign key (`country_id`) references `countries` (`id`) on delete cascade) ``` @@ -92,4 +92,3 @@ Schema::create('language_translations', function(Blueprint $table){ ``` The best solution though would be to update your mysql version. And **always make sure you have the same version both in development and production environment!** - diff --git a/docs/installation.md b/docs/installation.md index 6080bf78..fd528ee2 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -13,7 +13,7 @@ composer require astrotomic/laravel-translatable We copy the configuration file to our project. ```bash -php artisan vendor:publish --tag=translatable +php artisan vendor:publish --tag=translatable ``` After this you will have to configure the `locales` your app should use. @@ -40,6 +40,7 @@ That's the only configuration key you **have** to adjust. All the others have a In this example, we want to translate the model `Post`. We will need an extra table `post_translations`: {% code title="create\_posts\_table.php" %} + ```php Schema::create('posts', function(Blueprint $table) { $table->increments('id'); @@ -47,9 +48,11 @@ Schema::create('posts', function(Blueprint $table) { $table->timestamps(); }); ``` + {% endcode %} {% code title="create\_post\_translations\_table" %} + ```php Schema::create('post_translations', function(Blueprint $table) { $table->increments('id'); @@ -62,6 +65,7 @@ Schema::create('post_translations', function(Blueprint $table) { $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); }); ``` + {% endcode %} ## Models @@ -69,6 +73,7 @@ Schema::create('post_translations', function(Blueprint $table) { The translatable model `Post` should [use the trait](http://www.sitepoint.com/using-traits-in-php-5-4/) `Astrotomic\Translatable\Translatable`. The default convention for the translation model is `PostTranslation`. The array `$translatedAttributes` contains the names of the fields being translated in the `PostTranslation` model. {% code title="Post.php" %} + ```php use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract; use Astrotomic\Translatable\Translatable; @@ -76,14 +81,16 @@ use Astrotomic\Translatable\Translatable; class Post extends Model implements TranslatableContract { use Translatable; - + public $translatedAttributes = ['title', 'content']; protected $fillable = ['author']; } ``` + {% endcode %} {% code title="PostTranslation.php" %} + ```php class PostTranslation extends Model { @@ -91,6 +98,7 @@ class PostTranslation extends Model protected $fillable = ['title', 'content']; } ``` + {% endcode %} ### Custom foreign key @@ -98,38 +106,43 @@ class PostTranslation extends Model You may also define a custom foreign key for the package to use, e.g. in case of single table inheritance. So, you have a child class `ChildPost` that inherits from `Post` class, but has the same database table as its parent. {% code title="ChildPost.php" %} + ```php -class ChildPost extends Post +class ChildPost extends Post { protected $table = 'posts'; } ``` + {% endcode %} You will have to create a Translation Class for it. {% code title="ChildPostTranslation.php" %} + ```php use Illuminate\Database\Eloquent\Model; -class ChildPostTranslation extends Model +class ChildPostTranslation extends Model { protected $table = 'post_translations'; public $timestamps = false; - protected $fillable = ['title', 'content']; + protected $fillable = ['title', 'content']; } ``` + {% endcode %} This will try to get data from `post_translations` table using foreign key `child_post_id` according to Laravel. So, in this case, you will have to change the property `$translationForeignKey` to your `'post_id'`. {% code title="ChildPost.php" %} + ```php -class ChildPost extends Post +class ChildPost extends Post { protected $table = 'posts'; protected $translationForeignKey = 'post_id'; } ``` -{% endcode %} +{% endcode %} diff --git a/docs/issues.md b/docs/issues.md index 279f1308..be5fb0fa 100644 --- a/docs/issues.md +++ b/docs/issues.md @@ -3,4 +3,3 @@ Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the package? Feel free to [create an issue on GitHub](https://github.com/Astrotomic/laravel-translatable/issues), we’ll try to address it as soon as possible. If you’ve found a bug regarding security please mail [dev.gummibeer@gmail.com](mailto:dev.gummibeer@gmail.com) instead of using the issue tracker. - From bec641d078661506ef1db1e0e6bf8fbf2f0f5ce2 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Fri, 11 Jun 2021 04:10:06 +0700 Subject: [PATCH 02/13] add methods getTranslationChanges and wasTranslationChanged to Translatable --- src/Translatable/Translatable.php | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index e597272b..83a6abf9 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -4,6 +4,7 @@ use Astrotomic\Translatable\Traits\Relationship; use Astrotomic\Translatable\Traits\Scopes; +use Astrotomic\Translatable\Validation\RuleFactory; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -458,4 +459,35 @@ public function __isset($key) { return $this->isTranslationAttribute($key) || parent::__isset($key); } + + public function getTranslationChanges(): array + { + switch (config('translatable.rule_factory.format')) { + case RuleFactory::FORMAT_KEY: + $replacementFunc = function (string $key, string $locale): string { + return $key . ':' . $locale; + }; + break; + default: + $replacementFunc = function (string $key, string $locale): string { + return $locale . '.' . $key; + }; + } + $translationChanges = array(); + foreach ($this->translations as $translation) { + $locale = $translation->locale; + foreach ($translation->getChanges() as $attribute => $value) { + $translationChanges[$replacementFunc($attribute, $locale)] = $value; + } + } + return $translationChanges; + } + + public function wasTranslationChanged($attributes = null): bool + { + return $this->hasChanges( + $this->getTranslationChanges(), + is_array($attributes) ? $attributes : func_get_args() + ); + } } From 8d5326d0d99a8c16c92295185c2d3b2a2f3e63b0 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Fri, 11 Jun 2021 04:19:41 +0700 Subject: [PATCH 03/13] add dev dependency laravel/legacy-factories for laravel 8 compatibility --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index e6cdadd4..430e4056 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "illuminate/support": "5.8.* || ^6.0 || ^7.0 || ^8.0" }, "require-dev": { + "laravel/legacy-factories": "^1.0.4", "orchestra/testbench": "3.8.* || ^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^8.0 || ^9.0" }, From 5102892be40f876435b325746147c946b13b006d Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Fri, 11 Jun 2021 04:20:06 +0700 Subject: [PATCH 04/13] fix typo in existing test --- tests/TranslatableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index ab21ffbd..cf3ae9f8 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -438,10 +438,10 @@ public function it_returns_if_attribute_is_translated(): void /** @test */ public function config_overrides_apps_locale(): void { - $veegtable = factory(Vegetable::class)->create(['name:de' => 'Erbsen']); + $vegetable = factory(Vegetable::class)->create(['name:de' => 'Erbsen']); App::make('config')->set('translatable.locale', 'de'); - static::assertEquals('Erbsen', $veegtable->name); + static::assertEquals('Erbsen', $vegetable->name); } /** @test */ From 777c3377c2058109027e898bd35b9a52c62a7c14 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Fri, 11 Jun 2021 04:21:13 +0700 Subject: [PATCH 05/13] add unit tests for new methods getTranslationChanges and wasTranslationChanged --- tests/TranslatableTest.php | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index cf3ae9f8..9ce99962 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -9,6 +9,7 @@ use Astrotomic\Translatable\Tests\Eloquent\Person; use Astrotomic\Translatable\Tests\Eloquent\Vegetable; use Astrotomic\Translatable\Tests\Eloquent\VegetableTranslation; +use Astrotomic\Translatable\Validation\RuleFactory; use Illuminate\Database\Eloquent\MassAssignmentException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\App; @@ -1058,4 +1059,49 @@ public function it_can_restore_translations_in_a_transaction() $this->assertDatabaseHas('vegetables', ['identity' => $vegetable->identity]); $this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]); } + + /** @test */ + public function it_returns_no_translation_changes_for_unchanged_model() + { + $vegetable = factory(Vegetable::class)->create([ + 'en' => ['name' => 'Peas'], + 'de' => ['name' => 'Erbsen'] + ]); + $changes = $vegetable->getTranslationChanges(); + static::assertIsArray($changes); + static::assertEmpty($changes); + static::assertFalse($vegetable->wasTranslationChanged()); + } + + /** @test */ + public function it_returns_translation_changes_for_changed_model() + { + $vegetable = factory(Vegetable::class)->create([ + 'en' => ['name' => 'Peas'], + 'de' => ['name' => 'Birnen'] + ]); + $vegetable->fill(['de' => ['name' => 'Erbsen']]); + $vegetable->save(); + + static::assertEquals(['de.name' => 'Erbsen'], $vegetable->getTranslationChanges()); + static::assertTrue($vegetable->wasTranslationChanged()); + static::assertTrue($vegetable->wasTranslationChanged('de.name')); + static::assertFalse($vegetable->wasTranslationChanged('en.name')); + } + + /** @test */ + public function it_returns_translation_changes_in_format_array() + { + $this->app->make('config')->set('translatable.rule_factory.format', RuleFactory::FORMAT_KEY); + $vegetable = factory(Vegetable::class)->create([ + 'en' => ['name' => 'Peas'], + 'de' => ['name' => 'Birnen'] + ]); + $vegetable->fill(['de' => ['name' => 'Erbsen']]); + $vegetable->save(); + + static::assertEquals(['name:de' => 'Erbsen'], $vegetable->getTranslationChanges()); + static::assertTrue($vegetable->wasTranslationChanged('name:de')); + static::assertFalse($vegetable->wasTranslationChanged('de.name')); + } } From 17b01488d3489496cec2ff298f8ba746aa9c5c47 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Fri, 11 Jun 2021 04:54:54 +0700 Subject: [PATCH 06/13] Revert "add dev dependency laravel/legacy-factories for laravel 8 compatibility" This reverts commit 8d5326d0d99a8c16c92295185c2d3b2a2f3e63b0. --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 430e4056..e6cdadd4 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ "illuminate/support": "5.8.* || ^6.0 || ^7.0 || ^8.0" }, "require-dev": { - "laravel/legacy-factories": "^1.0.4", "orchestra/testbench": "3.8.* || ^4.0 || ^5.0 || ^6.0", "phpunit/phpunit": "^8.0 || ^9.0" }, From 845e8f1660207e615636c219caec0f33e9980157 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:10:57 +0700 Subject: [PATCH 07/13] change output of Translatable::getTranslationChanges() to associative array temporarily remove Translatable::wasTranslationChanged() adjust unit tests --- src/Translatable/Translatable.php | 26 ++++---------------------- tests/TranslatableTest.php | 21 ++------------------- 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 83a6abf9..2a136521 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Arr; use Illuminate\Support\Str; /** @@ -462,32 +463,13 @@ public function __isset($key) public function getTranslationChanges(): array { - switch (config('translatable.rule_factory.format')) { - case RuleFactory::FORMAT_KEY: - $replacementFunc = function (string $key, string $locale): string { - return $key . ':' . $locale; - }; - break; - default: - $replacementFunc = function (string $key, string $locale): string { - return $locale . '.' . $key; - }; - } $translationChanges = array(); foreach ($this->translations as $translation) { - $locale = $translation->locale; - foreach ($translation->getChanges() as $attribute => $value) { - $translationChanges[$replacementFunc($attribute, $locale)] = $value; + $changes = $translation->getChanges(); + if (!empty($changes)) { + $translationChanges[$translation->locale] = $changes; } } return $translationChanges; } - - public function wasTranslationChanged($attributes = null): bool - { - return $this->hasChanges( - $this->getTranslationChanges(), - is_array($attributes) ? $attributes : func_get_args() - ); - } } diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 9ce99962..55bdfe63 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -1070,7 +1070,6 @@ public function it_returns_no_translation_changes_for_unchanged_model() $changes = $vegetable->getTranslationChanges(); static::assertIsArray($changes); static::assertEmpty($changes); - static::assertFalse($vegetable->wasTranslationChanged()); } /** @test */ @@ -1083,25 +1082,9 @@ public function it_returns_translation_changes_for_changed_model() $vegetable->fill(['de' => ['name' => 'Erbsen']]); $vegetable->save(); - static::assertEquals(['de.name' => 'Erbsen'], $vegetable->getTranslationChanges()); - static::assertTrue($vegetable->wasTranslationChanged()); - static::assertTrue($vegetable->wasTranslationChanged('de.name')); - static::assertFalse($vegetable->wasTranslationChanged('en.name')); - } + static::assertEquals(['de' => ['name' => 'Erbsen']], $vegetable->getTranslationChanges()); - /** @test */ - public function it_returns_translation_changes_in_format_array() - { $this->app->make('config')->set('translatable.rule_factory.format', RuleFactory::FORMAT_KEY); - $vegetable = factory(Vegetable::class)->create([ - 'en' => ['name' => 'Peas'], - 'de' => ['name' => 'Birnen'] - ]); - $vegetable->fill(['de' => ['name' => 'Erbsen']]); - $vegetable->save(); - - static::assertEquals(['name:de' => 'Erbsen'], $vegetable->getTranslationChanges()); - static::assertTrue($vegetable->wasTranslationChanged('name:de')); - static::assertFalse($vegetable->wasTranslationChanged('de.name')); + static::assertEquals(['de' => ['name' => 'Erbsen']], $vegetable->getTranslationChanges()); } } From 0633a684441ce2a3015fcbc1522627fed778c0a6 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:17:22 +0700 Subject: [PATCH 08/13] add new Translatable::wasTranslationChanged() with unit tests --- src/Translatable/Translatable.php | 48 +++++++++++++++++++++++++++++++ tests/TranslatableTest.php | 30 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 2a136521..a9544eac 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -472,4 +472,52 @@ public function getTranslationChanges(): array } return $translationChanges; } + + public function wasTranslationChanged($attributes = null, $locale = null): bool + { + $translationChanges = $this->getTranslationChanges(); + if (empty($translationChanges)){ + return false; + } + if (empty($attributes)){ + return true; + } + if (!is_array($attributes)){ + $attributes = [$attributes]; + } + if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { + $attributes = array_map(function ($attribute) { return implode('.', array_reverse( explode(':', $attribute)));}, $attributes ); + } + + $attributesWithoutLocale = []; + foreach ($attributes as $attribute) { + if (Arr::get($translationChanges, $attribute)) { + return true; + } + if (!Str::contains($attribute, '.')) { + $attributesWithoutLocale[] = $attribute; + } + } + if (!empty($locale)){ + $localeChanges = Arr::get($translationChanges, $locale); + if (empty($localeChanges)) { + return false; + } + foreach ($attributesWithoutLocale as $attribute) { + if (isset($localeChanges[$attribute])) { + return true; + } + } + } else { + foreach ($attributesWithoutLocale as $attribute) { + foreach ($translationChanges as $localeChanges) { + if (isset($localeChanges[$attribute])) { + return true; + } + } + } + } + + return false; + } } diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 55bdfe63..1aec3ddf 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -1087,4 +1087,34 @@ public function it_returns_translation_changes_for_changed_model() $this->app->make('config')->set('translatable.rule_factory.format', RuleFactory::FORMAT_KEY); static::assertEquals(['de' => ['name' => 'Erbsen']], $vegetable->getTranslationChanges()); } + + /** @test */ + public function it_returns_whether_any_translation_has_changed() + { + $vegetable = factory(Vegetable::class)->create([ + 'en' => ['name' => 'Peas'], + 'de' => ['name' => 'Birnen'] + ]); + static::assertFalse($vegetable-> wasTranslationChanged()); + $vegetable->fill(['de' => ['name' => 'Erbsen']]); + $vegetable->save(); + static::assertTrue($vegetable->wasTranslationChanged()); + static::assertTrue($vegetable->wasTranslationChanged(['name'])); + static::assertTrue($vegetable->wasTranslationChanged('name')); + static::assertTrue($vegetable->wasTranslationChanged('name','de')); + static::assertFalse($vegetable->wasTranslationChanged('name','en')); + static::assertTrue($vegetable->wasTranslationChanged('de.name')); + static::assertTrue($vegetable->wasTranslationChanged('de.name'), 'en'); + static::assertFalse($vegetable->wasTranslationChanged('en.name')); + static::assertFalse($vegetable->wasTranslationChanged('name:de')); + + $this->app->make('config')->set('translatable.rule_factory.format', RuleFactory::FORMAT_KEY); + static::assertTrue($vegetable->wasTranslationChanged('name')); + static::assertTrue($vegetable->wasTranslationChanged('name','de')); + static::assertFalse($vegetable->wasTranslationChanged('name','en')); + static::assertTrue($vegetable->wasTranslationChanged('name:de')); + static::assertFalse($vegetable->wasTranslationChanged('name:en')); + static::assertTrue($vegetable->wasTranslationChanged('de.name')); + static::assertFalse($vegetable->wasTranslationChanged('en.name')); + } } From 6986ffe390fb08c76f7dca2830859986d38355f3 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:36:32 +0700 Subject: [PATCH 09/13] fix formatting issues for styleCI --- src/Translatable/Translatable.php | 18 ++++++++++-------- tests/TranslatableTest.php | 17 +++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index a9544eac..0368eaca 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -463,10 +463,10 @@ public function __isset($key) public function getTranslationChanges(): array { - $translationChanges = array(); + $translationChanges = []; foreach ($this->translations as $translation) { $changes = $translation->getChanges(); - if (!empty($changes)) { + if (! empty($changes)) { $translationChanges[$translation->locale] = $changes; } } @@ -476,17 +476,19 @@ public function getTranslationChanges(): array public function wasTranslationChanged($attributes = null, $locale = null): bool { $translationChanges = $this->getTranslationChanges(); - if (empty($translationChanges)){ + if (empty($translationChanges)) { return false; } - if (empty($attributes)){ + if (empty($attributes)) { return true; } - if (!is_array($attributes)){ + if (! is_array($attributes)){ $attributes = [$attributes]; } if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { - $attributes = array_map(function ($attribute) { return implode('.', array_reverse( explode(':', $attribute)));}, $attributes ); + $attributes = array_map(function ($attribute) { + return implode('.', array_reverse( explode(':', $attribute))); + }, $attributes ); } $attributesWithoutLocale = []; @@ -494,11 +496,11 @@ public function wasTranslationChanged($attributes = null, $locale = null): bool if (Arr::get($translationChanges, $attribute)) { return true; } - if (!Str::contains($attribute, '.')) { + if (! Str::contains($attribute, '.')) { $attributesWithoutLocale[] = $attribute; } } - if (!empty($locale)){ + if (! empty($locale)){ $localeChanges = Arr::get($translationChanges, $locale); if (empty($localeChanges)) { return false; diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 1aec3ddf..40d5f09a 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -769,7 +769,8 @@ public function numeric_translated_attribute(): void $this->app->make('config')->set('translatable.fallback_locale', 'de'); $this->app->make('config')->set('translatable.use_fallback', true); - $vegetable = new class extends Vegetable { + $vegetable = new class extends Vegetable + { protected $table = 'vegetables'; public $translationModel = VegetableTranslation::class; @@ -1077,7 +1078,7 @@ public function it_returns_translation_changes_for_changed_model() { $vegetable = factory(Vegetable::class)->create([ 'en' => ['name' => 'Peas'], - 'de' => ['name' => 'Birnen'] + 'de' => ['name' => 'Birnen'], ]); $vegetable->fill(['de' => ['name' => 'Erbsen']]); $vegetable->save(); @@ -1093,16 +1094,16 @@ public function it_returns_whether_any_translation_has_changed() { $vegetable = factory(Vegetable::class)->create([ 'en' => ['name' => 'Peas'], - 'de' => ['name' => 'Birnen'] + 'de' => ['name' => 'Birnen'], ]); - static::assertFalse($vegetable-> wasTranslationChanged()); + static::assertFalse($vegetable->wasTranslationChanged()); $vegetable->fill(['de' => ['name' => 'Erbsen']]); $vegetable->save(); static::assertTrue($vegetable->wasTranslationChanged()); static::assertTrue($vegetable->wasTranslationChanged(['name'])); static::assertTrue($vegetable->wasTranslationChanged('name')); - static::assertTrue($vegetable->wasTranslationChanged('name','de')); - static::assertFalse($vegetable->wasTranslationChanged('name','en')); + static::assertTrue($vegetable->wasTranslationChanged('name', 'de')); + static::assertFalse($vegetable->wasTranslationChanged('name', 'en')); static::assertTrue($vegetable->wasTranslationChanged('de.name')); static::assertTrue($vegetable->wasTranslationChanged('de.name'), 'en'); static::assertFalse($vegetable->wasTranslationChanged('en.name')); @@ -1110,8 +1111,8 @@ public function it_returns_whether_any_translation_has_changed() $this->app->make('config')->set('translatable.rule_factory.format', RuleFactory::FORMAT_KEY); static::assertTrue($vegetable->wasTranslationChanged('name')); - static::assertTrue($vegetable->wasTranslationChanged('name','de')); - static::assertFalse($vegetable->wasTranslationChanged('name','en')); + static::assertTrue($vegetable->wasTranslationChanged('name', 'de')); + static::assertFalse($vegetable->wasTranslationChanged('name', 'en')); static::assertTrue($vegetable->wasTranslationChanged('name:de')); static::assertFalse($vegetable->wasTranslationChanged('name:en')); static::assertTrue($vegetable->wasTranslationChanged('de.name')); From 60d20de229a9c14637827eb4fdf3d8f3e608c76e Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:45:22 +0700 Subject: [PATCH 10/13] fix formatting issues for styleCI (even more) --- src/Translatable/Translatable.php | 6 +++--- tests/TranslatableTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 0368eaca..0c9ee1ed 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -482,13 +482,13 @@ public function wasTranslationChanged($attributes = null, $locale = null): bool if (empty($attributes)) { return true; } - if (! is_array($attributes)){ + if (! is_array($attributes)) { $attributes = [$attributes]; } if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { $attributes = array_map(function ($attribute) { return implode('.', array_reverse( explode(':', $attribute))); - }, $attributes ); + }, $attributes); } $attributesWithoutLocale = []; @@ -500,7 +500,7 @@ public function wasTranslationChanged($attributes = null, $locale = null): bool $attributesWithoutLocale[] = $attribute; } } - if (! empty($locale)){ + if (! empty($locale)) { $localeChanges = Arr::get($translationChanges, $locale); if (empty($localeChanges)) { return false; diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 40d5f09a..e910a554 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -1066,7 +1066,7 @@ public function it_returns_no_translation_changes_for_unchanged_model() { $vegetable = factory(Vegetable::class)->create([ 'en' => ['name' => 'Peas'], - 'de' => ['name' => 'Erbsen'] + 'de' => ['name' => 'Erbsen'], ]); $changes = $vegetable->getTranslationChanges(); static::assertIsArray($changes); From 8c635c22d1e5104e656123700f85ba7e1fdb084a Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:47:03 +0700 Subject: [PATCH 11/13] fix formatting issues for styleCI (last!) --- src/Translatable/Translatable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 0c9ee1ed..d8b0940c 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -487,7 +487,7 @@ public function wasTranslationChanged($attributes = null, $locale = null): bool } if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { $attributes = array_map(function ($attribute) { - return implode('.', array_reverse( explode(':', $attribute))); + return implode('.', array_reverse(explode(':', $attribute))); }, $attributes); } From 6953156dc34d6df7a9041fe6a648732eedefd465 Mon Sep 17 00:00:00 2001 From: Gio Kettl Date: Mon, 21 Jun 2021 14:48:30 +0700 Subject: [PATCH 12/13] fix formatting issues for styleCI (oh dear!) --- src/Translatable/Translatable.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index d8b0940c..25f70cb1 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -470,6 +470,7 @@ public function getTranslationChanges(): array $translationChanges[$translation->locale] = $changes; } } + return $translationChanges; } From 7163a88a1cc46a7d6e0b3ac4da2b05d5b19ff52a Mon Sep 17 00:00:00 2001 From: Khungio Date: Thu, 23 Sep 2021 21:47:39 +0700 Subject: [PATCH 13/13] Apply suggestions from code review add return types in unit tests add phpdoc comments Co-authored-by: Tom Witkowski --- src/Translatable/Translatable.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 25f70cb1..8da8d7c1 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -474,7 +474,11 @@ public function getTranslationChanges(): array return $translationChanges; } - public function wasTranslationChanged($attributes = null, $locale = null): bool + /** + * @param string[]|string|null $attributes + * @param string|null $locale + */ + public function wasTranslationChanged($attributes = null, ?string $locale = null): bool { $translationChanges = $this->getTranslationChanges(); if (empty($translationChanges)) { @@ -483,9 +487,7 @@ public function wasTranslationChanged($attributes = null, $locale = null): bool if (empty($attributes)) { return true; } - if (! is_array($attributes)) { - $attributes = [$attributes]; - } + $attributes = is_array($attributes) ? $attributes : [$attributes]; if (config('translatable.rule_factory.format') === RuleFactory::FORMAT_KEY) { $attributes = array_map(function ($attribute) { return implode('.', array_reverse(explode(':', $attribute)));