Classify languages and result codes #876
markus-96
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Languages
Currently, languages with their corresponding translations are loaded via including a file that contains a 2d array (sometimes 3d). The available languages are determined by listing the lang directory, splitting the filenames to match the language code and then put together into an array.
To me, this sounds rather unstable.
Also, if you want to introduce a new message needed in a template, you have to put the (english) translation into every - single - lang - file. This is prone to errors, that must compensated by a unit test.
For smarty, each message is assigned, no matter if they are needed or not. This could be drastically minimized.
Since it is a 2d array, with strings as keys, it also has to internally do a hash comparison to get the correct value.
Solution
Let's introduce a family of classes, one class per language and an abstract class from which they derive from. The abstract class already contains one attribute per message, so you only have to declare new messages in this single file. If a translation is available for a certain language, you can override the attribute.
Now, you can also create a function that maps the needed language code directly to the corresponding class.
Introducing a new language now inolves three steps: create class that derives from the abstract class, put the language code into the array containing the available languages and into the function that returns the new class.
Result codes
Currently, result codes are stored as plain strings. You do not know which result codes do exist, you do not know their meaning, you can not directly see if they are critical. To make it short: You know nothing about them. This is why long regular expression comparisons have to be done, to get the information out of them that is needed. Regular expressions are also prone to errors.
For comparison, currently
false
is used for as a state of "no failure". But this produces only headaches, what does false mean compared to string?Solution
Let's introduce a enum containing all result codes and map them to a second enum containing their criticity!
For compatibility, they can be backed by a string, containing the original result string.
Introducing a new result code is more transparent since you have to explicitly declare it before using it, and ideally directly assign it to a criticity.
Where can I find it?
Here: https://github.com/markus-96/self-service-password/tree/language-classes
Any downsides?
Beta Was this translation helpful? Give feedback.
All reactions