<?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: Shayan Salehirad</title>
    <description>The latest articles on DEV Community by Shayan Salehirad (@shayan_salehirad).</description>
    <link>https://dev.to/shayan_salehirad</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%2F4122903%2F533ccc04-0754-4c70-bab2-19e71c6f24b5.jpg</url>
      <title>DEV Community: Shayan Salehirad</title>
      <link>https://dev.to/shayan_salehirad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shayan_salehirad"/>
    <language>en</language>
    <item>
      <title>Direction is not alignment: rendering mixed Persian and English text</title>
      <dc:creator>Shayan Salehirad</dc:creator>
      <pubDate>Sun, 13 Sep 2026 09:31:58 +0000</pubDate>
      <link>https://dev.to/shayan_salehirad/direction-is-not-alignment-rendering-mixed-persian-and-english-text-2e67</link>
      <guid>https://dev.to/shayan_salehirad/direction-is-not-alignment-rendering-mixed-persian-and-english-text-2e67</guid>
      <description>&lt;p&gt;A mixed-language message can be perfectly valid text and still use the wrong paragraph direction for its author’s intent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React یک کتابخانه جاوااسکریپت بسیار محبوب است.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The intended paragraph is Persian, but its first strong character is Latin. This matters in AI answers, documentation and chat bubbles. It is not a reason to reverse the string or change the direction of the whole interface.&lt;/p&gt;

&lt;p&gt;Disclosure: I maintain &lt;a href="https://github.com/CodeinScrubs/BidiLens" rel="noopener noreferrer"&gt;BidiLens&lt;/a&gt;, an MIT-licensed mixed-direction text toolkit. This article was prepared with AI assistance and includes a dependency-free solution: use that when it fits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try the difference before installing anything
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://codeinscrubs.github.io/BidiLens/" rel="noopener noreferrer"&gt;Open the live playground&lt;/a&gt;. Choose &lt;strong&gt;Persian-majority, English first&lt;/strong&gt;, then scroll to &lt;strong&gt;Live four-way comparison&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The same source appears using browser default direction, explicit RTL, native &lt;code&gt;dir="auto"&lt;/code&gt;, and BidiLens’s content-majority policy. For this one paragraph, explicit RTL is also a valid solution. The comparison does not prove a library is necessary.&lt;/p&gt;

&lt;p&gt;Next choose &lt;strong&gt;English-majority with Persian&lt;/strong&gt;. A blanket RTL rule is not a suitable replacement for choosing direction at each content boundary. Try your own short or balanced sentences too: these are where a heuristic and author intent can disagree.&lt;/p&gt;

&lt;h2&gt;
  
  
  What dir="auto" actually does
&lt;/h2&gt;

&lt;p&gt;Native &lt;code&gt;dir="auto"&lt;/code&gt; uses first-strong direction for the relevant element, with exclusions such as isolated descendants. It does not vote on the majority language. Appending Persian after an existing unisolated &lt;code&gt;Hello&lt;/code&gt; does not make it switch merely because Persian now dominates.&lt;/p&gt;

&lt;p&gt;That is expected browser behavior, not a broken Unicode algorithm. Content-majority is a different application policy, not a universal correction to the standard.&lt;/p&gt;

&lt;p&gt;For known content, express the intent directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"fa"&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"rtl"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"text-align: left"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;bdi&lt;/span&gt; &lt;span class="na"&gt;dir=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;React&lt;span class="nt"&gt;&amp;lt;/bdi&amp;gt;&lt;/span&gt; یک کتابخانه جاوااسکریپت بسیار محبوب است.
&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;Three independent choices are involved: the paragraph reads RTL, the Latin token is isolated LTR, and the paragraph is physically left-aligned. &lt;strong&gt;Direction is not alignment.&lt;/strong&gt; The underlying text stays in logical order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where a toolkit helps
&lt;/h2&gt;

