├── CHANGELOG.md ├── .github ├── FUNDING.yml ├── SECURITY.md ├── dependabot.yml ├── workflows │ ├── fix-php-code-style-issues.yml │ ├── phpstan.yml │ ├── update-changelog.yml │ └── dependabot-auto-merge.yml └── CONTRIBUTING.md ├── docs ├── getting-started │ ├── _index.md │ ├── upgrade.md │ ├── changelog.md │ ├── installation.md │ └── usage.md ├── _index.md ├── filament.md └── introduction.md ├── .prettierrc ├── resources └── lang │ ├── ar │ └── actions.php │ ├── bn │ └── actions.php │ ├── ca │ └── actions.php │ ├── da │ └── actions.php │ ├── de │ └── actions.php │ ├── en │ └── actions.php │ ├── es │ └── actions.php │ ├── fa │ └── actions.php │ ├── fi │ └── actions.php │ ├── fr │ └── actions.php │ ├── he │ └── actions.php │ ├── hu │ └── actions.php │ ├── id │ └── actions.php │ ├── it │ └── actions.php │ ├── ja │ └── actions.php │ ├── km │ └── actions.php │ ├── lt │ └── actions.php │ ├── ms │ └── actions.php │ ├── nl │ └── actions.php │ ├── no │ └── actions.php │ ├── ro │ └── actions.php │ ├── ru │ └── actions.php │ ├── sk │ └── actions.php │ ├── sv │ └── actions.php │ ├── uk │ └── actions.php │ ├── zh_CN │ └── actions.php │ ├── zh_TW │ └── actions.php │ ├── az │ └── actions.php │ ├── ckb │ └── actions.php │ ├── eu │ └── actions.php │ ├── hr │ └── actions.php │ ├── ku │ └── actions.php │ ├── my │ └── actions.php │ ├── np │ └── actions.php │ ├── pt_BR │ └── actions.php │ ├── pt_PT │ └── actions.php │ ├── tr │ └── actions.php │ ├── uz │ └── actions.php │ ├── vi │ └── actions.php │ └── hy │ └── actions.php ├── .editorconfig ├── phpstan.neon ├── src ├── Resources │ ├── Pages │ │ ├── ManageRecords │ │ │ └── Concerns │ │ │ │ └── Translatable.php │ │ ├── Concerns │ │ │ ├── HasTranslatableRecord.php │ │ │ └── HasTranslatableFormWithExistingRecordData.php │ │ ├── ListRecords │ │ │ └── Concerns │ │ │ │ └── Translatable.php │ │ ├── ViewRecord │ │ │ └── Concerns │ │ │ │ └── Translatable.php │ │ ├── CreateRecord │ │ │ └── Concerns │ │ │ │ └── Translatable.php │ │ └── EditRecord │ │ │ └── Concerns │ │ │ └── Translatable.php │ ├── RelationManagers │ │ └── Concerns │ │ │ └── Translatable.php │ └── Concerns │ │ ├── Translatable.php │ │ └── HasActiveLocaleSwitcher.php ├── Actions │ ├── LocaleSwitcher.php │ └── Concerns │ │ └── HasTranslatableLocaleOptions.php ├── SpatieTranslatableServiceProvider.php ├── Tables │ └── Actions │ │ └── LocaleSwitcher.php ├── SpatieTranslatablePlugin.php └── SpatieTranslatableContentDriver.php ├── .gitignore ├── pint.json ├── LICENSE.md ├── composer.json └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [atmonshi] 2 | -------------------------------------------------------------------------------- /docs/getting-started/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Getting Started 3 | weight: 1 4 | --- 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": false, 3 | "singleQuote": true, 4 | "trailingComma": "all" 5 | } 6 | -------------------------------------------------------------------------------- /docs/getting-started/upgrade.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Upgrading 3 | weight: 90 4 | --- 5 | 6 | ## upgrade to v1 7 | 8 | soon -------------------------------------------------------------------------------- /resources/lang/ar/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'اللغة', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/bn/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ভাষা', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ca/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Idioma', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/da/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Sprog', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/de/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Sprache', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/en/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Locale', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/es/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Idioma', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/fa/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'زبان', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/fi/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Lokaali', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/fr/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Locale', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/he/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'שפה', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/hu/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Nyelv', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/id/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Bahasa', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/it/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Locale', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ja/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ロケール', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/km/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ទីតាំង', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/lt/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Kalba', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ms/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Locale', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/nl/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Taal', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/no/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Språk', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ro/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Limba', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ru/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Язык', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/sk/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Jazyk', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/sv/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Språk', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/uk/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Мова', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/zh_CN/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => '语言环境', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/zh_TW/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => '語系', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you discover any security related issues, please email info@larazeus.com instead of using the issue tracker. 4 | -------------------------------------------------------------------------------- /resources/lang/az/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Aktiv dil', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ckb/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ناوچە/زمان', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/eu/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Hizkuntza', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/hr/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Lokalitet', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/ku/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ناوچە/زمان', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/my/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'ဘာသာစကား', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/np/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'स्थानीय भाषा', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/pt_BR/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Idioma', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/pt_PT/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Idioma', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/tr/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Aktif Dil', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/uz/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Mahalliy', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/vi/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Ngôn ngữ', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /resources/lang/hy/actions.php: -------------------------------------------------------------------------------- 1 | [ 6 | 'label' => 'Տեղայնություն', 7 | ], 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /docs/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: v1 3 | slogan: Filament support for Spatie's Laravel Translatable package. 4 | githubUrl: https://github.com/lara-zeus/spatie-translatable 5 | branch: 1.x 6 | icon: carbon-translate 7 | --- 8 | -------------------------------------------------------------------------------- /docs/getting-started/changelog.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Changelog 3 | weight: 100 4 | --- 5 | 6 | All changes to @zeus Translatable are auto updated documented on our site [changelog](https://larazeus.com/spatie-translatable) 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml}] 15 | indent_size = 2 -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | parameters: 2 | level: 9 3 | paths: 4 | - src 5 | 6 | checkOctaneCompatibility: true 7 | checkModelProperties: true 8 | 9 | ignoreErrors: 10 | - 11 | identifier: missingType.iterableValue 12 | - 13 | identifier: trait.unused 14 | -------------------------------------------------------------------------------- /src/Resources/Pages/ManageRecords/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale)) { 12 | return $this->record; 13 | } 14 | 15 | return $this->record->setLocale($this->activeLocale); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/workflows/fix-php-code-style-issues.yml: -------------------------------------------------------------------------------- 1 | name: code style 2 | 3 | on: [push] 4 | 5 | jobs: 6 | php-code-styling: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v6 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Fix PHP code style issues 16 | uses: aglipanci/laravel-pint-action@2.6 17 | 18 | - name: Commit changes 19 | uses: stefanzweifel/git-auto-commit-action@v7 20 | with: 21 | commit_message: Fix styling 22 | -------------------------------------------------------------------------------- /.github/workflows/phpstan.yml: -------------------------------------------------------------------------------- 1 | name: PHPStan 2 | 3 | on: 4 | push: 5 | paths: 6 | - '**.php' 7 | - 'phpstan.neon.dist' 8 | 9 | jobs: 10 | phpstan: 11 | name: phpstan 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v6 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.3' 20 | coverage: none 21 | 22 | - name: Install composer dependencies 23 | uses: ramsey/composer-install@v3 24 | 25 | - name: Run PHPStan 26 | run: ./vendor/bin/phpstan --error-format=github -------------------------------------------------------------------------------- /src/Actions/LocaleSwitcher.php: -------------------------------------------------------------------------------- 1 | label(__('lara-zeus-spatie-translatable::actions.active_locale.label')); 21 | 22 | $this->setTranslatableLocaleOptions(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/SpatieTranslatableServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 12 | $this->publishes([ 13 | __DIR__ . '/../resources/lang' => lang_path('vendor/lara-zeus/spatie-translatable-translations'), 14 | ], 'lara-zeus-spatie-translatable-translations'); 15 | } 16 | 17 | $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'lara-zeus-spatie-translatable'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v6 14 | with: 15 | ref: 1.x 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v7 25 | with: 26 | branch: 1.x 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /src/Tables/Actions/LocaleSwitcher.php: -------------------------------------------------------------------------------- 1 | label(__('lara-zeus-spatie-translatable::actions.active_locale.label')); 25 | 26 | $this->setTranslatableLocaleOptions(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Actions/Concerns/HasTranslatableLocaleOptions.php: -------------------------------------------------------------------------------- 1 | options(function (): array { 12 | 13 | /** @var object $livewire */ 14 | $livewire = $this->getLivewire(); 15 | 16 | if (! method_exists($livewire, 'getTranslatableLocales')) { 17 | return []; 18 | } 19 | 20 | $locales = []; 21 | 22 | /** @var SpatieTranslatablePlugin $plugin */ 23 | $plugin = filament('spatie-translatable'); 24 | 25 | foreach ($livewire->getTranslatableLocales() as $locale) { 26 | $locales[$locale] = $plugin->getLocaleLabel($locale) ?? $locale; 27 | } 28 | 29 | return $locales; 30 | }); 31 | 32 | return $this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) php coder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/Resources/RelationManagers/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale) || 15 | (! in_array($this->activeLocale, $this->getTranslatableLocales(), true)) 16 | ) { 17 | $this->setActiveLocale(); 18 | } 19 | } 20 | 21 | public function getTranslatableLocales(): array 22 | { 23 | return filament('spatie-translatable')->getDefaultLocales(); 24 | } 25 | 26 | public function getDefaultTranslatableLocale(): string 27 | { 28 | return $this->getTranslatableLocales()[0]; 29 | } 30 | 31 | public function getActiveTableLocale(): ?string 32 | { 33 | return $this->activeLocale; 34 | } 35 | 36 | protected function setActiveLocale(): void 37 | { 38 | $this->activeLocale = $this->getDefaultTranslatableLocale(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /.github/workflows/dependabot-auto-merge.yml: -------------------------------------------------------------------------------- 1 | name: dependabot-auto-merge 2 | on: pull_request_target 3 | 4 | permissions: 5 | pull-requests: write 6 | contents: write 7 | 8 | jobs: 9 | dependabot: 10 | runs-on: ubuntu-latest 11 | if: ${{ github.actor == 'dependabot[bot]' }} 12 | steps: 13 | 14 | - name: Dependabot metadata 15 | id: metadata 16 | uses: dependabot/fetch-metadata@v2.4.0 17 | with: 18 | github-token: "${{ secrets.GITHUB_TOKEN }}" 19 | 20 | - name: Auto-merge Dependabot PRs for semver-minor updates 21 | if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} 22 | run: gh pr merge --auto --merge "$PR_URL" 23 | env: 24 | PR_URL: ${{github.event.pull_request.html_url}} 25 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 26 | 27 | - name: Auto-merge Dependabot PRs for semver-patch updates 28 | if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} 29 | run: gh pr merge --auto --merge "$PR_URL" 30 | env: 31 | PR_URL: ${{github.event.pull_request.html_url}} 32 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} -------------------------------------------------------------------------------- /src/Resources/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | getTranslatableAttributes(); 27 | 28 | if (! count($attributes)) { 29 | throw new RuntimeException("Model [{$model}] must have [\$translatable] properties defined."); 30 | } 31 | 32 | return $attributes; 33 | } 34 | 35 | public static function getTranslatableLocales(): array 36 | { 37 | return filament('spatie-translatable')->getDefaultLocales(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Resources/Pages/ListRecords/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale = $this->getStoredActiveLocale() ?? static::getResource()::getDefaultTranslatableLocale(); 28 | } 29 | 30 | public function getTranslatableLocales(): array 31 | { 32 | return static::getResource()::getTranslatableLocales(); 33 | } 34 | 35 | public function getActiveTableLocale(): ?string 36 | { 37 | return $this->activeLocale; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /docs/filament.md: -------------------------------------------------------------------------------- 1 | # Filament Translatable 2 | 3 | Translatable is Filament support for Spatie's Laravel Translatable package. 4 | 5 | ## Features 6 | 7 | - 🔥 Using Spatie translatable package 8 | - 🔥 Default translatable locales 9 | - 🔥 Locale Switcher 10 | - 🔥 Support for create, edit, list and view pages 11 | - 🔥 Setting the translatable locales for a particular resource 12 | - 🔥 Translating relation managers 13 | 14 | ## More Details 15 | 16 | **✨ to learn more about Translatable, please visit:** 17 | 18 | - [Docs](https://larazeus.com/docs/spatie-translatable) 19 | 20 | ## Important Note on Using the Local Switcher 21 | 22 | Please be aware that there are some known limitations when using the local switcher with certain complex field types. 23 | 24 | To avoid potential issues, we recommend disabling the local switcher and using the specified field types instead. 25 | 26 | ### Available Components for Translatable Fields: 27 | 28 | * https://filamentphp.com/plugins/solution-forest-translate-field 29 | * https://filamentphp.com/plugins/mvenghaus-translatable-inline 30 | * https://filamentphp.com/plugins/outerweb-translatable-fields 31 | * https://filamentphp.com/plugins/34ml-translatable-field 32 | 33 | You can also create your own custom fields. Please refer to the following example: 34 | 35 | * https://github.com/lara-zeus/chaos/blob/1.x/src/Forms/Components/MultiLang.php 36 | 37 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lara-zeus/spatie-translatable", 3 | "description": "Filament support for `spatie/laravel-translatable`.", 4 | "license": "MIT", 5 | "homepage": "https://github.com/lara-zeus/spatie-translatable", 6 | "support": { 7 | "issues": "https://github.com/lara-zeus/spatie-translatable/issues", 8 | "source": "https://github.com/lara-zeus/spatie-translatable" 9 | }, 10 | "require": { 11 | "php": "^8.1", 12 | "ext-intl": "*", 13 | "filament/filament": "^4.0", 14 | "spatie/laravel-translatable": "^6.0" 15 | }, 16 | "require-dev": { 17 | "roave/security-advisories": "dev-latest", 18 | "larastan/larastan": "^3.4", 19 | "laravel/pint": "^1.0", 20 | "orchestra/testbench": "^10.2", 21 | "phpstan/extension-installer": "^1.4" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "LaraZeus\\SpatieTranslatable\\": "src" 26 | } 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "LaraZeus\\SpatieTranslatable\\SpatieTranslatableServiceProvider" 32 | ] 33 | } 34 | }, 35 | "config": { 36 | "sort-packages": true, 37 | "allow-plugins": { 38 | "phpstan/extension-installer": true 39 | } 40 | }, 41 | "prefer-stable": true 42 | } 43 | -------------------------------------------------------------------------------- /docs/introduction.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Introduction 3 | weight: 1 4 | --- 5 | 6 | ## Introduction 7 | @zeus Translatable is Filament support for Spatie's Laravel Translatable package. 8 | 9 | ## Features 10 | 11 | - 🔥 Using Spatie translatable package 12 | - 🔥 default translatable locales 13 | - 🔥 Locale Switcher 14 | - 🔥 Support for create, edit, list and view pages 15 | - 🔥 Setting the translatable locales for a particular resource 16 | - 🔥 Translating relation managers 17 | 18 | ## More Details 19 | 20 | **✨ to learn more about Translatable, please visit:** 21 | 22 | - [Docs](https://larazeus.com/docs/spatie-translatable) 23 | 24 | ## Important Note on Using the Local Switcher 25 | 26 | Please be aware that there are some known limitations when using the local switcher with certain complex field types. 27 | 28 | To avoid potential issues, we recommend disabling the local switcher and using the specified field types instead. 29 | 30 | ### Available Components for Translatable Fields: 31 | 32 | * https://filamentphp.com/plugins/solution-forest-translate-field 33 | * https://filamentphp.com/plugins/mvenghaus-translatable-inline 34 | * https://filamentphp.com/plugins/outerweb-translatable-fields 35 | * https://filamentphp.com/plugins/34ml-translatable-field 36 | 37 | You can also create your own custom fields. Please refer to the following example: 38 | 39 | * https://github.com/lara-zeus/chaos/blob/1.x/src/Forms/Components/MultiLang.php 40 | 41 | -------------------------------------------------------------------------------- /src/Resources/Pages/ViewRecord/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale = $this->getStoredActiveLocale() 30 | ?? static::getResource()::getDefaultTranslatableLocale(); 31 | } 32 | 33 | public function updatingActiveLocale(): void 34 | { 35 | $this->oldActiveLocale = $this->activeLocale; 36 | } 37 | 38 | public function updatedActiveLocale(): void 39 | { 40 | if (filament('spatie-translatable')->getPersistLocale()) { 41 | session()->put('spatie_translatable_active_locale', $this->activeLocale); 42 | } 43 | 44 | if (blank($this->oldActiveLocale)) { 45 | return; 46 | } 47 | 48 | $translatableAttributes = static::getResource()::getTranslatableAttributes(); 49 | 50 | $this->otherLocaleData[$this->oldActiveLocale] = Arr::only($this->data, $translatableAttributes); 51 | $this->data = [ 52 | ...$this->data, 53 | ...$this->otherLocaleData[$this->activeLocale] ?? [], 54 | ]; 55 | } 56 | 57 | public function getTranslatableLocales(): array 58 | { 59 | return static::getResource()::getTranslatableLocales(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Resources/Pages/Concerns/HasTranslatableFormWithExistingRecordData.php: -------------------------------------------------------------------------------- 1 | activeLocale ??= $this->getStoredActiveLocale() ?? $this->getDefaultTranslatableLocale(); 16 | 17 | $record = $this->getRecord(); 18 | $translatableAttributes = static::getResource()::getTranslatableAttributes(); 19 | 20 | foreach ($this->getTranslatableLocales() as $locale) { 21 | $translatedData = []; 22 | 23 | foreach ($translatableAttributes as $attribute) { 24 | $translatedData[$attribute] = $record->getTranslation( 25 | $attribute, 26 | $locale, 27 | useFallbackLocale: filament('spatie-translatable')->getUseFallbackLocale() 28 | ); 29 | } 30 | 31 | if ($locale !== $this->activeLocale) { 32 | $this->otherLocaleData[$locale] = $this->mutateFormDataBeforeFill($translatedData); 33 | 34 | continue; 35 | } 36 | 37 | /** @internal Read the DocBlock above the following method. */ 38 | $this->fillFormWithDataAndCallHooks($record, $translatedData); 39 | } 40 | } 41 | 42 | protected function getDefaultTranslatableLocale(): string 43 | { 44 | $resource = static::getResource(); 45 | 46 | $availableLocales = array_keys($this->getRecord()->getTranslations($resource::getTranslatableAttributes()[0])); 47 | $defaultLocale = $resource::getDefaultTranslatableLocale(); 48 | 49 | if (in_array($defaultLocale, $availableLocales, true)) { 50 | return $defaultLocale; 51 | } 52 | 53 | $resourceLocales = $this->getTranslatableLocales(); 54 | 55 | return array_intersect($availableLocales, $resourceLocales)[0] ?? $defaultLocale; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /docs/getting-started/installation.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Installation 3 | weight: 2 4 | --- 5 | 6 | ## Installation 7 | 8 | Install the plugin with Composer: 9 | 10 | ```bash 11 | composer require lara-zeus/spatie-translatable 12 | ``` 13 | 14 | ## Adding the plugin to a panel 15 | 16 | To add a plugin to a panel, you must include it in the configuration file using the `plugin()` method: 17 | 18 | ```php 19 | use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin; 20 | 21 | public function panel(Panel $panel): Panel 22 | { 23 | return $panel 24 | // ... 25 | ->plugin(SpatieTranslatablePlugin::make()); 26 | } 27 | ``` 28 | 29 | ## Persist active local in Session 30 | 31 | to remember the user's selected locale throughout their session, you can pass the method `persist()` 32 | 33 | ```php 34 | use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin; 35 | 36 | public function panel(Panel $panel): Panel 37 | { 38 | return $panel 39 | // ... 40 | ->plugin( 41 | SpatieTranslatablePlugin::make() 42 | ->persist(), 43 | ); 44 | } 45 | ``` 46 | 47 | ## Setting the default translatable locales 48 | 49 | To set up the locales that can be used to translate content, you can pass an array of locales to the `defaultLocales()` plugin method: 50 | 51 | ```php 52 | use LaraZeus\SpatieTranslatable\SpatieTranslatablePlugin; 53 | 54 | public function panel(Panel $panel): Panel 55 | { 56 | return $panel 57 | // ... 58 | ->plugin( 59 | SpatieTranslatablePlugin::make() 60 | ->defaultLocales(['en', 'es']), 61 | ); 62 | } 63 | ``` 64 | 65 | ## Preparing your model class 66 | 67 | You need to make your model translatable. You can read how to do this in [Spatie's documentation](https://spatie.be/docs/laravel-translatable/installation-setup#content-making-a-model-translatable). 68 | 69 | ## Preparing your resource class 70 | 71 | You must apply the `LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable` trait to your resource class: 72 | 73 | ```php 74 | use LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable; 75 | use Filament\Resources\Resource; 76 | 77 | class BlogPostResource extends Resource 78 | { 79 | use Translatable; 80 | 81 | // ... 82 | } 83 | ``` 84 | -------------------------------------------------------------------------------- /src/SpatieTranslatablePlugin.php: -------------------------------------------------------------------------------- 1 | defaultLocales; 47 | } 48 | 49 | public function defaultLocales(?array $defaultLocales = null): static 50 | { 51 | $this->defaultLocales = $defaultLocales; 52 | 53 | return $this; 54 | } 55 | 56 | public function getUseFallbackLocale(): bool 57 | { 58 | return $this->useFallbackLocale; 59 | } 60 | 61 | public function useFallbackLocale(bool $useFallbackLocale = true): static 62 | { 63 | $this->useFallbackLocale = $useFallbackLocale; 64 | 65 | return $this; 66 | } 67 | 68 | public function getPersistLocale(): bool 69 | { 70 | return $this->persistLocale; 71 | } 72 | 73 | public function persist(bool $persistLocale = true): static 74 | { 75 | $this->persistLocale = $persistLocale; 76 | 77 | return $this; 78 | } 79 | 80 | public function getLocaleLabelUsing(?Closure $callback): static 81 | { 82 | $this->getLocaleLabelUsing = $callback; 83 | 84 | return $this; 85 | } 86 | 87 | public function getLocaleLabel(string $locale, ?string $displayLocale = null): ?string 88 | { 89 | $displayLocale ??= session('spatie_translatable_active_locale') ?? app()->getLocale(); 90 | 91 | $label = null; 92 | 93 | if ($callback = $this->getLocaleLabelUsing) { 94 | $label = $callback($locale, $displayLocale); 95 | } 96 | 97 | return $label ?? (locale_get_display_name($locale, $displayLocale) ?: null); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/Resources/Concerns/HasActiveLocaleSwitcher.php: -------------------------------------------------------------------------------- 1 | activeLocale, $this->getTranslatableLocales(), true)) { 19 | return null; 20 | } 21 | 22 | return $this->activeLocale; 23 | } 24 | 25 | public function getActiveActionsLocale(): ?string 26 | { 27 | return $this->activeLocale; 28 | } 29 | 30 | /** 31 | * @return class-string | null 32 | */ 33 | public function getFilamentTranslatableContentDriver(): ?string 34 | { 35 | return SpatieTranslatableContentDriver::class; 36 | } 37 | 38 | public function updatedActiveLocale(): void 39 | { 40 | if (filament('spatie-translatable')->getPersistLocale()) { 41 | session()->put('spatie_translatable_active_locale', $this->activeLocale); 42 | } 43 | 44 | if (blank($this->oldActiveLocale)) { 45 | return; 46 | } 47 | 48 | $this->resetValidation(); 49 | 50 | $translatableAttributes = static::getResource()::getTranslatableAttributes(); 51 | 52 | try { 53 | $this->otherLocaleData[$this->oldActiveLocale] = Arr::only( 54 | $this->form->getState(), 55 | $translatableAttributes 56 | ); 57 | 58 | $this->form->fill([ 59 | ...Arr::except( 60 | $this->form->getState(), 61 | $translatableAttributes 62 | ), 63 | ...$this->otherLocaleData[$this->activeLocale] ?? [], 64 | ]); 65 | 66 | unset($this->otherLocaleData[$this->activeLocale]); 67 | } catch (ValidationException $e) { 68 | $this->activeLocale = $this->oldActiveLocale; 69 | 70 | throw $e; 71 | } 72 | } 73 | 74 | protected function getStoredActiveLocale(): ?string 75 | { 76 | if (! filament('spatie-translatable')->getPersistLocale()) { 77 | return null; 78 | } 79 | 80 | $locale = session()->get('spatie_translatable_active_locale'); 81 | 82 | if ($locale && in_array($locale, $this->getTranslatableLocales(), true)) { 83 | return $locale; 84 | } 85 | 86 | return null; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Resources/Pages/CreateRecord/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale = $this->getStoredActiveLocale() ?? static::getResource()::getDefaultTranslatableLocale(); 37 | } 38 | 39 | public function getTranslatableLocales(): array 40 | { 41 | return static::getResource()::getTranslatableLocales(); 42 | } 43 | 44 | protected function handleRecordCreation(array $data): Model 45 | { 46 | $record = new ($this->getModel())($data); 47 | 48 | $translatableAttributes = static::getResource()::getTranslatableAttributes(); 49 | 50 | $record->fill( 51 | Arr::except($data, $translatableAttributes) 52 | ); 53 | 54 | foreach (Arr::only($data, $translatableAttributes) as $key => $value) { 55 | $record->setTranslation($key, $this->activeLocale, $value); 56 | } 57 | 58 | foreach ($this->otherLocaleData as $locale => $localeData) { 59 | try { 60 | $this->form->fill($this->form->getState()); 61 | } catch (ValidationException $exception) { 62 | continue; 63 | } 64 | 65 | $localeData = $this->mutateFormDataBeforeCreate($localeData); 66 | 67 | foreach (Arr::only($localeData, $translatableAttributes) as $key => $value) { 68 | $record->setTranslation($key, $locale, $value); 69 | } 70 | } 71 | 72 | if ($parentRecord = $this->getParentRecord()) { 73 | return $this->associateRecordWithParent($record, $parentRecord); 74 | } 75 | 76 | $record->save(); 77 | 78 | return $record; 79 | } 80 | 81 | public function updatingActiveLocale(): void 82 | { 83 | $this->oldActiveLocale = $this->activeLocale; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /src/Resources/Pages/EditRecord/Concerns/Translatable.php: -------------------------------------------------------------------------------- 1 | activeLocale = $this->getStoredActiveLocale() ?? static::getResource()::getDefaultTranslatableLocale(); 37 | } 38 | 39 | public function getTranslatableLocales(): array 40 | { 41 | return static::getResource()::getTranslatableLocales(); 42 | } 43 | 44 | protected function handleRecordUpdate(Model $record, array $data): Model 45 | { 46 | $translatableAttributes = static::getResource()::getTranslatableAttributes(); 47 | $record->fill( 48 | Arr::except($data, $translatableAttributes) 49 | ); 50 | 51 | foreach (Arr::only($data, $translatableAttributes) as $key => $value) { 52 | $record->setTranslation($key, $this->activeLocale, $value); 53 | } 54 | 55 | foreach ($this->otherLocaleData as $locale => $localeData) { 56 | try { 57 | $this->form->fill($this->form->getState()); 58 | } catch (ValidationException $exception) { 59 | if (! array_key_exists($locale, $record->locales())) { 60 | continue; 61 | } 62 | 63 | $this->setActiveLocale($locale); 64 | 65 | throw $exception; 66 | } 67 | 68 | $localeData = $this->mutateFormDataBeforeSave($localeData); 69 | 70 | foreach (Arr::only($localeData, $translatableAttributes) as $key => $value) { 71 | $record->setTranslation($key, $locale, $value); 72 | } 73 | } 74 | 75 | $record->save(); 76 | 77 | return $record; 78 | } 79 | 80 | public function updatingActiveLocale(): void 81 | { 82 | $this->oldActiveLocale = $this->activeLocale; 83 | } 84 | 85 | public function setActiveLocale(string $locale): void 86 | { 87 | $this->updatingActiveLocale(); 88 | $this->activeLocale = $locale; 89 | $this->updatedActiveLocale(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 |

