diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index b4fde7c1d4e9..2b5036160309 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -58,10 +58,30 @@ class AccountRepository $account->account_key = strtolower(str_random(RANDOM_KEY_LENGTH)); $account->company_id = $company->id; - if ($locale = Session::get(SESSION_LOCALE)) { - if ($language = Language::whereLocale($locale)->first()) { - $account->language_id = $language->id; - } + // Set default language/currency based on IP + $data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $account->ip)); + $currencyCode = strtolower($data['geoplugin_currencyCode']); + $countryCode = strtolower($data['geoplugin_countryCode']); + + $currency = \Cache::get('currencies')->filter(function ($item) use ($currencyCode) { + return strtolower($item->code) == $currencyCode; + })->first(); + if ($currency) { + $account->currency_id = $currency->id; + } + + $country = \Cache::get('countries')->filter(function ($item) use ($countryCode) { + return strtolower($item->iso_3166_2) == $countryCode || strtolower($item->iso_3166_3) == $countryCode; + })->first(); + if ($country) { + $account->country_id = $country->id; + } + + $language = \Cache::get('languages')->filter(function ($item) use ($countryCode) { + return strtolower($item->locale) == $countryCode; + })->first(); + if ($language) { + $account->language_id = $language->id; } $account->save();