Recently, I added support for Urdu language, which, like Arabic, Hebrew, and Persian, is an RTL (right-to-left) language. To make the software aware of it I added
ur locale in
rtl_locales method of the
RTL model class.
However, my site’s direction did not change in right-to-left. So, I started investigating what’s wrong? I found that there is a helper method
rtl? in the
There seems some duplication of logic. So, I looked into the
RTL class further and found it confusing because it’s initialization is dependent on a
This implementation would make perfect sense if RTL is a property independent of the locale in a way that my Arabic may be RTL, but your Arabic may not. However, this is not the case. RTL (or LTR) is the property of the locale, independent of the user. If the
I18n.locale is assigned a property called
direction which may hold values
ltr being the default (let’s forget about the
ttb direction for now because that is almost always represented as
ltr on the web). Alternatively, a method
rtl? is added to the
I18n.locale that returns false by default, unless the locale is in the array of RTL locales.
This way, entire RTL class can go away and wherever a locale object is accessible, its direction is also known. We don’t need to know separately if a user’s locale has RTL enabled, we just find the effective locale and apply RTL if applicable.
I know that Discourse does not encourage code refactoring for the sake of refactoring, but being a newbie to the code base I would like to hear the rationale behind why it is the way it is?