Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Last active October 18, 2023 01:27
Show Gist options
  • Save pjaudiomv/4583ddf0e6e22ca6e74e9390945bf8a6 to your computer and use it in GitHub Desktop.
Save pjaudiomv/4583ddf0e6e22ca6e74e9390945bf8a6 to your computer and use it in GitHub Desktop.
Language Names with Days of Week
<?php
function getLangInfo()
{
$langs = ['da', 'de', 'en', 'es', 'fa', 'fr', 'it', 'pl', 'pt', 'ru', 'sv'];
$ret = [];
foreach ($langs as $lang) {
$daysOfWeek = [];
$lang_name = \Locale::getDisplayLanguage($lang, $lang);
$lang_name_en = \Locale::getDisplayLanguage($lang, 'en');
if (strlen($lang_name) < 3) {
// If locale is invalid getDisplayLanguage just returns the provided locale
// So this is how we handle errors as any language should have more than two chars
// Skip language and move to the next one.
continue;
}
// Capitalize the first letter of the language name
$firstChar = mb_substr($lang_name, 0, 1, "utf-8");
$then = mb_substr($lang_name, 1, null, "utf-8");
$lang_name = mb_strtoupper($firstChar, "utf-8") . $then;
// Populate the language data
$ret[$lang]['name'] = $lang_name;
$ret[$lang]['code'] = $lang;
$ret[$lang]['en_name'] = $lang_name_en;
// Populate the days of the week
for ($i = 0; $i < 7; $i++) {
$dateTime = new \DateTime("Sunday +{$i} days");
$day = ucfirst(\IntlDateFormatter::formatObject($dateTime, 'cccc', $lang));
$daysOfWeek[] = $day;
}
$ret[$lang]['days_of_week'] = $daysOfWeek;
}
return $ret;
}
echo json_encode(getLangInfo());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment