Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman committed Aug 17, 2024
1 parent 4231a37 commit fd10bef
Show file tree
Hide file tree
Showing 32 changed files with 400 additions and 558 deletions.
7 changes: 7 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TRANSLATOR_DRIVER=openai
TRANSLATORS_DEEPL_API_KEY=your-deepl-api-key
TRANSLATORS_GOOGLE_PROJECT=your-google-project
TRANSLATORS_MICROSOFT_KEY=your-microsoft-key
TRANSLATORS_MICROSOFT_REGION=your-microsoft-region
TRANSLATORS_OPENAI_API_KEY=your-openai-api-key
TRANSLATORS_OPENAI_MODEL=gpt-4o-mini
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: :vendor_name
github: datashaman
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/:vendor_name/:package_name/discussions/new?category=q-a
url: https://github.com/datashaman/laravel-translators/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/:vendor_name/:package_name/discussions/new?category=ideas
url: https://github.com/datashaman/laravel-translators/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a security issue
url: https://github.com/:vendor_name/:package_name/security/policy
url: https://github.com/datashaman/laravel-translators/security/policy
about: Learn how to notify us for sensitive bugs
6 changes: 4 additions & 2 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ jobs:
php-version: '8.2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v3
- name: Install dependencies
run: |
composer install --no-interaction --no-progress
composer require $(composer suggests --list datashaman/laravel-translators) --no-interaction --no-progress
- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update
composer require $(composer suggests --list datashaman/laravel-translators) --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: List Installed Dependencies
Expand Down
23 changes: 12 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
.idea
.phpunit.cache
build
composer.lock
coverage
docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
node_modules
/.envrc
/.idea
/.phpunit.cache
/build
/composer.lock
/coverage
/docs
/node_modules
/phpstan.neon
/phpunit.xml
/testbench.yaml
/vendor
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Changelog

All notable changes to `:package_name` will be documented in this file.
All notable changes to `laravel-translators` will be documented in this file.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) :vendor_name <author@domain.com>
Copyright (c) Marlin Forbes <marlinf@datashaman.com>

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 Down
72 changes: 32 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,62 @@
# :package_description
# laravel-translators

