Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Last active January 21, 2016 04:45
Show Gist options
  • Save egorvinogradov/90ce9dcbce92819633d7 to your computer and use it in GitHub Desktop.
Save egorvinogradov/90ce9dcbce92819633d7 to your computer and use it in GitHub Desktop.
Available Courses
<?php
// Inside <div class="entry-content">
// docs/wp-content/themes/resonar/content-single.php
$locale_data = localize_school_page(get_post_custom($ID), ICL_LANGUAGE_CODE);
echo "<h3>" . $locale_data['labels']['select_program_and_dates'] . '</h3>';
foreach ($locale_data['programs'] as $program) {
echo "<b>" . $program['name'] . "</b><br>";
echo $program['description'] . '<br>';
echo $locale_data['labels']['cost'] . ' ' . $locale_data['labels']['from'] . ' $' . $program['minimal_cost_per_week'] . ' ' . $locale_data['labels']['a_week'] . '<br><br>';
if ($program['is_flexible']) {
echo '<i>' . $locale_data['labels']['flexible_program_title'] . '</i><br>';
echo '<i>' . $locale_data['labels']['flexible_program_caption'] . '</i><br>';
echo $locale_data['labels']['duration'] . ': ' . $locale_data['labels']['from'] . ' ' . $program['minimal_duration_in_weeks'] . '<br><br>';
}
else {
foreach ($program['options'] as $option) {
echo $locale_data['labels']['cost'] . ': $' . $option['cost'] . '<br>';
echo $locale_data['labels']['duration'] . ': ' . $option['duration_in_weeks_locale'] . ' (' . $option['duration_in_months_locale'] . ')' . '<br>';
echo $locale_data['labels']['start_date'] . ': ';
echo "<select>";
foreach ($option['start_dates'] as $start_date) {
echo "<option>" . $start_date['start_date_locale'] . "</option>";
}
echo "</select>";
echo "<br><br>";
}
}
echo "<hr>";
}
echo "<hr><hr><hr>";
foreach ($locale_data['available_features'] as $feature) {
echo "<b>" . $feature['title'] . "</b> — " . $feature['caption'] . '<br>';
}
?>
[
{
"name": "Intensive English",
"description": "Accelerated program",
"minimal_cost_per_week": 110,
"options": [
{
"cost": 440,
"duration_in_weeks": 4,
"start_dates": [
{
"date": 16,
"month": 0
},
{
"date": 26,
"month": 1
},
{
"date": 10,
"month": 3
}
]
},
{
"cost": 800,
"duration_in_weeks": 8,
"start_dates": [
{
"date": 16,
"month": 0
},
{
"date": 26,
"month": 1
},
{
"date": 10,
"month": 3
}
]
}
],
"is_flexible": false
},
{
"name": "TOEFL Preparation",
"description": "Preparation to TOEFL test",
"minimal_cost_per_week": 110,
"options": [
{
"cost": 440,
"duration_in_weeks": 1,
"start_dates": [
{
"date": 16,
"month": 0
},
{
"date": 26,
"month": 1
},
{
"date": 10,
"month": 3
}
]
},
{
"cost": 800,
"duration_in_weeks": 3,
"start_dates": [
{
"date": 16,
"month": 0
},
{
"date": 26,
"month": 1
},
{
"date": 10,
"month": 3
}
]
}
],
"is_flexible": false
},
{
"name": "Flexible program",
"description": "Program with flexible duration",
"minimal_cost_per_week": 140,
"minimal_duration_in_weeks": 21,
"options": [],
"is_flexible": true
}
]
<?php
// docs/wp-content/themes/resonar/functions.php
// ==================
// Program formatting
// ==================
function format_start_date($month, $date, $lang_code){
$months_dict = array(
'en' => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
'ru' => ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']
);
if (!$months_dict[$lang_code] || $lang_code == 'en') {
return $months_dict[$lang_code][$month] . " " . $date;
}
else {
return $date . ' ' . $months_dict[$lang_code][$month];
}
}
function get_inflected_value($number, $dict, $lang_code){
if (!$number) {
return '';
}
else if ($lang_code == 'ru') {
$cases = array (2, 0, 1, 1, 1, 2);
return $number . ' ' . $dict[$lang_code][ ($number % 100 > 4 && $number % 100 < 20) ? 2 : $cases[min($number % 10, 5)] ];
}
else {
if ($dict[$lang_code]) {
$lang_code = 'en';
}
if ($number > 1) {
return $number . ' ' . $dict[$lang_code][1];
}
else {
return $number . ' ' . $dict[$lang_code][0];
}
}
}
function get_duration_caption($number, $lang_code){
$week_dict = array(
'en' => ['week', 'weeks'],
'ru' => ['неделя', 'недели', 'недель']
);
$months_dict = array(
'en' => ['month', 'months'],
'ru' => ['месяц', 'месяца', 'месяцев']
);
return array(
'weeks' => get_inflected_value($number, $week_dict, $lang_code),
'months' => get_inflected_value(convert_weeks_into_months($number), $months_dict, $lang_code)
);
}
function convert_weeks_into_months($weeks){
$months_float = $weeks * 7 / 30.5;
$months_int = floor($months_float);
$remainder = $months_float % 1;
if ($remainder > 0.7) {
return $months_int + 1;
}
else if ($remainder > 0.4) {
return $months_int + 0.5;
}
else {
return $months_int;
}
}
function get_school_feature_by_key($key, $lang_code){
$feature_dict = array(
'en' => array(
'school-feature-toefl' => array(
'title' => 'TOEFL Preparation',
'caption' => 'TOEFL test preparation programs'
),
'school-feature-intensive' => array(
'title' => 'Intensive Programs',
'caption' => 'Intensive programs for faster English learning'
),
'school-feature-homestay' => array(
'title' => 'Homestay',
'caption' => 'Option to live in an American family during studying'
),
'school-feature-residence' => array(
'title' => 'Student Hostels',
'caption' => 'Option to live in a student hostel or apartments'
),
'school-feature-activities' => array(
'title' => 'Out of Class Activities',
'caption' => 'Organizing of out of class activities by a school (e.g. hiking, watching movies, tours etc.)'
),
'school-feature-postpay' => array(
'title' => 'Payment After Visa',
'caption' => 'All documents that are required for getting a visa are sent by a school before full tuition payment'
),
'school-feature-flexible' => array(
'title' => 'Flexible Duration',
'caption' => 'Study can be extended and paid weekly'
),
),
'ru' => array(
'school-feature-toefl' => array(
'title' => 'Подготовка к TOEFL',
'caption' => 'Подготовка к тесту по английскому языку в формате TOEFL'
),
'school-feature-intensive' => array(
'title' => 'Интенсивные программы',
'caption' => 'Наличие интенсивных программ для ускоренного изучения английского языка'
),
'school-feature-homestay' => array(
'title' => 'Проживание в семье',
'caption' => 'Опция проживания в американской семье во время обучения'
),
'school-feature-residence' => array(
'title' => 'Проживание в хостеле',
'caption' => 'Опция проживания в студенческом хостеле или резиденции'
),
'school-feature-activities' => array(
'title' => 'Организация досуга',
'caption' => 'Организация развлечений для студентов во внеучебное время (например, совместные выезды, просмотр фильмов, экскурсии и др.)'
),
'school-feature-postpay' => array(
'title' => 'Оплата после визы',
'caption' => 'Все бумаги, необходимые для получения студенческой визы, высылаются школой еще до полной оплаты обучения'
),
'school-feature-flexible' => array(
'title' => 'Гибкие сроки обучения',
'caption' => 'Срок обучения можно продлять и оплачивать понедельно'
),
),
);
if (!$feature_dict[$lang_code]) {
$lang_code = 'en';
}
return $feature_dict[$lang_code][$key];
}
function localize_school_page($custom_fields, $lang_code){
$default_lang = 'en';
// Programs
$programs = json_decode($custom_fields['school-programs'][0], true);
foreach ($programs as $program_index => $program) {
foreach ($program['options'] as $option_index => $option) {
//$duration_in_weeks_locale = format_week_caption($option['duration_in_weeks'], $lang_code);
$duration_locale = get_duration_caption($option['duration_in_weeks'], $lang_code);
$programs[$program_index]['options'][$option_index]['duration_in_weeks_locale'] = $duration_locale['weeks'];
$programs[$program_index]['options'][$option_index]['duration_in_months_locale'] = $duration_locale['months'];
foreach ($option['start_dates'] as $start_date_index => $start_date) {
$start_date_str = format_start_date($start_date['month'], $start_date['date'], ICL_LANGUAGE_CODE);
$programs[$program_index]['options'][$option_index]['start_dates'][$start_date_index]['start_date_locale'] = $start_date_str;
}
}
}
// Features
$feature_list = [
'school-feature-toefl',
'school-feature-intensive',
'school-feature-homestay',
'school-feature-residence',
'school-feature-activities',
'school-feature-postpay',
'school-feature-flexible'
];
$available_features = [];
foreach ($feature_list as $feature_key) {
$feature = json_decode($custom_fields[$feature_key][0], true);
if ($feature) {
array_push($available_features, get_school_feature_by_key($feature_key, $lang_code));
}
}
// Labels
$label_dict = array(
'en' => array(
'select_program_and_dates' => 'Select program and dates',
'flexible_program_title' => 'Flexible duration and dates',
'flexible_program_caption' => 'Tuituon can start at any Monday for any duration',
'cost' => 'Price',
'duration' => 'Duration',
'start_date' => 'Start date',
'from' => 'from',
'a_week' => 'a week',
'commission_message' => "We don't charge an additional fee. Our price is equal or lower than school's price.",
'commission_link_text' => 'How it works',
'commission_link_href' => '',
'rating' => 'Rating',
'book' => 'Book now',
'location' => 'Location'
),
'ru' => array(
'select_program_and_dates' => 'Выберите курс и даты начала',
'flexible_program_title' => 'Программа с гибкими сроками и датами',
'flexible_program_caption' => 'Начать обучение можно с любого понедельника. Срок обучения — любой.',
'cost' => 'Цена',
'duration' => 'Продолжительность',
'start_date' => 'Дата начала',
'from' => 'от',
'a_week' => 'в неделю',
'commission_message' => 'Мы не берем комиссию. Наша цена равна или ниже цены, предоставляемой школой.',
'commission_link_text' => 'Как это работает?',
'commission_link_href' => '',
'rating' => 'Рейтинг',
'book' => 'Забронировать',
'location' => 'Расположение'
)
);
if ($label_dict[$lang_code]) {
$labels = $label_dict[$lang_code];
}
else {
$labels = $label_dict[$default_lang];
}
return array(
'programs' => $programs,
'available_features' => $available_features,
'labels' => $labels
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment