r/cpp • u/friedkeenan • Mar 28 '26
Evolving a Translation System with Reflection in C++
https://friedkeenan.github.io/posts/2026/03/28/evolving-a-translation-system-with-reflection/
17
Upvotes
r/cpp • u/friedkeenan • Mar 28 '26
6
u/JVApen Clever is an insult, not a compliment. - T. Winters Mar 28 '26
Looks pretty nice!
I might be stuck in the past, though I am worried about putting all translations inside the C++ code. I'm wondering if this can somehow integrate external files that have the translations, using #embed or even loading it at runtime.
1
u/Miserable_Guess_1266 Mar 28 '26
Very interesting read, thank you!
What occurred to me is that the defaulting problem is largely solvable without reflection. Even with the present day solution, you could introduce a wrapper struct that contains the format_string as a member and has no default constructor. IE:
struct Translation { std::format_string<...> value; Translation(std::format_string<...> v):value(v){} };
So translations that don't have a default value in the `translation_set` _must_ be initialized by the user. That doesn't get us to "defaults must be explicitly specified", but it gets us to "defaults can be omitted, but forgotten non-defaulted fields cause errors".
I took this idea and ran with it a little further to get to the same user-facing API as your solution: https://godbolt.org/z/Wzh1jWEcT
The gist is: instead of defining a separate input_t, I'm storing a wrapper type in strings_t that holds a variant. It probably yields worse codegen when looking up a translation string, because the variant might contain a reference to look up another language. But in my opinion the code is a little simpler. Tradeoffs, as always :)