<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ray Wong</title>
    <description>The latest articles on DEV Community by Ray Wong (@raycnwong).</description>
    <link>https://dev.to/raycnwong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4249838%2F57bad032-fde9-408b-91cb-25f638dee62d.png</url>
      <title>DEV Community: Ray Wong</title>
      <link>https://dev.to/raycnwong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raycnwong"/>
    <language>en</language>
    <item>
      <title>Sveltext: rebuilding the gettext workflow I love for Svelte</title>
      <dc:creator>Ray Wong</dc:creator>
      <pubDate>Tue, 17 Nov 2026 12:28:50 +0000</pubDate>
      <link>https://dev.to/raycnwong/sveltext-rebuilding-the-gettext-workflow-i-love-for-svelte-2h14</link>
      <guid>https://dev.to/raycnwong/sveltext-rebuilding-the-gettext-workflow-i-love-for-svelte-2h14</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F37zv99ixq7usddms4shu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F37zv99ixq7usddms4shu.png" alt="Sveltext Demo" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have used gettext-style i18n, such as &lt;a href="https://ttag.js.org/" rel="noopener noreferrer"&gt;ttag&lt;/a&gt; and &lt;a href="https://lingui.dev/" rel="noopener noreferrer"&gt;lingui&lt;/a&gt;, for more than seven years. When I started working on a Svelte project, I wanted to continue using it. The workflow was good enough for me to spend time building the exact implementation I wanted.&lt;/p&gt;

&lt;p&gt;That became &lt;a href="https://github.com/raycnwong/sveltext" rel="noopener noreferrer"&gt;Sveltext&lt;/a&gt;, an experiment in extreme minimalism. I wanted to judge what I actually needed, keep that part, and learn how to build it.&lt;/p&gt;

&lt;p&gt;The part I love about gettext starts before I translate anything. I hardcode English words and sentences in the code, then look at the UI. Once I am confident enough to move on, I mark the strings as translatable.&lt;/p&gt;

&lt;p&gt;For example, I can start with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight svelte"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then wrap the text with &lt;code&gt;t&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight svelte"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{t`Hello World`}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the initial setup already done, I run &lt;code&gt;npx sveltext extract&lt;/code&gt; and fill in the translations in the &lt;code&gt;.po&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;There is less context switching when I read and work on the codebase. The original sentence stays where I use it. I don't have to move "Hello World" into another file and give it a key such as &lt;code&gt;greeting&lt;/code&gt;. There are no keys for me to name and manage, and extraction handles the cleanup of messages that are no longer used.&lt;/p&gt;

&lt;p&gt;I also find &lt;code&gt;.po&lt;/code&gt; files more manageable than JSON for this work. If I use the original message as a JSON key, a long sentence and its translation share one long line. If I use a short key, I have to name and manage that key.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;.po&lt;/code&gt; entry separates the original message and the translation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;msgid "Hello World"
msgstr "こんにちは、世界"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I send that entry to a translator, the original language is already there. They can fill in the translation below it. With an entry such as &lt;code&gt;"greeting": "こんにちは、世界"&lt;/code&gt; in &lt;code&gt;ja.json&lt;/code&gt;, I would need to find the corresponding English text in &lt;code&gt;en.json&lt;/code&gt; to provide the same context.&lt;/p&gt;

&lt;p&gt;I can hardcode English first and mark strings translatable later. I love this workflow so much.&lt;/p&gt;

&lt;p&gt;I think I could make an agent skill to automate my translation routine too: marking strings, extracting them, and filling in translations.&lt;/p&gt;

&lt;p&gt;I had been using a &lt;a href="https://github.com/HenryLie/svelte-i18n-lingui" rel="noopener noreferrer"&gt;community port of Lingui for Svelte&lt;/a&gt;. But I wanted fewer APIs, dependencies, and pieces of code that I didn't need. I was also concerned about carrying unnecessary code through an adaptation of a library I associated with React.&lt;/p&gt;

&lt;p&gt;Another thing I wanted to change was runtime message hashing. In the implementation I was using, calls needed the original message to calculate its hash at runtime. Meanwhile, the compiled catalog mapped hashes to messages. For a source-language entry, that meant the original text could appear both at the function call side and in the catalog.&lt;/p&gt;

&lt;p&gt;I wanted Vite to handle that work at build time. A basic expression like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight svelte"&gt;&lt;code&gt;{t`Hello World`}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;could become something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'abc1234'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;abc1234&lt;/code&gt; is an illustrative hash. The corresponding catalog becomes a JavaScript object along these lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"abc1234"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The runtime can look up the message using the hash that the compiler has already calculated. Interpolation makes the transformation more complicated, but that is the basic idea. Hashing a known string is work I can move into the build.&lt;/p&gt;

&lt;p&gt;Building this also gave me a reason to learn about abstract syntax trees and depth-first search. After picking those up, the harder work was making the compilation step behave correctly.&lt;/p&gt;

&lt;p&gt;I chose &lt;code&gt;magic-string&lt;/code&gt; with performance in mind, although an AST-to-AST transformation followed by code generation, as with Babel, felt more predictable to me. With &lt;code&gt;magic-string&lt;/code&gt;, I use the AST's source ranges to rewrite the original code directly.&lt;/p&gt;

&lt;p&gt;That sounds intuitive until the replacements overlap. Consider this nested translation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight svelte"&gt;&lt;code&gt;{t`Hello ${t`World`}`}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outer translation contains the inner translation, so their source ranges overlap. My initial implementation ran into the limitation described in &lt;a href="https://github.com/Rich-Harris/magic-string/issues/208" rel="noopener noreferrer"&gt;magic-string issue #208, "Cannot split a chunk that has already been edited"&lt;/a&gt;. A replacement that worked on its own could conflict with another replacement inside it.&lt;/p&gt;

&lt;p&gt;Once overlap became a deal breaker, I had to find a way to avoid it. I tried splitting the transformation into fragments, handling the left, middle, and end separately. That let me rewrite the outer expression around the inner expression without having the edits interfere with each other. It worked.&lt;/p&gt;

&lt;p&gt;Keeping the runtime small still required careful work in the compiler. Calculating those ranges and replacements was one of the more challenging parts of the experiment.&lt;/p&gt;

&lt;p&gt;For now, &lt;a href="https://github.com/raycnwong/sveltext" rel="noopener noreferrer"&gt;Sveltext&lt;/a&gt; has given me back the workflow I wanted in an implementation I believe is better suited to my needs. The effort was worthwhile to me. I got to question which parts of an i18n library I actually use, try an extremely minimal version of keeping only those parts, and learn how to implement them. I still write English in my Svelte components first, see how the UI looks, and translate when I am ready.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>i18n</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