Lara Zeus Translatable is Filament support for Spatie's Laravel Translatable package.

6 | 7 |

8 | 9 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/lara-zeus/spatie-translatable.svg?style=flat-square)](https://packagist.org/packages/lara-zeus/spatie-translatable) 10 | [![Code Style](https://img.shields.io/github/actions/workflow/status/lara-zeus/spatie-translatable/fix-php-code-style-issues.yml?label=code-style&flat-square)](https://github.com/lara-zeus/spatie-translatable/actions?query=workflow%3Afix-php-code-style-issues+branch%3A1.x) 11 | [![Total Downloads](https://img.shields.io/packagist/dt/lara-zeus/spatie-translatable.svg?style=flat-square)](https://packagist.org/packages/lara-zeus/spatie-translatable) 12 | [![Total Downloads](https://img.shields.io/github/stars/lara-zeus/spatie-translatable?style=flat-square)](https://github.com/lara-zeus/spatie-translatable) 13 | 14 |

15 | 16 | > [!IMPORTANT] 17 | > This plugin is for filament v4 only, if you're looking for filament v3 support, please check the new repo: [translatable](https://github.com/lara-zeus/spatie-translatable). 18 | 19 | ## Support Filament 20 | 21 | 22 | filament-logo 23 | 24 | 25 | ## Introduction 26 | 27 | This repository is a fork of the [Filament Spatie Laravel Translatable plugin](https://github.com/filamentphp/spatie-laravel-translatable-plugin), [Lara Zeus](https://larazeus.com). 28 | 29 | Our objective is to address existing issues, introduce additional features, and enhance the overall functionality of the plugin. 30 | 31 | We are committed to providing ongoing improvements and welcome contributions and suggestions from the community. 32 | 33 | ## Features 34 | 35 | - 🔥 Using Spatie translatable package 36 | - 🔥 default translatable locales 37 | - 🔥 Locale Switcher 38 | - 🔥 Support for create, edit, list and view pages 39 | - 🔥 Setting the translatable locales for a particular resource 40 | - 🔥 Translating relation managers 41 | 42 | ## Full Documentation 43 | 44 | > Visit our website to get the complete documentation: https://larazeus.com/docs/spatie-translatable 45 | 46 | ### Important Note on Using the Local Switcher 47 | 48 | Please be aware that there are some known limitations when using the local switcher with certain complex field types. 49 | 50 | To avoid potential issues, we recommend disabling the local switcher and using the specified field types instead. 51 | 52 | #### Available Components for Translatable Fields: 53 | 54 | * https://filamentphp.com/plugins/solution-forest-translate-field 55 | * https://filamentphp.com/plugins/mvenghaus-translatable-inline 56 | * https://filamentphp.com/plugins/outerweb-translatable-fields 57 | * https://filamentphp.com/plugins/34ml-translatable-field 58 | 59 | You can also create your own custom fields. Please refer to the following example: 60 | 61 | * https://github.com/lara-zeus/chaos/blob/1.x/src/Forms/Components/MultiLang.php 62 | 63 | ## Changelog 64 | 65 | Please see [CHANGELOG](CHANGELOG.md) for more information on recent changes. 66 | 67 | ## Support 68 | available support channels: 69 | * open an issue on [GitHub](https://github.com/lara-zeus/spatie-translatable/issues) 70 | * Email us using the [contact center](https://larazeus.com/contact-us) 71 | 72 | ## Contributing 73 | 74 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 75 | 76 | ## Security 77 | 78 | If you find any security-related issues, please email info@larazeus.com instead of using the issue tracker. 79 | 80 | ## Credits 81 | 82 | - [Dan Harrin](https://github.com/danharrin) 83 | - [php coder](https://github.com/atmonshi) 84 | - [Mohamed Sabil](https://github.com/mohamedsabil83) 85 | - [All Contributors](../../contributors) 86 | 87 | ## License 88 | 89 | The MIT License (MIT). Please have a look at [License File](LICENSE.md) for more information. 90 | -------------------------------------------------------------------------------- /src/SpatieTranslatableContentDriver.php: -------------------------------------------------------------------------------- 1 | isTranslatableAttribute($attribute); 28 | } 29 | 30 | /** 31 | * @param array $data 32 | */ 33 | public function makeRecord(string $model, array $data): Model 34 | { 35 | $record = new $model; 36 | 37 | if (method_exists($record, 'setLocale')) { 38 | $record->setLocale($this->activeLocale); 39 | } 40 | 41 | $translatableAttributes = method_exists($record, 'getTranslatableAttributes') ? 42 | $record->getTranslatableAttributes() : 43 | []; 44 | 45 | /** @var Model $record */ 46 | $record->fill(Arr::except($data, $translatableAttributes)); 47 | 48 | if (method_exists($record, 'setTranslation')) { 49 | foreach (Arr::only($data, $translatableAttributes) as $key => $value) { 50 | $record->setTranslation($key, $this->activeLocale, $value); 51 | } 52 | } 53 | 54 | return $record; 55 | } 56 | 57 | public function setRecordLocale(Model $record): Model 58 | { 59 | if (! method_exists($record, 'setLocale')) { 60 | return $record; 61 | } 62 | 63 | return $record->setLocale($this->activeLocale); 64 | } 65 | 66 | /** 67 | * @param array $data 68 | */ 69 | public function updateRecord(Model $record, array $data): Model 70 | { 71 | if (method_exists($record, 'setLocale')) { 72 | $record->setLocale($this->activeLocale); 73 | } 74 | 75 | $translatableAttributes = method_exists($record, 'getTranslatableAttributes') ? 76 | $record->getTranslatableAttributes() : 77 | []; 78 | 79 | $record->fill(Arr::except($data, $translatableAttributes)); 80 | 81 | if (method_exists($record, 'setTranslation')) { 82 | foreach (Arr::only($data, $translatableAttributes) as $key => $value) { 83 | $record->setTranslation($key, $this->activeLocale, $value); 84 | } 85 | } 86 | 87 | $record->save(); 88 | 89 | return $record; 90 | } 91 | 92 | /** 93 | * @return array 94 | */ 95 | public function getRecordAttributesToArray(Model $record): array 96 | { 97 | $attributes = $record->attributesToArray(); 98 | 99 | if (! method_exists($record, 'getTranslatableAttributes')) { 100 | return $attributes; 101 | } 102 | 103 | if (! method_exists($record, 'getTranslation')) { 104 | return $attributes; 105 | } 106 | 107 | foreach ($record->getTranslatableAttributes() as $attribute) { 108 | $attributes[$attribute] = $record->getTranslation( 109 | $attribute, 110 | $this->activeLocale, 111 | useFallbackLocale: (new SpatieTranslatablePlugin)->getUseFallbackLocale() 112 | ); 113 | } 114 | 115 | return $attributes; 116 | } 117 | 118 | public function applySearchConstraintToQuery(Builder $query, string $column, string $search, string $whereClause, ?bool $isCaseInsensitivityForced = null): Builder 119 | { 120 | /** @var Connection $databaseConnection */ 121 | $databaseConnection = $query->getConnection(); 122 | 123 | $column = match ($databaseConnection->getDriverName()) { 124 | 'pgsql' => "{$column}->>'{$this->activeLocale}'", 125 | default => "json_extract({$column}, '$.\"{$this->activeLocale}\"')", 126 | }; 127 | 128 | $search = generate_search_term_expression($search, $isCaseInsensitivityForced, $databaseConnection); 129 | 130 | return $query->{$whereClause}( 131 | generate_search_column_expression($column, $isCaseInsensitivityForced, $databaseConnection), 132 | 'like', 133 | str($search)->wrap('%')->toString(), 134 | ); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /docs/getting-started/usage.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Usage 3 | weight: 4 4 | --- 5 | 6 | ## Making resource pages translatable 7 | 8 | After [preparing your resource class](#preparing-your-resource-class), you must make each of your resource's pages translatable too. You can find your resource's pages in the `Pages` directory of each resource folder. To prepare a page, you must apply the corresponding `Translatable` trait to it, and install a `LocaleSwitcher` header action: 9 | 10 | ```php 11 | use Filament\Actions; 12 | use Filament\Resources\Pages\ListRecords; 13 | use LaraZeus\SpatieTranslatable\Resources\Pages\ListRecords\Concerns\Translatable; 14 | use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; 15 | 16 | class ListBlogPosts extends ListRecords 17 | { 18 | use Translatable; 19 | 20 | protected function getHeaderActions(): array 21 | { 22 | return [ 23 | LocaleSwitcher::make(), 24 | // ... 25 | ]; 26 | } 27 | 28 | // ... 29 | } 30 | ``` 31 | 32 | ```php 33 | use Filament\Actions; 34 | use Filament\Resources\Pages\CreateRecord; 35 | use LaraZeus\SpatieTranslatable\Resources\Pages\CreateRecord\Concerns\Translatable; 36 | use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; 37 | 38 | class CreateBlogPost extends CreateRecord 39 | { 40 | use Translatable; 41 | 42 | protected function getHeaderActions(): array 43 | { 44 | return [ 45 | LocaleSwitcher::make(), 46 | // ... 47 | ]; 48 | } 49 | 50 | // ... 51 | } 52 | ``` 53 | 54 | ```php 55 | use Filament\Actions; 56 | use Filament\Resources\Pages\EditRecord; 57 | use LaraZeus\SpatieTranslatable\Resources\Pages\EditRecord\Concerns\Translatable; 58 | use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; 59 | 60 | class EditBlogPost extends EditRecord 61 | { 62 | use Translatable; 63 | 64 | protected function getHeaderActions(): array 65 | { 66 | return [ 67 | LocaleSwitcher::make(), 68 | // ... 69 | ]; 70 | } 71 | 72 | // ... 73 | } 74 | ``` 75 | 76 | And if you have a `ViewRecord` page for your resource: 77 | 78 | ```php 79 | use Filament\Actions; 80 | use Filament\Resources\Pages\ViewRecord; 81 | use LaraZeus\SpatieTranslatable\Resources\Pages\ViewRecord\Concerns\Translatable; 82 | use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; 83 | 84 | class ViewBlogPost extends ViewRecord 85 | { 86 | use Translatable; 87 | 88 | protected function getHeaderActions(): array 89 | { 90 | return [ 91 | LocaleSwitcher::make(), 92 | // ... 93 | ]; 94 | } 95 | 96 | // ... 97 | } 98 | ``` 99 | 100 | If you're using a simple resource, you can make the `ManageRecords` page translatable instead: 101 | 102 | ```php 103 | use Filament\Actions; 104 | use Filament\Resources\Pages\ManageRecords; 105 | use LaraZeus\SpatieTranslatable\Resources\Pages\ManageRecords\Concerns\Translatable; 106 | use LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher; 107 | 108 | class ManageBlogPosts extends ListRecords 109 | { 110 | use Translatable; 111 | 112 | protected function getHeaderActions(): array 113 | { 114 | return [ 115 | LocaleSwitcher::make(), 116 | // ... 117 | ]; 118 | } 119 | 120 | // ... 121 | } 122 | ``` 123 | 124 | ### Setting the translatable locales for a particular resource 125 | 126 | By default, the translatable locales can be [set globally for all resources in the plugin configuration](#setting-the-default-translatable-locales). Alternatively, you can customize the translatable locales for a particular resource by overriding the `getTranslatableLocales()` method in your resource class: 127 | 128 | ```php 129 | use LaraZeus\SpatieTranslatable\Resources\Concerns\Translatable; 130 | use Filament\Resources\Resource; 131 | 132 | class BlogPostResource extends Resource 133 | { 134 | use Translatable; 135 | 136 | // ... 137 | 138 | public static function getTranslatableLocales(): array 139 | { 140 | return ['en', 'fr']; 141 | } 142 | } 143 | ``` 144 | 145 | ## Translating relation managers 146 | 147 | First, you must apply the `LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable` trait to the relation manager class: 148 | 149 | ```php 150 | use LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable; 151 | use Filament\Resources\RelationManagers\RelationManager; 152 | 153 | class BlogPostsRelationManager extends RelationManager 154 | { 155 | use Translatable; 156 | 157 | // ... 158 | } 159 | ``` 160 | 161 | Now, you can add a new `LocaleSwitcher` action to the header of the relation manager's `table()`: 162 | 163 | ```php 164 | use Filament\Tables; 165 | use Filament\Tables\Table; 166 | 167 | public function table(Table $table): Table 168 | { 169 | return $table 170 | ->columns([ 171 | // ... 172 | ]) 173 | ->headerActions([ 174 | // ... 175 | \LaraZeus\SpatieTranslatable\Actions\LocaleSwitcher::make(), 176 | ]); 177 | } 178 | ``` 179 | 180 | ### Inheriting the relation manager's active locale from the resource page 181 | 182 | If you wish to reactively inherit the locale of the `Translatable` resource page that the relation manager is being displayed on, you can override the `$activeLocale` property and add Livewire's `Reactive` attribute to it: 183 | 184 | ```php 185 | use LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable; 186 | use Filament\Resources\RelationManagers\RelationManager; 187 | use Livewire\Attributes\Reactive; 188 | 189 | class BlogPostsRelationManager extends RelationManager 190 | { 191 | use Translatable; 192 | 193 | #[Reactive] 194 | public ?string $activeLocale = null; 195 | 196 | // ... 197 | } 198 | ``` 199 | 200 | If you do this, you no longer need a `LocaleSwitcher` action in the `table()`. 201 | 202 | ### Setting the translatable locales for a particular relation manager 203 | 204 | By default, the translatable locales can be [set globally for all relation managers in the plugin configuration](#setting-the-default-translatable-locales). Alternatively, you can customize the translatable locales for a particular relation manager by overriding the `getTranslatableLocales()` method in your relation manager class: 205 | 206 | ```php 207 | use LaraZeus\SpatieTranslatable\Resources\RelationManagers\Concerns\Translatable; 208 | use Filament\Resources\RelationManagers\RelationManager; 209 | 210 | class BlogPostsRelationManager extends RelationManager 211 | { 212 | use Translatable; 213 | 214 | // ... 215 | 216 | public function getTranslatableLocales(): array 217 | { 218 | return ['en', 'fr']; 219 | } 220 | } 221 | ``` 222 | 223 | ## Publishing translations 224 | 225 | If you wish to translate the package, you may publish the language files using: 226 | 227 | ```bash 228 | php artisan vendor:publish --tag=lara-zeus-spatie-translatable-translations 229 | ``` 230 | --------------------------------------------------------------------------------