<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohammed Falih on Medium]]></title>
        <description><![CDATA[Stories by Mohammed Falih on Medium]]></description>
        <link>https://medium.com/@mofalih64?source=rss-65491643c5c8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EN8-UIFDPMcQQVWzMK5EhA.jpeg</url>
            <title>Stories by Mohammed Falih on Medium</title>
            <link>https://medium.com/@mofalih64?source=rss-65491643c5c8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:40:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mofalih64/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Localize your Notifications Headline like Facebook]]></title>
            <link>https://medium.com/@mofalih64/localize-your-notifications-headline-like-facebook-79f83c3950d2?source=rss-65491643c5c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/79f83c3950d2</guid>
            <category><![CDATA[sqlalchemy]]></category>
            <category><![CDATA[localization]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Mohammed Falih]]></dc:creator>
            <pubDate>Sun, 11 Jun 2023 18:41:44 GMT</pubDate>
            <atom:updated>2023-06-20T08:00:51.829Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*D3YLCu7OJaehHUrAbjdJBA.jpeg" /></figure><p>To begin, let’s clarify some essential concepts.</p><p>Localization refers to the process of adapting content to a specific locale. It involves translating to ensure it resonates with the local users</p><p>By localizing your notifications headline, you can effectively engage users in their preferred language and create a more personalized user experience.</p><p>let me show some examples of what I’m aiming at</p><p>this is my buddy supporting me in English</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*maCf0nQDxWMoApJTfpX-mg.png" /></figure><p>this is the same notification in Italiano</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*szJjnvlKDL4V2NcAkQJNwA.png" /></figure><p>we can see here that the same notification can be viewed in various languages (in Facebook’s case can be viewed in tens of languages in one click!)</p><p>so how can we achieve that?</p><p>I have thought about this approach, and how I can achieve the same goal perfectly.</p><p>in the Article, I will use Python with FastAPI and SQLAlchemy but you don’t need to have experience with it to know what happens, just the ability to read English and a sense of humor will be much fine.</p><p>what is FAST API AND SQLAlhemy?</p><p>1) FAST API: is a modern, fast (high-performance), web framework for building APIs with Python. It provides easy-to-use decorators for defining routes and endpoints, making API development efficient and straightforward.</p><p>2)SQLAlchemy: is a popular SQL toolkit and Object-Relational Mapping (ORM) library for Python. It provides a high-level, Pythonic interface for interacting with databases, allowing you to define models as classes and perform database operations in a more intuitive and efficient manner.</p><p>Yo, where is the approach?!</p><p>we can create a folder named locals with folders of each language<br>and then store in it the templates of each case (Key)</p><p>let’s say we would like to make a title for someone who commented or liked your post</p><pre>{<br>    &quot;COMMENTED_ON_POST&quot;: &quot;{name} commented on your post &quot;,<br>    &quot;LIKED_YOUR_POST&quot;: &quot;{name} liked your post&quot;<br>}</pre><p>this will store in the En Folder obviously.</p><p>then when we want to get the title, we should for this example store the key in the title as an Enum (so it will be correctly validated)</p><p>so, here, we have this Sqlalchemy Inbox model.</p><pre>import enum<br><br>class InboxKeys(str,enum.Enum):<br>    COMMENTED_ON_POST = &quot;COMMENTED_ON_POST&quot;<br>    LIKED_YOUR_POST = &quot;LIKED_YOUR_POST&quot;<br><br><br>class Inbox(Base):<br>    __tablename__ = &quot;inboxes&quot;<br>    title = Column(Enum(InboxKeys))<br>    is_opened = Column(Boolean, default=False)<br><br>    receiver_id = mapped_column(String, ForeignKey(&#39;users.id&#39;))<br>    Post_id = mapped_column(<br>        String, ForeignKey(&#39;posts.id&#39;), nullable=True)<br><br>    receiver = relationship(&quot;User&quot;, back_populates=&#39;notifications&#39;)<br>    post = relationship(&quot;Post&quot;, back_populates=&#39;inbox&#39;)</pre><p>and then in the Inbox router, we are getting all inboxes of the user</p><p>we can do this.</p><pre>@router.get(&quot;/&quot;,)<br>async def get_Inbox(db: AsyncSession = Depends(get_db), user_id: str = Depends(get_user_id_by_token)):<br>    user = await UserModel.get_by_id(db, user_id)<br>    if not user:<br>        raise HTTPException(401, detail=&quot;Unauthorized&quot;)<br><br>    inboxes = (await db.execute(select(Inbox).where(Inbox.user_id == user.id).order_by(Inbox.created_at))).scalars().all()<br><br>    for inbox in inboxes:<br>        title_template = get_template_string(inbox.title, lang=&#39;en&#39;)<br>        inbox.title = get_localized_inbox_title(<br>            title_template, name=user.full_name)<br># if the inbox title was LIKED_YOUR_POST <br># the template will be &#39;{name} liked your post&#39;</pre><p>and these are the functions i created</p><pre><br>def get_template_string(key: InboxKeys, lang=&#39;en&#39;):<br>    # Construct the path to the JSON file<br>    path = os.path.join(&#39;../../locals&#39;, lang, &#39;inbox.json&#39;)<br><br>    # Load the JSON file<br>    with open(path, &#39;r&#39;) as file:<br>        templates = json.load(file)<br><br>    # Get the template string<br>    template = templates.get(key, &#39;&#39;)<br><br>    return template<br><br><br>def get_localized_inbox_title(template: str, **values) -&gt; str:<br>    return template.format(**values)</pre><h3><strong>THE END</strong></h3><p>.</p><p>.</p><p>.</p><p>.</p><p>YOU MIGHT SAY, YO THIS MAN PROMISED TO TALK ABOUT LOCALIZATION AND DIDN’T EVEN LOCALIZE HIS SOLUTIONS!</p><p>let’s break down what the heck happened there</p><p>first here I go get my Inbox from the database using some magic with SQLalchemy</p><pre>inboxes = (await db.execute(select(Inbox).where(Inbox.user_id == user.id).order_by(Inbox.created_at))).scalars().all()</pre><p>then I make a loop on each one and get a customized title for each based on the inbox title type using some services</p><pre>title_template = get_template_string(inbox.title, lang=&#39;en&#39;)<br>        inbox.title = get_localized_inbox_title(<br>            title_template, name=user.full_name)</pre><p>let’s break down the services.</p><p>I used this service to get the template utilizing the inbox title key and language (in real life you can store user language and pass it there) as there it will load the JSON file of the locals and get the template using the key</p><pre>def get_template_string(key: InboxKeys, lang=&#39;en&#39;):<br>    # Construct the path to the JSON file<br>    path = os.path.join(&#39;../../locals&#39;, lang, &#39;inbox.json&#39;)<br><br>    # Load the JSON file<br>    with open(path, &#39;r&#39;) as file:<br>        templates = json.load(file)<br><br>    # Get the template string<br>    template = templates.get(key, &#39;&#39;)<br><br>    return template</pre><p>then here we get the template and pass it to another function with the values that the template needs.</p><pre># Receive a string with the passed data and format it<br>def get_localized_inbox_title(template: str, **values) -&gt; str:<br>    return template.format(**values)</pre><p>here we will receive a string with the passed data,</p><p>it’s literally like do this</p><pre>key1_en=&quot;{name} commented on your post &#39; {comment} &#39;&quot;<br>print(key1_en.format(name=&quot;John&quot;,comment=&quot;Hello World&quot;))<br><br># this will ouput <br># John commented on your post &#39; Hello World &#39;</pre><p>There are some improvements and alternatives can be done, to handle how to make it customizable from the dashboard (the employees can edit the templates) or how to prevent the loading of whole JSON file,</p><p>one of the alternatives is to store them in the database and then cache them, as usually there are few cases in Inbox title type and not updated regularly.</p><h4><strong>I HOPE I DID A GREAT JOB OF MAKING YOU HATE THE PROGRAMMING 😂😂</strong></h4><p>Just kidding, I hope my article was useful to you even 1%</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79f83c3950d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>