Skip to content

Commit

Permalink
Add locale support
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoRSeifert committed Sep 4, 2024
1 parent dd47278 commit 5d566af
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
47 changes: 45 additions & 2 deletions parser/mpFuncCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,40 @@ MUP_NAMESPACE_START
throw ParserError(err);
}

string_type localized_weekday(int week_day, const ptr_val_type *a_pArg) {
string_type locale = a_pArg[1]->GetString();
string_type ret;
string_type en[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string_type nb[7] = {"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"};
string_type pt[7] = {"Domingo", "Segunda-Feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"};
string_type es[7] = {"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado"};
string_type fr[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
string_type de[7] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
string_type zh[7] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
string_type th[7] = {"วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"};

if(locale == "en") {
ret = en[week_day];
} else if(locale == "nb") {
ret = nb[week_day];
} else if(locale == "pt-BR") {
ret = pt[week_day];
} else if(locale == "es-ES") {
ret = es[week_day];
} else if(locale == "fr-FR") {
ret = fr[week_day];
} else if(locale == "de-DE") {
ret = de[week_day];
} else if(locale == "zh-CN") {
ret = zh[week_day];
} else if(locale == "th-TH") {
ret = th[week_day];
} else {
raise_error(ecUKNOWN_LOCALE, 2, a_pArg);
}
return ret;
}

//------------------------------------------------------------------------------
//
// class FunDaysDiff
Expand Down Expand Up @@ -972,7 +1006,7 @@ MUP_NAMESPACE_START
{
if (a_iArgc < 1) {
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent()));
} else if (a_iArgc > 1) {
} else if (a_iArgc > 2) {
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent()));
}

Expand All @@ -983,9 +1017,18 @@ MUP_NAMESPACE_START
raise_error(ecINVALID_DATETIME_FORMAT, 1, a_pArg);
}

bool has_locale = false;
if (a_iArgc == 2) {
has_locale = true;
}

int week_day = date.tm_wday;

*ret = week_day;
if(has_locale) {
*ret = localized_weekday(week_day, a_pArg);
} else {
*ret = week_day;
}
}

////------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions parser/mpParserMessageProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ MUP_NAMESPACE_START
m_vErrMsg[ecADD_HOURS_DATE] = _T("The first parameter could not be converted to a date. Please use the format: \"yyyy-mm-dd\"");
m_vErrMsg[ecADD_HOURS_DATETIME] = _T("The first parameter could not be converted to a date time. Please use the format: \"yyyy-mm-ddTHH:MM\"");
m_vErrMsg[ecINVALID_TYPES_MATCH] = _T("Both values of the default(x, y) function should have the same type");
m_vErrMsg[ecUKNOWN_LOCALE] = _T("The chosen locale is not supported");
}

#if defined(_UNICODE)
Expand Down
3 changes: 2 additions & 1 deletion parser/mpTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ enum EErrorCodes
ecADD_HOURS_DATETIME = 57, ///< Invalid date time format for add_hours() first parameter

ecINVALID_TYPES_MATCH = 58,
ecUKNOWN_LOCALE = 59, ///< The chosen locale is not supported

// time related errors
ecINVALID_TIME_FORMAT = 59, ///< Invalid time format
ecINVALID_TIME_FORMAT = 60, ///< Invalid time format

// The last two are special entries
ecCOUNT, ///< This is no error code, It just stores the total number of error codes
Expand Down
1 change: 1 addition & 0 deletions value_test/mpError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ MUP_NAMESPACE_START
m_vErrMsg[ecADD_HOURS_DATE] = _T("The first parameter could not be converted to a date. Please use the format: \"yyyy-mm-dd\"");
m_vErrMsg[ecADD_HOURS_DATETIME] = _T("The first parameter could not be converted to a date time. Please use the format: \"yyyy-mm-ddTHH:MM\"");
m_vErrMsg[ecINVALID_TYPES_MATCH] = _T("Both values of the default(x, y) function should have the same type");
m_vErrMsg[ecUKNOWN_LOCALE] = _T("The chosen locale is not supported");

#if defined(_DEBUG)
for (int i=0; i<ecCOUNT; ++i)
Expand Down
3 changes: 2 additions & 1 deletion value_test/mpError.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ MUP_NAMESPACE_START
ecADD_HOURS_DATETIME = 57, ///< Invalid date time format for add_hours() first parameter

ecINVALID_TYPES_MATCH = 58, ///< Both values of the default(x, y) function should have the same type
ecUKNOWN_LOCALE = 59, ///< The chosen locale is not supported

// The last two are special entries
// The last two are special entries
ecCOUNT, ///< This is no error code, It just stores just the total number of error codes
ecUNDEFINED = -1, ///< Undefined message, placeholder to detect unassigned error messages
};
Expand Down

0 comments on commit 5d566af

Please sign in to comment.