[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
<!--delete-->
---
This repo can be used to scaffold a Laravel package. Follow these steps to get started:
[![Latest Version on Packagist](https://img.shields.io/packagist/v/datashaman/laravel-translators.svg?style=flat-square)](https://packagist.org/packages/datashaman/laravel-translators)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/datashaman/laravel-translators/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/datashaman/laravel-translators/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/datashaman/laravel-translators/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/datashaman/laravel-translators/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/datashaman/laravel-translators.svg?style=flat-square)](https://packagist.org/packages/datashaman/laravel-translators)

1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
3. Have fun creating your package.
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
---
<!--/delete-->
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Laravel manager for translators.

## Support us
Includes drivers for:

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/:package_name)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
- [DeepL Translator](https://www.deepl.com/en/pro-api)
- [Google Translate](https://cloud.google.com/translate/docs/)
- [Microsoft Translator](https://learn.microsoft.com/en-us/azure/ai-services/translator/)
- [OpenAI](https://platform.openai.com/)

## Installation

You can install the package via composer:
The OpenAI driver is the default, which can be changed easily.

You must still install the package with the driver you want to use:

```bash
composer require :vendor_slug/:package_slug
composer require datashaman/laravel-translators openai-php/client // for OpenAI
composer require datashaman/laravel-translators google/cloud-translate // for Google Translate
composer require datashaman/laravel-translators deeplcom/deepl-php // for DeepL Translator
```

You can publish and run the migrations with:
The Microsoft Translator driver uses the REST API, so no additional packages are required:

```bash
php artisan vendor:publish --tag=":package_slug-migrations"
php artisan migrate
composer require datashaman/laravel-translators
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag=":package_slug-config"
php artisan vendor:publish --tag="laravel-translators-config"
```

This is the contents of the published config file:
## Usage

```php
return [
];
```
use Datashaman\LaravelTranslators\Facades\Translator;

Optionally, you can publish the views using
// Default is OpenAI
$translations = Translator::translate(['Hello, world!'], 'fr'); // ["Bonjour, monde !"]

```bash
php artisan vendor:publish --tag=":package_slug-views"
```
$translations = Translator::driver('google')->translate(['Hello, World!'], 'fr'); // ["Bonjour le monde!"]

## Usage
$translations = Translator::driver('deepl')->translate(['Hello, World!'], 'fr'); // ["Bonjour à tous !"]

```php
$variable = new VendorName\Skeleton();
echo $variable->echoPhrase('Hello, VendorName!');
$translations = Translator::driver('microsoft')->translate(['Hello, World!'], 'fr'); // ["Salut tout le monde!"]
```

The default driver is OpenAI. You can change the default driver (set `TRANSLATOR_DRIVER` in your `.env` file).

There are various configuration requirements per service to setup before they can be used. There is an [example .env](.example.env) file with all the configuration options. You are also free to modify the config and environment variables (once config is published) to suit your own schema.

## Testing

```bash
Expand All @@ -85,7 +77,7 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [:author_name](https://github.com/:author_username)
- [Datashaman](https://github.com/datashaman)
- [All Contributors](../../contributors)

## License
Expand Down
38 changes: 22 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": ":vendor_slug/:package_slug",
"description": ":package_description",
"name": "datashaman/laravel-translators",
"description": "This is my package laravel-translators",
"keywords": [
":vendor_name",
"laravel",
":package_slug"
"translation",
"localization"
],
"homepage": "https://github.com/:vendor_slug/:package_slug",
"homepage": "https://github.com/datashaman/laravel-translators",
"license": "MIT",
"authors": [
{
"name": ":author_name",
"email": "author@domain.com",
"name": "Marlin Forbes",
"email": "marlinf@datashaman.com",
"role": "Developer"
}
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^10.0||^11.0"
"illuminate/contracts": "^10.0||^11.0",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"laravel/pint": "^1.14",
Expand All @@ -33,21 +33,26 @@
"phpstan/phpstan-phpunit": "^1.3",
"spatie/laravel-ray": "^1.35"
},
"suggest": {
"deeplcom/deepl-php": "For the DeepL Translator driver",
"google/cloud-translate": "For the Google Translate driver",
"openai-php/client": "For the OpenAI driver"
},
"autoload": {
"psr-4": {
"VendorName\\Skeleton\\": "src/",
"VendorName\\Skeleton\\Database\\Factories\\": "database/factories/"
"Datashaman\\LaravelTranslators\\": "src/",
"Datashaman\\LaravelTranslators\\Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"psr-4": {
"VendorName\\Skeleton\\Tests\\": "tests/",
"Datashaman\\LaravelTranslators\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"clear": "@php vendor/bin/testbench package:purge-laravel-translators --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": [
"@composer run prepare",
Expand All @@ -67,16 +72,17 @@
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"php-http/discovery": true
}
},
"extra": {
"laravel": {
"providers": [
"VendorName\\Skeleton\\SkeletonServiceProvider"
"Datashaman\\LaravelTranslators\\LaravelTranslatorsServiceProvider"
],
"aliases": {
"Skeleton": "VendorName\\Skeleton\\Facades\\Skeleton"
"Translator": "Datashaman\\LaravelTranslators\\Facades\\Translator"
}
}
},
Expand Down
6 changes: 0 additions & 6 deletions config/skeleton.php

This file was deleted.

47 changes: 47 additions & 0 deletions config/translators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

return [
'default' => env('TRANSLATOR_DRIVER', 'openai'),

'services' => [
'deepl' => [
'api_key' => env('TRANSLATORS_DEEPL_API_KEY'),
],

'google' => [
'project' => env('TRANSLATORS_GOOGLE_PROJECT'),
],

'microsoft' => [
'key' => env('TRANSLATORS_MICROSOFT_KEY'),
'region' => env('TRANSLATORS_MICROSOFT_REGION', 'westus'),
],

'openai' => [
'api_key' => env('TRANSLATORS_OPENAI_API_KEY', env('OPENAI_API_KEY')),
'model' => env('TRANSLATORS_OPENAI_MODEL', 'gpt-4o-mini'),
'prompt' => ' Translate the following list of messages into the target locale. Preserve the original meaning and formatting. Preserve the order of the messages.',
'response_format' => [
'type' => 'json_schema',
'json_schema' => [
'name' => 'translation',
'strict' => true,
'schema' => [
'type' => 'object',
'properties' => [
'translations' => [
'type' => 'array',
'items' => [
'type' => 'string',
'description' => 'Translated message.',
],
],
],
'required' => ['translations'],
'additionalProperties' => false,
],
],
],
],
],
];
Loading

0 comments on commit fd10bef

Please sign in to comment.