Skip to content

Commit

Permalink
Added Code fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rslanzi committed Mar 14, 2021
1 parent 9e661af commit 8d7437e
Show file tree
Hide file tree
Showing 35 changed files with 1,709 additions and 8,472 deletions.
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.1.0 - 2021-02-18
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- initial release
## [Unreleased]
- Refactoring
- Tests

## [0.2.0] - 2021-03-14
### Added
- Code field support.
- Json field support.

## [0.1.0] - 2021-02-18
### Added
- Nova Translatable field for text and text area
- Single line support.
- Sluggable support.
12 changes: 6 additions & 6 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
MIT License

Copyright (c) Spatie bvba <info@spatie.be>
Copyright (c) 2021 @rslanzi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
201 changes: 95 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,149 +1,138 @@
# Making Nova fields translatable
# Laravel Nova Translatable

This package contains a `Translatable` class you can use to make any Nova field type translatable and groupable into Panel by locale.
This [Laravel Nova](https://nova.laravel.com/) field allows you to manage translated fields with [astrotomic/laravel-translatable](https://github.com/Astrotomic/laravel-translatable).

Imagine you have this `fields` method in a `Post` Nova resource:
## Requirements

```php
public function fields(Request $request)
{
return [
ID::make()->sortable(),

Panel::make('it', []),
Panel::make('en', []),

Translatable::make([
Text::make('title'),
NovaTinyMCE::make('text'),
])->groupsFieldsIntoPanels(),
];
}
```json
laravel/nova: ^2.9 || ^3.0
astrotomic/laravel-translatable: ^11.0
waynestate/nova-ckeditor4-field: ^0.6.0
```

That `Post` Nova resource will be rendered like this.
## Features

![screenshot](https://github.com/rslanzi/nova-translatable/blob/main/docs/screenshot.png?raw=true)
* Supports almost all Nova fields
* Supports default validation automatically
* Simple to implement with minimal code changes (after astrotomic/laravel-translatable support)
* Locale tabs to switch between different locale values of the same field

## Requirements

This Nova field requires Nova 3 specifically and MySQL 5.7.8 or higher.
## Supported fields
* Code
* Counted text (with max char and warning treshold)
* CKEditor
* Json
* Sluggable
* Text (also single line)
* Textarea
* Trix

## Installation

First you must install [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) into your Laravel app. In a nutshell, this package will store translations for your model in a json column in your table. On top of that, it provides many handy functions to store and retrieve translations. Be sure to read [the entire readme of laravel-translatable](https://github.com/spatie/laravel-translatable/blob/master/README.md) before using this Nova package.
Firstly, set up [astrotomic/laravel-translatable](https://github.com/astrotomic/laravel-translatable).

Next, you can install this Nova package into a Laravel app that uses [Nova](https://nova.laravel.com) via composer:
Install the package in a Laravel Nova project via Composer:

```bash
composer require rslanzi/nova-translatable
# Install nova-translatable
composer install rslanzi/nova-translatable

# Publish configuration (optional, but useful for setting default locales)
php artisan vendor:publish --tag="nova-translatable-config"
```

## Usage

In order to use the package you must first let `Translatable` know which locales your app is using using the `Translatable::defaultLocales()` method. You can put this code in `AppServiceProvider` or a dedicated service provider of your own.

### Text Field
Single line text field
```php
// in any service provider

\Rslanzi\NovaTranslatable\Translatable::defaultLocales(['en', 'it']);
NovaTranslatable::make('Title')
->singleLine()
```

Next, you must prepare your model [as explained](https://github.com/spatie/laravel-translatable#making-a-model-translatable) in the readme of laravel-translatable. In short: you must add `json` columns to your model's table for each field you want to translate. Your model must use the `Spatie\Translatable\HasTranslations` on your model. Finally, you must also add a `$translatable` property on your model that holds an array with the translatable attribute names.

Now that your model is configured for translations, you can use `Translatable` in the related Nova resource. Any fields you want to display in a multilingual way can be passed as an array to `Translatable.

### Textarea Field:
Multi line text field
```php
public function fields(Request $request)
{
return [
ID::make()->sortable(),

Translatable::make([
Text::make('title'),
Trix::make('text'),
]),
];
}
NovaTranslatable::make('Text')
->hideFromIndex()
```

### Customizing the locales per translatable

If you have a Nova resource where you want different locales than the ones configured globally, you can call the `locales` method on `Translatable`.

### Counted Text Field:
Text field with chars counter
```php
Translatable::make([
Text::make('title'),
Trix::make('text'),
])->locales(['de', 'es']),
NovaTranslatable::make('Title')
->singleLine()
->counted()
```

These fields will now use the `de` and `es` locales.

### Customizing the name of a translatable

By default translatable fields get ` ($locale)` appended to their name. You can customize this behaviour globally by providing a closure to `displayLocalizedNameByDefaultUsing` on `Translatable`. This callback will be used to render the localized field names.

#### Counted with max chars threshold.
Exceeded the threshold, the counter turns red.
```php
Translatable::displayLocalizedNameByDefaultUsing(function(Field $field, string $locale) {
return ucfirst($field->name) . " [{$locale}]";
})
NovaTranslatable::make('Title')
->singleLine()
->counted()
->maxChars(60)
->warningAt(50),
```

With this in place all names of translatable fields will get ` [$locale]` appended.

You can also customize the localized field name per resource by passing a closure the `displayLocalizedNameUsing` function.

#### Counted with max chars and warning thresholds.
Exceeded the warning threshold, the counter turns orange, exceeded the max chars threshold, the counter turns red.
```php
Translatable::make([
Text::make('title'),
Trix::make('text'),
])->displayLocalizedNameUsing(function(Field $field, string $locale) {
return ucfirst($field->name) . " --- {$locale}]";
}),
NovaTranslatable::make('Title')
->singleLine()
->counted()
->maxChars(60)
->warningAt(50),
```

With this in place, the localized field names will be suffixed with ` --- $locale`.

Of course you can still customize the name of a field as usual.

### CKEditor Field
CKEditor WYSIWYG editor. Usefull to manage HTML fields.
```php
Translatable::make([
Text::make('My title', 'title'),
Trix::make('text'),
])->displayLocalizedNameUsing(function(Field $field, string $locale) {
return ucfirst($field->name) . " [{$locale}]";
}),
NovaTranslatable::make('Text')
->ckeditor()
```

Using the code about above the name for the `title` field will be "My title ['en']".

## On customizing the UI

You might wonder why we didn't render the translatable fields in tabs, panels or with magical unicorns displayed next to them. The truth is that everybody wants translations to be displayed a bit different. That's why we opted to keep them very simple for now.

If Nova gains the ability to better structure a long form natively, we'd probably start leveraging that in a new major version of the package.

### Testing
### Trix Field
Trix field
```php
NovaTranslatable::make('Text')
->trix()
```

```bash
composer test
### Sluggable Field
Automatically populate a slug field based on another field. Title in this case.
```php
NovaTranslatable::make('Title')
->sluggable('Slug'),
NovaTranslatable::make('Slug')
->singleLine()
```

### Changelog
### Code Field
Code field. Use a syntax highlighted text area.
```php
NovaTranslatable::make('Text')
->code()
```
#### Code Field with custom language
Code field. Use a syntax highlighted text area.
```php
NovaTranslatable::make('Text')
->code()
->language('php')
```
**The Code field's currently supported languages are:**
dockerfile, javascript, markdown, nginx, php, ruby, sass, shell, vue, xml, yaml

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
### Json Field
```php
NovaTranslatable::make('Text')
->json()
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.


## Credits

- [Freek Van der Herten](https://github.com/freekmurze)
- [Riccardo Slanzi](https://github.com/rslanzi)
Please make sure to update tests as appropriate.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
This project is open-sourced software licensed under the [MIT license](LICENSE.md).
36 changes: 14 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
{
"name": "rslanzi/nova-translatable",
"description": "Making Nova fields translatable and groupable. Based on spatie/nova-translatable.",
"description": "A Laravel Nova field for astrotomic/laravel-translatable.",
"keywords": [
"laravel",
"nova"
"nova",
"translatable",
"ckeditor"
],
"homepage": "https://github.com/rslanzi/nova-translatable",
"license": "MIT",
"authors": [
{
"name": "Riccardo Slanzi",
"email": "rslanzi@gmail.com",
"role": "Developer"
}
],
"repositories": [
{
"type": "composer",
"url": "https://nova.laravel.com"
"email": "rslanzi@gmail.com"
}
],
"require": {
"php": "^7.2.0|^8.0",
"laravel/nova": "^3.0",
"spatie/laravel-translatable": "^4.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.0",
"phpunit/phpunit": "^8.5|^9.0"
"php": ">=7.3.0",
"laravel/nova": "^2.9 || ^3.0",
"astrotomic/laravel-translatable": "^11.5",
"waynestate/nova-ckeditor4-field": "^0.6.0"
},
"autoload": {
"psr-4": {
"Rslanzi\\NovaTranslatable\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Rslanzi\\NovaTranslatable\\Tests\\": "tests"
"extra": {
"laravel": {
"providers": [
"Rslanzi\\NovaTranslatable\\FieldServiceProvider"
]
}
},
"config": {
Expand Down
Loading

0 comments on commit 8d7437e

Please sign in to comment.