New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support returning the language a translation resolves to #16
Comments
Hey, thanks~! In your example, The reason for a "default" at all is that you're able to change the desired language per- // Change default language key
i18n.locale('pt');
// Retrieve translations w/ new defaults
i18n.t('intro.text', data); //=> 'Espero que você ache isso útil.'
i18n.t('intro.text', data, 'en'); //=> 'I hope you find this useful.' Rosetta does not auto-detect your users' languages for you. This is up to you and your application and should be tracked within your own application state. There are some recipes included in #1 (comment) for detecting/getting a user language. Separately, rosetta offers a Hope that all helps~ |
Hi @lukeed! Many thanks for your reply. I'm fully aware of how
Yes, that's obvious from reading the modules logic. The real issue here is that there's no built-in way of knowing that programatically. A consumer of the
Exactly. This is demonstrated in the example from my OT. And the reason why I think
My concerns have little to do with auto-detecting a language. In my snippets I'm providing a known, hardcoded language. The problem still persists.
This brings us closer to filling in the gap, but unfortunately that's not an actual solution as I'm sure you'd agree. Could you please consider re-opening this? I know you didn't ask for it, but as a humble piece of feedback to you, it might be better if you could wait for users responses before deciding whether or not an issue should be closed :) |
If I understood correctly, you’re asking for this: i18n.t(“foo”, data);
//~> { text: string | void, lang: string } which would also mean this: i18n.t(“foo”, data, “en”);
//~> { text: “bar”, lang: “en” } I’m sorry but this is pointless. You pass in a lang target directly, so echoing the value is of no use when the pooint of the library is to store a dictionary of translations so that you can get them. Accessors must always be known. And the API is programmatic. And because it’s straightforward it’s easily composable. For example, this little utility does exactly as the above without inducing an unnecessary breaking change: function t(ctx, token, data, lang) {
lang = lang || ctx.locale();
let value = ctx.t(token, data, lang);
return { lang, value };
} |
I think you might still be missing the point, given the examples you're providing. Let me elaborate a little bit.
Doesn't have to be that specific API, but sure. That's kind of in the ballpark of what I think we are missing, yes.
This is a matter of personal preference, clearly - but I disagree. It's not only not pointless, it's kind of essential to be able to provide accurate results in case of missing locales.
You would not be echoing the value. That's the whole point of my request. You would be answering with the actual language being returned from the translation.
Not entirely sure what you meant by this - but I'm gonna put my finger on not having implied otherwise. I said that clients have no programmatic way of knowing the locale that the resulting texts are in. Never mentioned the whole API.
It doesn't. And this is the reason why I'm inclined to say I'm doing a bad job at getting through with my questions. function t(ctx, token, data, lang) {
lang = lang || ctx.locale(); // <--- This will always return the value of `lang` unless falsy (useless)
let value = ctx.t(token, data, lang); // <--- Still no way of knowing which language `value` is in
return { lang, value } //<--- If the passed in `lang` is not among the translations, this result will be wrong
} Does that make things clearer? Going back to my original example, your utility would produce the following: const i18n = rosetta({ en: {...}, es: {...} })
i18n.locale('en')
// `value` would be either in no language at all (`''`) or EN,
// but `lang` would always be returned as `it`, which is wrong in both cases
const { lang, value } = t(i18n, 'foo.bar', {}, 'it') |
Sorry but I don't think you understand how t(key, params, lang) {
var val = dlv(tree[lang || locale], key, '');
if (typeof val === 'function') return val(params);
if (typeof val === 'string') return tmpl(val, params);
return val;
} If you pass a In your final snippet, the returned The utility provided does/will do what you've described. If not, then I'm sorry but your expectations are wrong. The |
To start with: great little library 👏🏼.
One minor thing I would love to see is a way of knowing which language your returned translation is actually in. This is very useful in case of missing translations.
Another way of looking at this is exposing whether or not
rosetta
managed to find a matching property in the dictionary.One common use case for this is setting a correct
<html language="{{lang}}">
attribute value in an HTML template.The text was updated successfully, but these errors were encountered: