Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ APP_EMAIL_NEW_USERS_NOTIFICATION=EmailThatWillSendNotificationsForNewUser
# https://github.com/monicahq/monica/blob/master/resources/views/settings/index.blade.php#L70
APP_DEFAULT_TIMEZONE=US/Eastern

# Default locale used in the application.
APP_DEFAULT_LOCALE=en

# Ability to disable signups on your instance.
# Can be true or false. Default to false.
APP_DISABLE_SIGNUP=true
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ UNRELEASED CHANGES:

* Fix called_at field in the Call object returned by the API
* Add Linkedin URL in the Contact object returned by the API
* Improve localization: add plural forms, localize every needed messages

RELEASED VERSIONS:

Expand Down
70 changes: 34 additions & 36 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace App\Helpers;

use Auth;
use Carbon\Carbon;
use Jenssegers\Date\Date;

class DateHelper
{
/**
* Set the locale of the instance for Date frameworks.
*
* @param string
* @return string
*/
public static function setLocale($locale)
{
$locale = $locale ?: config('app.locale');
Carbon::setLocale($locale);
Date::setLocale($locale);
}

/**
* Creates a Carbon object.
*
Expand All @@ -27,10 +39,10 @@ public static function createDateFromFormat($date, $timezone)
* @param Carbon $date
* @return string
*/
public static function getShortDate($date, $locale = null)
public static function getShortDate($date)
{
$date = new Date($date);
$locale = self::getLocale($locale);
$locale = Date::getLocale();

switch ($locale) {
case 'en':
Expand All @@ -55,10 +67,9 @@ public static function getShortDate($date, $locale = null)
* @param Carbon $date
* @return string
*/
public static function getShortMonth($date, $locale = null)
public static function getShortMonth($date)
{
$date = new Date($date);
$locale = self::getLocale($locale);
$format = 'M';

return $date->format($format);
Expand All @@ -71,10 +82,9 @@ public static function getShortMonth($date, $locale = null)
* @param Carbon $date
* @return string
*/
public static function getShortDay($date, $locale = null)
public static function getShortDay($date)
{
$date = new Date($date);
$locale = self::getLocale($locale);
$format = 'D';

return $date->format($format);
Expand All @@ -87,10 +97,10 @@ public static function getShortDay($date, $locale = null)
* @param Carbon $date
* @return string
*/
public static function getShortDateWithoutYear($date, $locale = null)
public static function getShortDateWithoutYear($date)
{
$date = new Date($date);
$locale = self::getLocale($locale);
$locale = Date::getLocale();

switch ($locale) {
case 'en':
Expand All @@ -115,10 +125,10 @@ public static function getShortDateWithoutYear($date, $locale = null)
* @param Carbon $date
* @return string
*/
public static function getShortDateWithTime($date, $locale = null)
public static function getShortDateWithTime($date)
{
$date = new Date($date);
$locale = self::getLocale($locale);
$locale = Date::getLocale();

switch ($locale) {
case 'en':
Expand All @@ -135,25 +145,6 @@ public static function getShortDateWithTime($date, $locale = null)
return $date->format($format);
}

/**
* Returns the locale of the instance, if defined. English by default.
*
* @param string
* @return string
*/
public static function getLocale($locale = null)
{
if (Auth::check()) {
$locale = $locale ?: Auth::user()->locale;
} else {
$locale = $locale ?: 'en';
}

Date::setLocale($locale);

return $locale;
}

/**
* Add a given number of week/month/year to a date.
* @param Carbon $date the start date
Expand Down Expand Up @@ -185,10 +176,18 @@ public static function addTimeAccordingToFrequencyType(Carbon $date, $frequency,
*/
public static function getMonthAndYear(int $month)
{
$month = Carbon::now()->addMonthsNoOverflow($month)->format('M');
$year = Carbon::now()->addMonthsNoOverflow($month)->format('Y');
$date = Date::now()->addMonthsNoOverflow($month);
$locale = Date::getLocale();

return $month.' '.$year;
switch ($locale) {
case 'en':
$format = 'M Y';
break;
default:
$format = 'M Y';
}

return $date->format($format);
}

/**
Expand All @@ -202,10 +201,10 @@ public static function getMonthAndYear(int $month)
public static function getNextTheoriticalBillingDate(String $interval)
{
if ($interval == 'monthly') {
return Carbon::now()->addMonth();
return Date::now()->addMonth();
}

return Carbon::now()->addYear();
return Date::now()->addYear();
}

/**
Expand All @@ -215,7 +214,6 @@ public static function getNextTheoriticalBillingDate(String $interval)
*/
public static function getListOfMonths()
{
Date::setLocale(auth()->user()->locale);
$months = collect([]);
$currentDate = Date::now();
$currentDate->day = 1;
Expand Down
53 changes: 53 additions & 0 deletions app/Helpers/LocaleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Helpers;

use Auth;

class LocaleHelper
{
/**
* Get the current or default locale.
*
* @return string
*/
public static function getLocale()
{
if (Auth::check()) {
$locale = Auth::user()->locale;
} else {
$locale = app('language.detector')->detect() ?: config('app.locale');
}

return $locale;
}

/**
* Get the current or default locale.
*
* @return string
*/
public static function getDirection()
{
$locale = self::getLocale();

switch ($locale) {
// Source: https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code
case 'ar':
case 'arc':
case 'dv':
case 'fa':
case 'ha':
case 'he':
case 'khw':
case 'ks':
case 'ku':
case 'ps':
case 'ur':
case 'yi':
return 'rtl';
default:
return 'ltr';
}
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/Contacts/GiftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function get(Contact $contact)
'is_an_idea' => $gift->is_an_idea,
'has_been_offered' => $gift->has_been_offered,
'has_been_received' => $gift->has_been_received,
'offered_at' => \App\Helpers\DateHelper::getShortDate($gift->offered_at, auth()->user()->locale),
'received_at' => \App\Helpers\DateHelper::getShortDate($gift->received_at, auth()->user()->locale),
'created_at' => \App\Helpers\DateHelper::getShortDate($gift->created_at, auth()->user()->locale),
'offered_at' => \App\Helpers\DateHelper::getShortDate($gift->offered_at),
'received_at' => \App\Helpers\DateHelper::getShortDate($gift->received_at),
'created_at' => \App\Helpers\DateHelper::getShortDate($gift->created_at),
'edit' => false,
'show_comment' => false,
];
Expand Down
27 changes: 10 additions & 17 deletions app/Http/Controllers/Settings/SubscriptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,17 @@ public function index()
}

if (! auth()->user()->account->isSubscribed()) {
$data = [
return view('settings.subscriptions.blank', [
'numberOfCustomers' => InstanceHelper::getNumberOfPaidSubscribers(),
];

return view('settings.subscriptions.blank', $data);
]);
}

$planId = auth()->user()->account->getSubscribedPlanId();

$data = [
return view('settings.subscriptions.account', [
'planInformation' => InstanceHelper::getPlanInformationFromConfig($planId),
'nextBillingDate' => auth()->user()->account->getNextBillingDate(),
];

return view('settings.subscriptions.account', $data);
]);
}

/**
Expand All @@ -49,19 +45,16 @@ public function upgrade(Request $request)
return redirect('settings/');
}

$account = auth()->user()->account;
if (auth()->user()->account->isSubscribed()) {
return redirect('/settings/subscriptions');
}

$plan = $request->query('plan');

$data = [
return view('settings.subscriptions.upgrade', [
'planInformation' => InstanceHelper::getPlanInformationFromConfig($plan),
'nextTheoriticalBillingDate' => DateHelper::getShortDate(DateHelper::getNextTheoriticalBillingDate($plan)),
];

if ($account->isSubscribed()) {
return redirect('/settings/subscriptions');
}

return view('settings.subscriptions.upgrade', $data);
]);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,23 @@ class SettingsController extends Controller
public function index()
{
return view('settings.index')
->withLocales($this->getLocaleList())
->withHours(\App\Helpers\DateHelper::getListOfHours());
}

private function getLocaleList()
{
$locales = collect([]);
foreach (config('lang-detector.languages') as $lang) {
$locales->push([
'lang' => $lang,
'name' => trans('settings.locale_'.$lang),
]);
}

return $locales->sortBy('name');
}

/**
* Save user settings.
*
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Middleware/CheckLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Http\Middleware;

use Auth;
use Closure;
use Carbon\Carbon;
use App\Helpers\DateHelper;
use App\Helpers\LocaleHelper;

class CheckLocale
{
Expand All @@ -17,10 +17,10 @@ class CheckLocale
*/
public function handle($request, Closure $next)
{
if (Auth::check()) {
\App::setLocale(Auth::user()->locale);
Carbon::setLocale(config('app.locale'));
}
$locale = LocaleHelper::getLocale();

\App::setLocale($locale);
DateHelper::setLocale($locale);

return $next($request);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public function getBody()
* @param string $locale
* @return string
*/
public function getCreatedAt($locale)
public function getCreatedAt()
{
return DateHelper::getShortDate($this->created_at, $locale);
return DateHelper::getShortDate($this->created_at);
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static function createDefault($account_id, $first_name, $last_name, $emai
$user->password = bcrypt($password);
$user->timezone = config('app.timezone');
$user->created_at = Carbon::now();
$user->locale = \App::getLocale();
$user->save();

return $user;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"predis/predis": "^1.1",
"sabre/vobject": "^4.1",
"sentry/sentry-laravel": "^0.8",
"symfony/translation": "^3.4"
"symfony/translation": "^3.4",
"vluzrmos/language-detector": "^1.0"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3",
Expand Down
Loading