M
M
Mercury132019-11-07 16:28:39
C++ / C#
Mercury13, 2019-11-07 16:28:39

What is the best way to implement localization?

We have this code...

namespace loc {
  const wchar_t* get(const char* key);
}

It turned out such a thing. For languages ​​that we do not know and are forced to "hammer" with English, and the plural rules are different, it displays something like "5 day remaining" - simply because Japanese does not have these rules.
Okay, let's fix it like this.
struct TaggedCWstr
{
    const PluralRule* rule;
    const wchar_t* text;

    operator const wchar_t* () const { return text; }
    operator std::wstring () const { return text; }
};

namespace loc {
  TaggedCWstr get(const char* key);
}

And here a stumbling block arose on operator std::wstring. If I comment it, it throws an error on
std::wstring xxx() {
  return loc::get(key);    // could not convert from TaggedCWstr to wstring
}

If I do, it throws an error on
std::wstring s;
s = loc::get("key");    // ambiguous overload for op=

Is there any way to satisfy both?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2019-11-07
@Mercury13

s = loc::get("key");    // ambiguous overload for op=

you have an ambiguity because both constructors std::wstring (copy constructor and conversion constructor from const wchar_t*) are implicit.
The conversion operators TaggedCWstrare also implicit.
This ambiguity must be eliminated. Replace, for example, both conversion operators with the conversion operator to std::wstring_view.
I would not make an implicit conversion operator to std::wstringand thereby allow uncontrolled access to dynamic memory without an explicit understanding of this process. It is better to std::wstringdo for getting operator *, and even better - not to break the semantics of operators at all and make a method with a telling name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question