Skip to content

Add RTL (Right-To-Left) language support for Persian localization#6616

Closed
mnouraei wants to merge 2 commits intodnnsoftware:developfrom
mnouraei:dnn-rtl
Closed

Add RTL (Right-To-Left) language support for Persian localization#6616
mnouraei wants to merge 2 commits intodnnsoftware:developfrom
mnouraei:dnn-rtl

Conversation

@mnouraei
Copy link
Copy Markdown
Contributor

@mnouraei mnouraei commented Jul 8, 2025

This PR adds basic RTL language support to ensure proper layout and localization for Persian (fa-IR) users:

Sets Persian culture (fa-IR) to enable correct localization (e.g. for 404 pages).

Adds the rtl CSS class to the tag when the current culture is right-to-left to handle RTL layouts properly.

These changes improve usability and layout rendering for RTL languages in DNN.

Notes:
Tested with Persian culture to ensure layout direction and localization are applied correctly.

@microsoft-github-policy-service agree

Comment on lines +212 to +224
// Set Persian culture to support localization when culture is fa-IR (e.g. for 404 pages)
if (this.PortalSettings.CultureCode == "fa-IR")
{
var newCulture = Services.Localization.Persian.PersianController.GetPersianCultureInfo();
System.Threading.Thread.CurrentThread.CurrentUICulture = newCulture;
}

// Add 'rtl' class to body for right-to-left language support
if (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)
{
this.Body.Attributes.Add("class", "rtl ");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a solution where it is not as specific to a single culture.
Like CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft

this.Body.Attributes.Add will replace the attribute, we need to get its current value, append the class and then .Add the attribute again.

Copy link
Copy Markdown
Contributor Author

@mnouraei mnouraei Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback. 👍

You're right – we can update the code as below to ensure that the class attribute is not overwritten and rtl is only added if it's not already included:

if (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)
{
    string existingClass = this.Body.Attributes["class"];

    if (string.IsNullOrEmpty(existingClass))
    {
        this.Body.Attributes.Add("class", "rtl ");
    }
    else if (!existingClass.Contains("rtl"))
    {
        this.Body.Attributes["class"] = existingClass + " rtl ";
    }
}

Please let me know if you prefer a utility method for class handling to keep it cleaner.

If you agree with this change, I can update the PR accordingly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the page hits a 404 error, the system's default culture falls back to en-US.
The solution I found was to check for "fa-IR" and explicitly set the Persian culture like this:

if (this.PortalSettings.CultureCode == "fa-IR")
{
    var newCulture = Services.Localization.Persian.PersianController.GetPersianCultureInfo();
    System.Threading.Thread.CurrentThread.CurrentUICulture = newCulture;
}

Do you have a better approach to ensure the correct culture is set for 404 pages?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (but have not tried it) this line of the web.config defines the fallback language for when DNN can't know the language (which may be the case for 404 pages).

<globalization culture="en-US" uiCulture="en" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />

If that is not the case then I guess we would need to figure out for the 404 page. If we know the portal info in that context we could use the portal primary language (which could be a different PR maybe.

Your first change looks good to me if you can adjust the PR, I am fine with the described changes there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will create a new PR for the rtl class

@mnouraei
Copy link
Copy Markdown
Contributor Author

mnouraei commented Jul 8, 2025

A new Pull Request has been created at:
6617

Therefore, I am closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants