/** * Run the database seeds. * * @return void */ public function run() { DB::table('currencies')->delete(); $currencies = json_decode(file_get_contents('https://openexchangerates.org/api/currencies.json?app_id=74e989a64ee04c2283c3f3a7ca631d1d')); $commonCurrencies = array_keys(PunicCurrency::getAllCurrencies(false, false, 'en')); $locales = Config::get('translatable.locales'); foreach ($currencies as $code => $name) { $currency = ['code' => $code, 'is_used' => in_array($code, $commonCurrencies)]; foreach ($locales as $key => $locale) { if (is_array($locale)) { $currency[$key] = ['name' => PunicCurrency::getName($code, null, $key)]; foreach ($locale as $countryLocale) { if (($name = PunicCurrency::getName($code, null, $key . '-' . $countryLocale)) != $currency[$key]['name']) { $currency[$key . '-' . $countryLocale] = ['name' => $name]; } } } else { $currencyName = PunicCurrency::getName($code, null, $locale); if ($currencyName || $locale == 'en') { $currency[$locale] = ['name' => $currencyName ?: $name]; } } } Currency::create($currency); } $extras = ['GGP' => '根西岛镑', 'JEP' => '泽西岛镑', 'IMP' => '马恩岛镑', 'BTC' => '比特币']; foreach ($extras as $code => $name) { if ($currency = Currency::whereCode($code)->first()) { $currency->fill(['zh-Hans' => ['name' => $name]]); $currency->save(); } } }
public function languageAndCurrencyByCountry($id) { $country = Country::find($id); if ($country) { $code = $country->code; $languages = PunicTerritory::getLanguages($code, 'of', true); $languages = array_map(function ($value) { return str_replace('_', '-', $value); }, $languages); $allLanguages = Language::lists('code')->toArray(); $languages = array_intersect(array_merge((array) $languages, ['en']), $allLanguages); $language = head($languages); $allCurrencies = Currency::lists('code')->toArray(); $currencies = PunicCurrency::getCurrencyForTerritory($code); $currencies = array_intersect(array_merge((array) $currencies, ['GBP']), $allCurrencies); $currency = head($currencies); $language = Language::whereCode($language)->first(); $currency = Currency::whereCode($currency)->first(); return ['language_id' => $language->id, 'currency_id' => $currency->id]; } else { return response('error', 404); } }