Localization primitive proposal #149
Replies: 7 comments 12 replies
-
Hmmm There are a couple of things I'm wondering:
<p>{ l10n.GREETING(name(), gender()) }</p>
Other than that — it's a great idea :) |
Beta Was this translation helpful? Give feedback.
-
Also, |
Beta Was this translation helpful? Give feedback.
-
This is a great suggestion! We recently had a chat in the solid-start Discord channel re i18n/l10n. I like that this proposal remains flexible, considers reactivity and seems to fit well with the future plans of Solid Start. Would love to see this proceed. |
Beta Was this translation helpful? Give feedback.
-
Very interesting. I have not seen the usage of template literal strings for the expression of plurals etc. The benefits from a dev perspective are clear. However, I fear that not all nuances of human languages can be (easily) expressed while leveraging template literal strings. Fluent syntax goes beyond pluralization. Nevertheless, neat idea.
That argument is true but will become obsolete. I am working on an entire localization ecosystem that includes an editor for translators. The editor abstracts away code syntax. Albeit a draft, here is an RFC opral/monorepo#127. One of the questions in the RFC is how different syntaxes, like the one in this proposal, can be supported. I think I found a novel solution. Summary of the RFCThe editor uses git, no sync whatsoever is required. The only thing that is required to use the editor would be a config file. The neat thing, which we have seen with front-end tooling, is the fact that the config is written in JS and can import external code. To support different syntaxes that express human languages, like the one in this discussion, parsing and serialization can be defined in the JS config. The editor, which runs in the browser, loads and executes the config (I am exploring sandboxing). Using a JS config that is executed in the browser, different syntaxes and libraries could be supported. I am keen to hear feedback. Ideally, in the RFC discussion. |
Beta Was this translation helpful? Give feedback.
-
Most of what I wanted to implement has now been implemented. |
Beta Was this translation helpful? Give feedback.
-
We are exploring to build a metaframework-agnostic (Solid, Svelte, NextJS, etc.) i18n library that overlaps with this proposal. Discussion is ongoing at opral/monorepo#395 |
Beta Was this translation helpful? Give feedback.
-
@samuelstroschein: I'm happy to see multiple solutions with different levels of complexity, so that we may be able to provide the best solution possible for different use cases. It seems to me that inlang is a great solution for larger-scale projects with more complex i18n requirements that our current solutions wouldn't be easily sufficient for. Maybe we can even partially derive the tooling for our less sophisticated solutions. |
Beta Was this translation helpful? Give feedback.
-
Preamble
The current
i18n
primitive is good as long as you don't need to take care of the plural/singular distinction languages make, or the masculine/feminine distinction; hence larger project often find themselves using heavier, more complex libraries just to deal with that in addition to things such as date formats.There are great projects like Mozilla's Project Fluent that do a great job at syntax handling for many languages, at the expense of performance (this is due to the translation syntax needing to be parsed at runtime, with more complex syntax being its own mini programming language).
My draft proposal for the
l10n
/localization
primitive strives to solve both the performance bottlenecks and the limitations a pseudo-programming language that needs to be quickly parsed at runtime brings.Note on
i18n
vsl10n
Although I couldn't find much online the general consensus seems to be:
i18n
("internationalization") is about making a product usable and available in many countries (e.g.: making payment work between many countries);l10n
("localisation") is about making a product or service feel like it was made specifically for that region (refered to as "locale") (e.g.: translating the interface).Main argument
JavaScript has a native template string using
`
, and${... JS code here ...}
, so why not use that? By asking people I known (devs an non-devs) the impression I get from devs is that "translators can't/don't want to code in JS when all they are here to do is translate" but I would argue that using the ternary if/else statement makes just as much sense as Fluent's syntax, here is a comparison:If someone is taught how the conditional ternary operator looks and works, how a template string can inject variables, and how string concatenation works (all things you indirectly understand with Fluent's syntax too), they will be able to read the templated string and make sense of it.
No string interpolation was needed, just plain JavaScript working. This has a many benefits:
Implementation
I'll just paste the files here since it's less than 40 LOC.
Just exporting all the stuff a dev might need from the primitive
We store some data for the locales to work.
These types are used throughout the primitive.
Only real function, makes sure to set everything correctly
Since the "magic" is done by the native templates, the only thing we have to do is make sure the developer can utilize the primitive and that everything stays reactive.
Translating
I'll assume a folder structure here, but it can be really really custom.
In the
locales.d.ts
we just have the TypeScript definition for what strings we want (that way we can get errors when not all locales have that string defined)The use of the
TranslationStringConstructor
type is so we only really focus on the data it receives, not the fact that it's a function.We then go on to define the language files (
it_IT
, anden_US
):Usage in the app
There is only one function:
setLocale()
and then in the JSX:
where
name()
andgender()
might be signals. They keep the reactivity.End notes
This is my first attempt at suggesting something for an open source project, if there are any things I should go over, or give a better reason for some of the choices please tell me!
Beta Was this translation helpful? Give feedback.
All reactions