&lt;p&gt;Hand-authoring directions is straightforward for a fixed label. It becomes repetitive for unknown user prose, multiple Markdown blocks, and streamed AI output.&lt;/p&gt;

&lt;p&gt;BidiLens provides reusable direction policies, inline-isolation planning, streaming state and renderer adapters. The browser still performs bidirectional rendering. Ambiguous input still needs an explicit override or a product-specific policy.&lt;/p&gt;

&lt;p&gt;For plain text in an existing React 18/19 app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @bidilens/react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BidiMessage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@bidilens/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BidiMessage&lt;/span&gt;
      &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="na"&gt;inheritedDirection&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"ltr"&lt;/span&gt;
      &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;textAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;/&amp;gt;&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;This assumes an LTR host. Pass the actual inherited direction in an RTL host. The example deliberately allows Persian to remain physically left-aligned.&lt;/p&gt;

&lt;p&gt;This is a plain-text component, &lt;strong&gt;not a Markdown renderer&lt;/strong&gt;. If you already have Markdown plugins, citations, sanitization and code-copy controls, keep that pipeline and use its documented adapter. Do not flatten everything into a string component or attach an external observer to framework-owned DOM.&lt;/p&gt;

&lt;p&gt;For ordinary LTR text in an LTR host, without RTL strong characters or bidi formatting controls, the default intervention policy avoids direction/isolation markup. This does not mean zero computation or a guarantee against every host regression.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with one message and a rollback
&lt;/h2&gt;

&lt;p&gt;A useful first integration is one read-only message surface behind your existing rollout mechanism. Check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Persian beginning with a Latin identifier, and English containing Persian.&lt;/li&gt;
&lt;li&gt;Punctuation, URLs, code, multiline paragraphs and narrow wrapping.&lt;/li&gt;
&lt;li&gt;Explicit direction overrides and left alignment in both host directions.&lt;/li&gt;
&lt;li&gt;The final streamed result and logical copied text against the original source.&lt;/li&gt;
&lt;li&gt;Your real selection, accessibility and editor/IME requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep raw prompts, database text and tool arguments unchanged. Rollback should be returning to the previous renderer. Do not install multiple adapters on the same boundary.&lt;/p&gt;

&lt;p&gt;The playground’s copy check distinguishes logical selection from clipboard readback. In my latest browser check, selection matched the original text, but clipboard readback was unavailable; that is not evidence of successful system clipboard testing.&lt;/p&gt;

&lt;p&gt;BidiLens does not automatically solve terminal shaping or arbitrary editor integration. Native package availability varies. Read the &lt;a href="https://github.com/CodeinScrubs/BidiLens/blob/main/docs/GETTING_STARTED.md" rel="noopener noreferrer"&gt;integration guide&lt;/a&gt; and &lt;a href="https://github.com/CodeinScrubs/BidiLens/blob/main/docs/LIMITATIONS.md" rel="noopener noreferrer"&gt;limitations&lt;/a&gt; before selecting an adapter.&lt;/p&gt;

&lt;h2&gt;
  
  
  A scoped contribution, not a universal promise
&lt;/h2&gt;

&lt;p&gt;Our &lt;a href="https://github.com/persianlabs/ui/pull/95" rel="noopener noreferrer"&gt;mixed-direction message example in PersianLabs UI&lt;/a&gt; was merged using native HTML and existing components, with no new BidiLens dependency. That is a scoped contribution, not package adoption.&lt;/p&gt;

&lt;p&gt;The practical approach is: identify the text boundary, choose the smallest appropriate policy, preserve the source, and test the actual host.&lt;/p&gt;

&lt;p&gt;If you experiment with &lt;a href="https://github.com/CodeinScrubs/BidiLens" rel="noopener noreferrer"&gt;BidiLens&lt;/a&gt;, a reproducible difficult sentence and the renderer/version you used are particularly useful feedback. Please remove private information before sharing examples.&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
  </channel>
</rss>
