39 #include "table/strings.h"
61 assert(this->
type !=
nullptr);
73 Debug(misc, 0,
"Trying to read invalid string parameter");
76 if (this->type !=
nullptr) {
77 if (this->type[this->
offset] != 0 && this->type[this->
offset] != type) {
78 Debug(misc, 0,
"Trying to read string parameter with wrong type");
97 while (max_value >= 10) {
115 uint64 val = count > 1 ? front : next;
116 for (; count > 1; count--) {
117 val = 10 * val + next;
158 for (
int i = 0; i < num; i++) {
160 strings[i] =
stredup((
const char *)(
size_t)_global_string_params.GetParam(i));
161 dst[i] = (size_t)strings[i];
163 strings[i] =
nullptr;
168 static char *StationGetSpecialString(
char *buff,
int x,
const char *last);
169 static char *GetSpecialTownNameString(
char *buff,
int ind, uint32 seed,
const char *last);
170 static char *GetSpecialNameString(
char *buff,
int ind,
StringParameters *args,
const char *last);
172 static char *
FormatString(
char *buff,
const char *str,
StringParameters *args,
const char *last, uint case_index = 0,
bool game_script =
false,
bool dry_run =
false);
182 delete[]
reinterpret_cast<char*
>(langpack);
187 std::unique_ptr<LanguagePack, LanguagePackDeleter> langpack;
189 std::vector<char *> offsets;
200 const char *GetStringPtr(
StringID string)
205 case TEXT_TAB_OLD_NEWGRF: NOT_REACHED();
230 if (index >= 0xC0 && !game_script) {
231 return GetSpecialTownNameString(buffr, index - 0xC0, args->
GetInt32(), last);
235 case TEXT_TAB_SPECIAL:
236 if (index >= 0xE4 && !game_script) {
237 return GetSpecialNameString(buffr, index - 0xE4, args, last);
241 case TEXT_TAB_OLD_CUSTOM:
244 error(
"Incorrect conversion of custom name string.");
251 case TEXT_TAB_OLD_NEWGRF:
265 error(
"String 0x%X is invalid. You are probably using an old version of the .lng file.\n",
string);
268 return FormatString(buffr, GetStringPtr(
string), args, last, case_index);
274 _global_string_params.
offset = 0;
323 static char *
FormatNumber(
char *buff, int64 number,
const char *last,
const char *separator,
int zerofill = 1,
int fractional_digits = 0)
325 static const int max_digits = 20;
326 uint64 divisor = 10000000000000000000ULL;
327 zerofill += fractional_digits;
328 int thousands_offset = (max_digits - fractional_digits - 1) % 3;
337 for (
int i = 0; i < max_digits; i++) {
338 if (i == max_digits - fractional_digits) {
340 if (
StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
341 buff +=
seprintf(buff, last,
"%s", decimal_separator);
345 if (num >= divisor) {
346 quot = num / divisor;
349 if ((tot |= quot) || i >= max_digits - zerofill) {
350 buff +=
seprintf(buff, last,
"%i", (
int)quot);
351 if ((i % 3) == thousands_offset && i < max_digits - 1 - fractional_digits) buff =
strecpy(buff, separator, last);
362 static char *FormatCommaNumber(
char *buff, int64 number,
const char *last,
int fractional_digits = 0)
365 if (
StrEmpty(separator)) separator = _langpack.langpack->digit_group_separator;
366 return FormatNumber(buff, number, last, separator, 1, fractional_digits);
369 static char *FormatNoCommaNumber(
char *buff, int64 number,
const char *last)
374 static char *FormatZerofillNumber(
char *buff, int64 number, int64 count,
const char *last)
379 static char *FormatHexNumber(
char *buff, uint64 number,
const char *last)
381 return buff +
seprintf(buff, last,
"0x" OTTD_PRINTFHEX64, number);
391 static char *
FormatBytes(
char *buff, int64 number,
const char *last)
396 const char *
const iec_prefixes[] = {
"",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei"};
398 while (number >= 1024 * 1024) {
404 if (
StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
408 buff +=
seprintf(buff, last,
"%i", (
int)number);
409 }
else if (number < 1024 * 10) {
410 buff +=
seprintf(buff, last,
"%i%s%02i", (
int)number / 1024, decimal_separator, (
int)(number % 1024) * 100 / 1024);
411 }
else if (number < 1024 * 100) {
412 buff +=
seprintf(buff, last,
"%i%s%01i", (
int)number / 1024, decimal_separator, (
int)(number % 1024) * 10 / 1024);
414 assert(number < 1024 * 1024);
415 buff +=
seprintf(buff, last,
"%i", (
int)number / 1024);
418 assert(
id <
lengthof(iec_prefixes));
419 buff +=
seprintf(buff, last,
NBSP "%sB", iec_prefixes[
id]);
424 static char *FormatYmdString(
char *buff,
Date date,
const char *last, uint case_index)
429 int64 args[] = {ymd.
day + STR_DAY_NUMBER_1ST - 1, STR_MONTH_ABBREV_JAN + ymd.
month, ymd.
year};
431 return FormatString(buff, GetStringPtr(STR_FORMAT_DATE_LONG), &tmp_params, last, case_index);
434 static char *FormatMonthAndYear(
char *buff,
Date date,
const char *last, uint case_index)
439 int64 args[] = {STR_MONTH_JAN + ymd.
month, ymd.
year};
441 return FormatString(buff, GetStringPtr(STR_FORMAT_DATE_SHORT), &tmp_params, last, case_index);
444 static char *FormatTinyOrISODate(
char *buff,
Date date,
StringID str,
const char *last)
455 int64 args[] = {(int64)(
size_t)day, (int64)(
size_t)month, ymd.
year};
457 return FormatString(buff, GetStringPtr(str), &tmp_params, last);
460 static char *FormatGenericCurrency(
char *buff,
const CurrencySpec *spec,
Money number,
bool compact,
const char *last)
464 bool negative = number < 0;
465 const char *multiplier =
"";
467 number *= spec->
rate;
471 if (buff +
Utf8CharLen(SCC_PUSH_COLOUR) > last)
return buff;
473 if (buff +
Utf8CharLen(SCC_RED) > last)
return buff;
475 buff =
strecpy(buff,
"-", last);
488 if (number >= 1000000000 - 500) {
489 number = (number + 500000) / 1000000;
490 multiplier =
NBSP "M";
491 }
else if (number >= 1000000) {
492 number = (number + 500) / 1000;
493 multiplier =
NBSP "k";
498 if (
StrEmpty(separator)) separator = _currency->separator.c_str();
499 if (
StrEmpty(separator)) separator = _langpack.langpack->digit_group_separator_currency;
501 buff =
strecpy(buff, multiplier, last);
509 if (buff +
Utf8CharLen(SCC_POP_COLOUR) > last)
return buff;
526 uint64 n =
abs(count);
528 switch (plural_form) {
537 return n != 1 ? 1 : 0;
549 return n > 1 ? 1 : 0;
556 return n % 10 == 1 && n % 100 != 11 ? 0 : n != 0 ? 1 : 2;
562 return n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4;
568 return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
574 return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
580 return n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
586 return n % 100 == 1 ? 0 : n % 100 == 2 ? 1 : n % 100 == 3 || n % 100 == 4 ? 2 : 3;
592 return n % 10 == 1 && n % 100 != 11 ? 0 : 1;
598 return n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2;
629 return (n == 1 ? 0 : n == 0 || (n % 100 > 1 && n % 100 < 11) ? 1 : (n % 100 > 10 && n % 100 < 20) ? 2 : 3);
634 return ((n == 1 || n == 11) ? 0 : (n == 2 || n == 12) ? 1 : ((n > 2 && n < 11) || (n > 12 && n < 20)) ? 2 : 3);
640 return n == 1 ? 0 : (n == 0 || (n % 100 > 0 && n % 100 < 20)) ? 1 : 2;
644 static const char *ParseStringChoice(
const char *b, uint form,
char **dst,
const char *last)
648 uint pos, i, mypos = 0;
650 for (i = pos = 0; i != n; i++) {
651 uint len = (byte)*b++;
652 if (i == form) mypos = pos;
656 *dst +=
seprintf(*dst, last,
"%s", b + mypos);
673 return ((input * this->multiplier) + (round && this->shift != 0 ? 1 << (this->shift - 1) : 0)) >> this->
shift;
683 int64
FromDisplay(int64 input,
bool round =
true, int64 divider = 1)
const
685 return ((input << this->shift) + (round ? (this->multiplier * divider) - 1 : 0)) / (this->multiplier * divider);
705 { { 1, 0}, STR_UNITS_VELOCITY_IMPERIAL, 0 },
706 { { 103, 6}, STR_UNITS_VELOCITY_METRIC, 0 },
707 { { 1831, 12}, STR_UNITS_VELOCITY_SI, 0 },
708 { {37888, 16}, STR_UNITS_VELOCITY_GAMEUNITS, 1 },
713 { { 1, 0}, STR_UNITS_POWER_IMPERIAL, 0 },
714 { {4153, 12}, STR_UNITS_POWER_METRIC, 0 },
715 { {6109, 13}, STR_UNITS_POWER_SI, 0 },
720 { {4515, 12}, STR_UNITS_WEIGHT_SHORT_IMPERIAL, STR_UNITS_WEIGHT_LONG_IMPERIAL },
721 { { 1, 0}, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC },
722 { {1000, 0}, STR_UNITS_WEIGHT_SHORT_SI, STR_UNITS_WEIGHT_LONG_SI },
727 { {4227, 4}, STR_UNITS_VOLUME_SHORT_IMPERIAL, STR_UNITS_VOLUME_LONG_IMPERIAL },
728 { {1000, 0}, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC },
729 { { 1, 0}, STR_UNITS_VOLUME_SHORT_SI, STR_UNITS_VOLUME_LONG_SI },
734 { {3597, 4}, STR_UNITS_FORCE_IMPERIAL, 0 },
735 { {3263, 5}, STR_UNITS_FORCE_METRIC, 0 },
736 { { 1, 0}, STR_UNITS_FORCE_SI, 0 },
741 { { 3, 0}, STR_UNITS_HEIGHT_IMPERIAL, 0 },
742 { { 1, 0}, STR_UNITS_HEIGHT_METRIC, 0 },
743 { { 1, 0}, STR_UNITS_HEIGHT_SI, 0 },
798 uint orig_offset = args->
offset;
810 FormatString(buff, str_arg, args, last, case_index, game_script,
true);
813 FormatString(buff, str_arg, args, last, case_index, game_script,
true);
816 args->
offset = orig_offset;
819 uint next_substr_case_index = 0;
820 char *buf_start = buff;
821 std::stack<const char *, std::vector<const char *>> str_stack;
822 str_stack.push(str_arg);
825 while (!str_stack.empty() && (b = Utf8Consume(&str_stack.top())) ==
'\0') {
828 if (str_stack.empty())
break;
829 const char *&str = str_stack.top();
835 if (b == 0)
continue;
840 uint64 sub_args_data[20];
841 WChar sub_args_type[20];
842 bool sub_args_need_free[20];
846 memset(sub_args_need_free, 0,
sizeof(sub_args_need_free));
849 uint32 stringid = strtoul(str, &p, 16);
850 if (*p !=
':' && *p !=
'\0') {
851 while (*p !=
'\0') p++;
853 buff =
strecat(buff,
"(invalid SCC_ENCODED)", last);
857 while (*p !=
'\0') p++;
859 buff =
strecat(buff,
"(invalid StringID)", last);
864 while (*p !=
'\0' && i < 20) {
869 bool instring =
false;
876 if (*p ==
'"' && escape) {
883 instring = !instring;
890 if (*p ==
':')
break;
891 if (*p ==
'\0')
break;
898 bool lookup = (l == SCC_ENCODED);
899 if (lookup) s += len;
901 param = strtoull(s, &p, 16);
905 while (*p !=
'\0') p++;
907 buff =
strecat(buff,
"(invalid sub-StringID)", last);
913 sub_args.SetParam(i++, param);
918 sub_args_need_free[i] =
true;
919 sub_args.SetParam(i++, (uint64)(
size_t)g);
928 for (
int i = 0; i < 20; i++) {
929 if (sub_args_need_free[i])
free((
void *)sub_args.GetParam(i));
935 StringID substr = Utf8Consume(&str);
936 str_stack.push(GetStringPtr(substr));
942 str_stack.push(GetStringPtr(substr));
943 case_index = next_substr_case_index;
944 next_substr_case_index = 0;
949 case SCC_GENDER_LIST: {
951 uint offset = orig_offset + (byte)*str++;
972 WChar c = Utf8Consume(&s);
974 if (c == SCC_GENDER_INDEX) gender = (byte)s[0];
976 str = ParseStringChoice(str, gender, &buff, last);
982 case SCC_GENDER_INDEX:
991 case SCC_PLURAL_LIST: {
992 int plural_form = *str++;
993 uint offset = orig_offset + (byte)*str++;
999 case SCC_ARG_INDEX: {
1000 args->
offset = orig_offset + (byte)*str++;
1004 case SCC_SET_CASE: {
1007 next_substr_case_index = (byte)*str++;
1011 case SCC_SWITCH_CASE: {
1014 uint num = (byte)*str++;
1016 if ((
byte)str[0] == case_index) {
1022 str += 3 + (str[1] << 8) + str[2];
1029 buff =
strecpy(buff, _openttd_revision, last);
1032 case SCC_RAW_STRING_POINTER: {
1033 if (game_script)
break;
1034 const char *str = (
const char *)(
size_t)args->
GetInt64(SCC_RAW_STRING_POINTER);
1046 buff =
GetStringWithArgs(buff, str, &tmp_params, last, next_substr_case_index, game_script);
1047 next_substr_case_index = 0;
1061 uint size = b - SCC_STRING1 + 1;
1063 buff =
strecat(buff,
"(too many parameters)", last);
1066 buff =
GetStringWithArgs(buff, str, &sub_args, last, next_substr_case_index, game_script);
1068 next_substr_case_index = 0;
1073 buff = FormatCommaNumber(buff, args->
GetInt64(SCC_COMMA), last);
1077 int64 number = args->
GetInt64(SCC_DECIMAL);
1078 int digits = args->
GetInt32(SCC_DECIMAL);
1079 buff = FormatCommaNumber(buff, number, last, digits);
1084 buff = FormatNoCommaNumber(buff, args->
GetInt64(SCC_NUM), last);
1087 case SCC_ZEROFILL_NUM: {
1089 buff = FormatZerofillNumber(buff, num, args->
GetInt64(), last);
1094 buff = FormatHexNumber(buff, (uint64)args->
GetInt64(SCC_HEX), last);
1101 case SCC_CARGO_TINY: {
1110 switch (cargo_str) {
1125 buff = FormatCommaNumber(buff, amount, last);
1129 case SCC_CARGO_SHORT: {
1137 switch (cargo_str) {
1163 case SCC_CARGO_LONG: {
1174 case SCC_CARGO_LIST: {
1175 CargoTypes cmask = args->
GetInt64(SCC_CARGO_LIST);
1179 if (!
HasBit(cmask, cs->Index()))
continue;
1181 if (buff >= last - 2)
break;
1191 buff =
GetStringWithArgs(buff, cs->name, args, last, next_substr_case_index, game_script);
1195 if (first) buff =
GetStringWithArgs(buff, STR_JUST_NOTHING, args, last, next_substr_case_index, game_script);
1198 next_substr_case_index = 0;
1201 assert(buff < last);
1205 case SCC_CURRENCY_SHORT:
1206 buff = FormatGenericCurrency(buff, _currency, args->
GetInt64(),
true, last);
1209 case SCC_CURRENCY_LONG:
1210 buff = FormatGenericCurrency(buff, _currency, args->
GetInt64(SCC_CURRENCY_LONG),
false, last);
1214 buff = FormatTinyOrISODate(buff, args->
GetInt32(SCC_DATE_TINY), STR_FORMAT_DATE_TINY, last);
1217 case SCC_DATE_SHORT:
1218 buff = FormatMonthAndYear(buff, args->
GetInt32(SCC_DATE_SHORT), last, next_substr_case_index);
1219 next_substr_case_index = 0;
1223 buff = FormatYmdString(buff, args->
GetInt32(SCC_DATE_LONG), last, next_substr_case_index);
1224 next_substr_case_index = 0;
1228 buff = FormatTinyOrISODate(buff, args->
GetInt32(), STR_FORMAT_DATE_ISO, last);
1255 case SCC_VELOCITY: {
1264 case SCC_VOLUME_SHORT: {
1272 case SCC_VOLUME_LONG: {
1280 case SCC_WEIGHT_SHORT: {
1288 case SCC_WEIGHT_LONG: {
1296 case SCC_COMPANY_NAME: {
1298 if (c ==
nullptr)
break;
1300 if (!c->
name.empty()) {
1301 int64 args_array[] = {(int64)(
size_t)c->
name.c_str()};
1305 int64 args_array[] = {c->
name_2};
1312 case SCC_COMPANY_NUM: {
1317 int64 args_array[] = {company + 1};
1324 case SCC_DEPOT_NAME: {
1327 uint64 args_array[] = {(uint64)args->
GetInt32()};
1328 WChar types_array[] = {SCC_STATION_NAME};
1330 buff =
GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_AIRCRAFT, &tmp_params, last);
1335 if (!d->name.empty()) {
1336 int64 args_array[] = {(int64)(
size_t)d->name.c_str()};
1342 buff =
GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_TRAIN + 2 * vt + (d->
town_cn == 0 ? 0 : 1), &tmp_params, last);
1347 case SCC_ENGINE_NAME: {
1349 if (e ==
nullptr)
break;
1352 int64 args_array[] = {(int64)(
size_t)e->
name.c_str()};
1362 case SCC_GROUP_NAME: {
1364 if (g ==
nullptr)
break;
1366 if (!g->
name.empty()) {
1367 int64 args_array[] = {(int64)(
size_t)g->
name.c_str()};
1371 int64 args_array[] = {g->
index};
1379 case SCC_INDUSTRY_NAME: {
1381 if (i ==
nullptr)
break;
1393 buff =
FormatString(buff, GetStringPtr(STR_FORMAT_INDUSTRY_NAME), &tmp_params, last, next_substr_case_index);
1395 next_substr_case_index = 0;
1399 case SCC_PRESIDENT_NAME: {
1401 if (c ==
nullptr)
break;
1415 case SCC_STATION_NAME: {
1416 StationID sid = args->
GetInt32(SCC_STATION_NAME);
1419 if (st ==
nullptr) {
1428 if (!st->
name.empty()) {
1429 int64 args_array[] = {(int64)(
size_t)st->
name.c_str()};
1434 if (st->
indtype != IT_INVALID) {
1446 uint64 args_array[] = {STR_TOWN_NAME, st->
town->
index, st->
index};
1447 WChar types_array[] = {0, SCC_TOWN_NAME, SCC_NUM};
1454 case SCC_TOWN_NAME: {
1456 if (t ==
nullptr)
break;
1458 if (!t->
name.empty()) {
1459 int64 args_array[] = {(int64)(
size_t)t->
name.c_str()};
1468 case SCC_WAYPOINT_NAME: {
1470 if (wp ==
nullptr)
break;
1472 if (!wp->
name.empty()) {
1473 int64 args_array[] = {(int64)(
size_t)wp->
name.c_str()};
1479 StringID str = ((wp->
string_id == STR_SV_STNAME_BUOY) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME);
1486 case SCC_VEHICLE_NAME: {
1488 if (v ==
nullptr)
break;
1490 if (!v->
name.empty()) {
1491 int64 args_array[] = {(int64)(
size_t)v->
name.c_str()};
1498 buff =
GetStringWithArgs(buff, STR_FORMAT_GROUP_VEHICLE_NAME, &tmp_params, last);
1505 default: str = STR_INVALID_VEHICLE;
break;
1506 case VEH_TRAIN: str = STR_SV_TRAIN_NAME;
break;
1507 case VEH_ROAD: str = STR_SV_ROAD_VEHICLE_NAME;
break;
1508 case VEH_SHIP: str = STR_SV_SHIP_NAME;
break;
1517 case SCC_SIGN_NAME: {
1519 if (si ==
nullptr)
break;
1521 if (!si->name.empty()) {
1522 int64 args_array[] = {(int64)(
size_t)si->name.c_str()};
1532 case SCC_STATION_FEATURES: {
1533 buff = StationGetSpecialString(buff, args->
GetInt32(SCC_STATION_FEATURES), last);
1547 static char *StationGetSpecialString(
char *buff,
int x,
const char *last)
1558 static char *GetSpecialTownNameString(
char *buff,
int ind, uint32 seed,
const char *last)
1563 static const char *
const _silly_company_names[] = {
1565 "Tiny Transport Ltd.",
1567 "Comfy-Coach & Co.",
1568 "Crush & Bump Ltd.",
1569 "Broken & Late Ltd.",
1571 "Supersonic Travel",
1573 "Lightning International",
1574 "Pannik & Loozit Ltd.",
1575 "Inter-City Transport",
1576 "Getout & Pushit Ltd."
1579 static const char *
const _surname_list[] = {
1611 static const char *
const _silly_surname_list[] = {
1626 static const char _initial_name_letters[] = {
1627 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
1628 'K',
'L',
'M',
'N',
'P',
'R',
'S',
'T',
'W',
1631 static char *GenAndCoName(
char *buff, uint32 arg,
const char *last)
1633 const char *
const *base;
1637 base = _silly_surname_list;
1638 num =
lengthof(_silly_surname_list);
1640 base = _surname_list;
1644 buff =
strecpy(buff, base[num *
GB(arg, 16, 8) >> 8], last);
1645 buff =
strecpy(buff,
" & Co.", last);
1650 static char *GenPresidentName(
char *buff, uint32 x,
const char *last)
1652 char initial[] =
"?. ";
1653 const char *
const *base;
1657 initial[0] = _initial_name_letters[
sizeof(_initial_name_letters) *
GB(x, 0, 8) >> 8];
1658 buff =
strecpy(buff, initial, last);
1660 i = (
sizeof(_initial_name_letters) + 35) *
GB(x, 8, 8) >> 8;
1661 if (i <
sizeof(_initial_name_letters)) {
1662 initial[0] = _initial_name_letters[i];
1663 buff =
strecpy(buff, initial, last);
1667 base = _silly_surname_list;
1668 num =
lengthof(_silly_surname_list);
1670 base = _surname_list;
1674 buff =
strecpy(buff, base[num *
GB(x, 16, 8) >> 8], last);
1679 static char *GetSpecialNameString(
char *buff,
int ind,
StringParameters *args,
const char *last)
1683 return strecpy(buff, _silly_company_names[std::min<uint>(args->
GetInt32() & 0xFFFF,
lengthof(_silly_company_names) - 1)], last);
1686 return GenAndCoName(buff, args->
GetInt32(), last);
1689 return GenPresidentName(buff, args->
GetInt32(), last);
1693 if (
IsInsideMM(ind - 6, 0, SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1)) {
1694 buff = GetSpecialTownNameString(buff, ind - 6, args->
GetInt32(), last);
1695 return strecpy(buff,
" Transport", last);
1708 this->
version == TO_LE32(LANGUAGE_PACK_VERSION) &&
1728 return 4 * this->
missing < LANGUAGE_TOTAL_STRINGS;
1740 std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(
reinterpret_cast<LanguagePack *
>(
ReadFileToMem(lang->
file, len, 1U << 20).release()));
1741 if (!lang_pack)
return false;
1744 const char *end = (
char *)lang_pack.get() + len + 1;
1747 if (end <= lang_pack->data || !lang_pack->IsValid()) {
1751 #if TTD_ENDIAN == TTD_BIG_ENDIAN
1753 lang_pack->offsets[i] = ReadLE16Aligned(&lang_pack->offsets[i]);
1757 std::array<uint, TEXT_TAB_END> tab_start, tab_num;
1761 uint16 num = lang_pack->offsets[i];
1764 tab_start[i] = count;
1770 std::vector<char *> offs(count);
1773 char *s = lang_pack->data;
1775 for (uint i = 0; i < count; i++) {
1776 if (s + len >= end)
return false;
1779 len = ((len & 0x3F) << 8) + (byte)*s++;
1780 if (s + len >= end)
return false;
1788 _langpack.langpack = std::move(lang_pack);
1789 _langpack.offsets = std::move(offs);
1800 extern void Win32SetCurrentLocaleName(
const char *iso_code);
1809 #ifdef WITH_ICU_I18N
1811 UErrorCode status = U_ZERO_ERROR;
1816 if (U_FAILURE(status)) {
1840 #if !(defined(_WIN32) || defined(__APPLE__))
1853 env = std::getenv(
"LANGUAGE");
1854 if (env !=
nullptr)
return env;
1856 env = std::getenv(
"LC_ALL");
1857 if (env !=
nullptr)
return env;
1859 if (param !=
nullptr) {
1860 env = std::getenv(param);
1861 if (env !=
nullptr)
return env;
1864 return std::getenv(
"LANG");
1888 if (newgrflangid == lang.newgrflangid)
return ⟨
1902 FILE *f = fopen(file,
"rb");
1903 if (f ==
nullptr)
return false;
1905 size_t read = fread(hdr,
sizeof(*hdr), 1, f);
1908 bool ret = read == 1 && hdr->
IsValid();
1925 if (dir !=
nullptr) {
1926 struct dirent *dirent;
1927 while ((dirent = readdir(dir)) !=
nullptr) {
1928 std::string d_name =
FS2OTTD(dirent->d_name);
1929 const char *extension = strrchr(d_name.c_str(),
'.');
1932 if (extension ==
nullptr || strcmp(extension,
".lng") != 0)
continue;
1939 Debug(misc, 3,
"{} is not a valid language file", lmd.
file);
1941 Debug(misc, 3,
"{}'s language ID is already known", lmd.
file);
1957 std::string path = FioGetDirectory(sp,
LANG_DIR);
1964 if (lang ==
nullptr) lang =
"en_GB";
1975 const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1;
1977 chosen_language = &lng;
1981 if (strcmp (lng.isocode,
"en_GB") == 0) en_GB_fallback = &lng;
1984 if (!lng.IsReasonablyFinished())
continue;
1986 if (strncmp(lng.isocode, lang, 5) == 0) chosen_language = &lng;
1987 if (strncmp(lng.isocode, lang, 2) == 0) language_fallback = &lng;
1992 if (chosen_language ==
nullptr) {
1993 chosen_language = (language_fallback !=
nullptr) ? language_fallback : en_GB_fallback;
2005 return _langpack.langpack->isocode;
2015 const Sprite *question_mark[FS_END];
2018 question_mark[size] =
GetGlyph(size,
'?');
2024 for (
WChar c = Utf8Consume(&text); c !=
'\0'; c = Utf8Consume(&text)) {
2025 if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
2026 size = (
FontSize)(c - SCC_FIRST_FONT);
2029 std::string size_name;
2032 case 0: size_name =
"medium";
break;
2033 case 1: size_name =
"small";
break;
2034 case 2: size_name =
"large";
break;
2035 case 3: size_name =
"mono";
break;
2036 default: NOT_REACHED();
2039 Debug(freetype, 0,
"Font is missing glyphs to display char 0x{:X} in {} font size", (
int)c, size_name);
2067 const char *ret = _langpack.offsets[_langpack.
langtab_start[this->
i] + this->
j];
2070 while (this->i < TEXT_TAB_END && this->
j >= _langpack.
langtab_num[this->i]) {
2085 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
2090 settings->small.os_handle = os_data;
2091 settings->medium.os_handle = os_data;
2092 settings->large.os_handle = os_data;
2113 if (searcher ==
nullptr) searcher = &pack_searcher;
2115 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
2124 bad_font = !
SetFallbackFont(&_freetype, _langpack.langpack->isocode, _langpack.langpack->winlangid, searcher);
2135 static char *err_str =
stredup(
"XXXThe current font is missing some of the characters used in the texts for this language. Using system fallback font instead.");
2141 if (bad_font && base_font) {
2156 static char *err_str =
stredup(
"XXXThe current font is missing some of the characters used in the texts for this language. Read the readme to see how to solve this.");
2169 #if !defined(WITH_ICU_LX) && !defined(WITH_UNISCRIBE) && !defined(WITH_COCOA)
2184 static char *err_str =
stredup(
"XXXThis version of OpenTTD does not support right-to-left languages. Recompile with icu enabled.");