// translate printf message ids before substituting values, as util.h's _() does. module engine import strings #include fn C.snprintf(&char, uint, &char, ...voidptr) i32 type MessageValue = i32 | i64 | int | string | u32 | u64 struct MessageArgument { text string signed i64 unsigned u64 is_string bool } fn message_argument(value MessageValue) MessageArgument { return match value { string { MessageArgument{ text: c_string_prefix(value), is_string: true } } i32 { MessageArgument{ signed: i64(value), unsigned: u64(value) } } i64 { MessageArgument{ signed: value, unsigned: u64(value) } } int { MessageArgument{ signed: i64(value), unsigned: u64(value) } } u32 { MessageArgument{ signed: i64(value), unsigned: u64(value) } } u64 { MessageArgument{ signed: i64(value), unsigned: value } } } } fn write_message_argument(output &char, size uint, format string, argument MessageArgument, conversion u8, wide bool) i32 { fmt := unsafe { &char(format.str) } return match conversion { `s` { C.snprintf(output, size, fmt, argument.text.str) } `d`, `i` { if wide { C.snprintf(output, size, fmt, argument.signed) } else { C.snprintf(output, size, fmt, i32(argument.signed)) } } else { if wide { C.snprintf(output, size, fmt, argument.unsigned) } else { C.snprintf(output, size, fmt, u32(argument.unsigned)) } } } } fn format_message_argument(format string, argument MessageArgument, conversion u8, wide bool) ?string { if (conversion == `s`) != argument.is_string { return none } length := write_message_argument(unsafe { nil }, 0, format, argument, conversion, wide) if length < 0 { return none } buffer := unsafe { &char(C.malloc(uint(length) + 1)) } if buffer == unsafe { nil } { return none } defer { C.free(buffer) } if write_message_argument(buffer, uint(length) + 1, format, argument, conversion, wide) != length { return none } return unsafe { tos_clone(buffer) } } fn render_localized_message(format string, arguments []MessageArgument) ?string { mut output := strings.new_builder(format.len) defer { unsafe { output.free() } } mut position := 0 mut next_argument := 0 mut ordering := 0 for position < format.len { if format[position] != `%` { output.write_u8(format[position]) position++ continue } position++ if position >= format.len { return none } if format[position] == `%` { output.write_u8(`%`) position++ continue } start := position mut digits_end := position for digits_end < format.len && format[digits_end].is_digit() { digits_end++ } mut argument_index := next_argument if digits_end > start && digits_end < format.len && format[digits_end] == `$` { if ordering == 1 { return none } ordering = 2 index := format[start..digits_end].parse_uint(10, 32) or { return none } if index == 0 || index > u64(arguments.len) { return none } argument_index = int(index) - 1 position = digits_end + 1 } else { if ordering == 2 { return none } ordering = 1 next_argument++ } if argument_index < 0 || argument_index >= arguments.len { return none } field_start := position for position < format.len && format[position] in [`-`, `+`, ` `, `#`, `0`, `'`] { position++ } for position < format.len && format[position].is_digit() { position++ } if position < format.len && format[position] == `.` { position++ for position < format.len && format[position].is_digit() { position++ } } length_start := position if position < format.len && format[position] in [`h`, `l`, `j`, `z`, `t`] { position++ if position < format.len && format[position] in [`h`, `l`] && format[position] == format[length_start] { position++ } } if position >= format.len { return none } length := format[length_start..position] conversion := format[position] if conversion !in [`s`, `d`, `i`, `u`, `o`, `x`, `X`] { return none } if conversion == `s` && length != '' { return none } wide := length in ['l', 'll', 'j', 'z', 't'] position++ token := '%' + format[field_start..position] value := format_message_argument(token, arguments[argument_index], conversion, wide) or { return none } output.write_string(value) } return output.str() } fn localized_message(message string, values ...MessageValue) string { items := values.map(message_argument(it)) translation := translated_message(message) if rendered := render_localized_message(translation, items) { return rendered } // a malformed catalog must not feed an unchecked format to a variadic call. return render_localized_message(message, items) or { message } }