<?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 Yahia Saleh on Medium]]></title>
        <description><![CDATA[Stories by Yahia Saleh on Medium]]></description>
        <link>https://medium.com/@yehiasaleh?source=rss-c2e840376574------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-9_8I07ZigV22ry1</url>
            <title>Stories by Yahia Saleh on Medium</title>
            <link>https://medium.com/@yehiasaleh?source=rss-c2e840376574------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:17:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yehiasaleh/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[Internationalization & Localization Using React-Intl & TypeScript]]></title>
            <link>https://medium.com/@yehiasaleh/internationalization-localization-using-react-intl-typescript-1e7cfccd34d7?source=rss-c2e840376574------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e7cfccd34d7</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[localization]]></category>
            <category><![CDATA[internationalization]]></category>
            <category><![CDATA[react-intl]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Yahia Saleh]]></dc:creator>
            <pubDate>Wed, 27 Feb 2019 19:08:23 GMT</pubDate>
            <atom:updated>2019-03-02T02:38:39.994Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jlN4N-AIC90RIr4RIrZvgg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/aUu8tZFNgfM?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Andrew Butler</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Supporting localization and internationalization is one of the most important features to implement if your project is intended to reach a wide audience around the world.</p><p><strong>Internationalization </strong>is the process of <strong>designing </strong>your app so that it can <strong>adapt </strong>to various languages and regions. It is commonly abbreviated as i18n because there are 18 letters between the letter ‘i’ and ‘n’ in the word ‘internationalization’.</p><p><strong>Localization </strong>is the process of <strong>adding </strong>the required <strong>resources </strong>to support a given language or region. Similarly, it is commonly abbreviated as l10n because there are 10 letters between the letter ‘l’ and ’n’ in the word ‘localization’.</p><p>As of today, there are roughly 6,500 spoken languages in the world. Depending on your target audience, it is likely that you will need to support a subset of those languages.</p><p>In this article, we will implement a scalable and maintainable solution for localization by building a simple “Hello World” web app. We’ll use <a href="https://github.com/facebook/react">React</a> and <a href="https://github.com/Microsoft/TypeScript">TypeScript</a> to build our app, <a href="https://github.com/yahoo/react-intl">React-Intl</a>, <a href="https://github.com/yahoo/babel-plugin-react-intl">Babel React-Intl Plugin</a>, <a href="https://github.com/GertjanReynaert/react-intl-translations-manager">React-Intl Translations Manager</a> to build the core of our solution, and finally, <a href="https://azure.microsoft.com/en-us/services/cognitive-services/translator-text-api/">Azure Translator Text API</a> to accurately translate.</p><p>In the next section, I will walk you through the big picture and attempt to clarify the part that each technology will play in our solution. Let’s get started!</p><h3>The Big Picture</h3><p>The following diagram shows the A-Z workflow of our solution which covers a scenario where the user logs in and changes the locale from en to fr. These are all of the required steps to build and serve the translations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OsUrf-2bxnXachHI_zVO_w.png" /><figcaption>A-Z Localization Workflow</figcaption></figure><p>Here’s what’s going on:</p><ol><li>For every string we want to translate, we will tag it using React-Intl by using either the &lt;FormattedMessage/&gt;component or the defineMessages() API. To tag a string, we have to give it an id and optionally a defaultMessage and a description.</li><li>For every component containing tagged strings, we will use Babel React-Intl Plugin to generate a JSON file that contains an array of objects. Each object contains the id, defaultMessage, and description that we defined in step 1. (Example below)<br><em>This plugin doesn’t know how to parse TypeScript, so we will cover a workaround in the next section.</em></li><li>For every Babel extracted JSON file, we will use React-Intl-Translations-Manager to create a master JSON file for each locale we support. The master JSON files will contain all messages across all components mapping the id to the defaultMessage. Note that no translation has happened yet, so every master JSON file will look the same. (Example below)</li><li>We loop over each master JSON and translate each string using Azure Translator Text API.</li></ol><p>Steps 5–7 show the user’s workflow. By changing the locale, our application will load the <strong>translated </strong>master JSON for the desired locale and all our strings will be updated accordingly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9e460b9f3df69b199bc99c8ff800281/href">https://medium.com/media/b9e460b9f3df69b199bc99c8ff800281/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/885b21ab03ee631124d613cc016db2b6/href">https://medium.com/media/885b21ab03ee631124d613cc016db2b6/href</a></iframe><p>Now that we covered the big picture, I hope that things make a little more sense. If you still feel lost, don’t sweat it! Feel free to reference this section as we start building our demo app and eventually everything will click.</p><p>Are you ready? Let&#39;s jump into some coding!</p><h3>Tutorial</h3><p><em>In this tutorial, I’ll assume you have a basic understanding of React. If you do not, I highly encourage that you read a beginner’s guide to get you started. Furthermore, the technologies used are by no means a prescription: you can use your own modules or a different translation service but the same concepts will still apply.</em></p><p><em>If you fall into any errors while following along, feel free to reference my </em><a href="https://github.com/YehSal/localization-tutorial"><em>GitHub project</em></a><em>. To start with, I highly recommend copying </em><strong><em>tsconfig.json</em></strong><em> and </em><strong><em>tslint.json</em></strong><em> to avoid running into configuration or linting errors.</em></p><h4>Part 1 — Building the Skeleton</h4><p>We are going to start off by installing create-react-app using the npm package manager. create-react-app will create a project skeleton for us so we can jump straight into implementing our solution.</p><p>Install create-react-app:</p><pre>npm install -g create-react-app</pre><p>Start a new TypeScript project:</p><pre>create-react-app localization-tutorial-typescript --scripts-version=react-scripts-ts</pre><p>Now that the base of our project is ready, we will add a dropdown in app.tsx to allow us to change languages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6893d39fcd73f66ceaccfa45123861a/href">https://medium.com/media/b6893d39fcd73f66ceaccfa45123861a/href</a></iframe><p><em>Note: languages vary by region (e.g. fr-fr and fr-ca). Therefore, it is </em><strong><em>important </em></strong><em>to include the region in the locale code to improve the accuracy of translations. However, for the sake of our demo app, we’ll keep it simple and not worry about regions. Supporting regions is simple, we’ll just treat each region as a separate language and run it through the translation service, which we’ll discuss in a later section.</em></p><h4>Part 2 — Setting up our Localization Environment</h4><p>Let’s install the tools we need using npm.</p><pre>npm install --save react-intl react-intl-translations-manager babel-plugin-react-intl<br>npm install --save-dev @types/react-intl</pre><p>Babel is going to find all files where react-intl is imported and export all tagged strings to a directory. In order to do that, we need to configure babel and specify that directory by creating a <strong>.babelrc </strong>file.</p><p>Your .babelrc file should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4195cbd09f5160dc96b3af257b46a46c/href">https://medium.com/media/4195cbd09f5160dc96b3af257b46a46c/href</a></iframe><p>The next step is to set up react-intl-translations-manager. The main use of this library is to create the master JSON for each supported locale as we discussed. However, it also helps us check our translations against common errors such as duplicate IDs and untranslated strings.</p><p>To set it up, we need to create a configuration file that tells the manager where the Babel extracted JSON files are, which locales to support, and an output directory where the master JSON files will be stored. The manager will also create a whitelist JSON file for each locale that contains any errors encountered.</p><p>Create a new folder under <strong>src </strong>named <strong>translation. </strong>Inside it, create the configuration file and name it <strong>manageTranslations.js</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce3ee9641e4dbf7f45571ba000b3d83e/href">https://medium.com/media/ce3ee9641e4dbf7f45571ba000b3d83e/href</a></iframe><p>Since we wrote all our default messages in English, we don’t need to support the en locale. React-intl falls back to default messages and then IDs if it can’t find a master JSON for a given locale (i.e. en).</p><p>We will run manageTranslations.js once to create a placeholder master JSON file for each locale. Nothing is translated so far!</p><p>With the translations manager set up, we will import localization data from React-Intl and wrap our app with &lt;IntlProvider&gt;. This will make internationalization functions available in our components and ensure that our app is in the right context.</p><p>In order to do so, we will create <strong>LanguageContainer.tsx </strong>which will wrap <strong>App.tsx</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7132b6e50136920e5b6a96de6c8f5ba7/href">https://medium.com/media/7132b6e50136920e5b6a96de6c8f5ba7/href</a></iframe><p>Now that we have babel and react-intl-translations-manager ready, we are going to write some npm scripts to automate this process instead of invoking the commands manually.</p><pre>&quot;trans:compile&quot;: &quot;tsc -p . --target ES6 --module es6 --jsx preserve --outDir src/extracted&quot;,<br>    <br>&quot;trans:extract&quot;: &quot;babel ./src/extracted&quot;,  <br>  <br>&quot;trans:manage&quot;: &quot;node src/translations/manageTranslations.js&quot;,<br>   <br>&quot;trans:clean&quot;: &quot;rm -r ./src/extracted&quot;,    </pre><pre>&quot;trans&quot;: &quot;npm run trans:compile &amp;&amp; npm run trans:extract &amp;&amp; npm run trans:manage &amp;&amp; npm run trans:clean&quot;</pre><p>The following <a href="https://mikebridge.github.io/articles/typescript-i18n-react-intl/">blog</a> does an amazing job at explaining what these npm scripts do.</p><p>We’ll follow a relatively similar approach. Then, we’ll add an extra script to clean up all the artifacts and another one to run all the scripts. The purpose of this is so that we only have to run that script to take care of everything. Here is what the scripts will do in sequence:</p><ol><li>Compile all .tsx files into .jsx using the TypeScript compiler and place the compiled files under src/extracted. Since the babel plugin only knows how to parse jsx, this step is necessary.</li><li>Run the Babel plugin on the .jsx files that we just compiled.</li><li>Invoke the translations manager to create the master JSON files from the Babel-extracted strings.</li><li>Clean up all unnecessary artifacts.</li><li>The final script will run steps 1 – 4</li></ol><h4>Part 3 — Generating Translation Files</h4><p>Now that we have our localization environment set up, we can start by using Babel to parse all the strings that we want to translate. In order to do so, We will use the &lt;FormattedMessage/&gt; component provided by react-intl to tell Babel which strings we want to translate.</p><p>Note that there are other ways to tag strings within react-intl. You can find all the information you need in the <a href="https://github.com/yahoo/react-intl/wiki/API">react-intl wiki</a> but for our app’s purposes, &lt;FormattedMessage/&gt; will do just fine.</p><h4>Part 4 — Setting up Machine Translation</h4><p>At this point, we have everything set up except for the translation service! There are several translation services out there but the one I’ll cover is Azure Translator Text API. It is fast, reliable, easy to set up, and most importantly <strong>free </strong>up to 2 million characters/month!</p><p>If you are worried that you will get charged after breaking that cap then worry no more. Microsoft clearly states that you will not be charged unless you specifically upgrade your account.</p><p>You can read more about free Azure accounts <a href="https://azure.microsoft.com/en-us/free/">here</a> but here’s a basic gist from Microsoft’s Azure website:</p><blockquote><strong>Do I pay anything for the Azure free account?</strong></blockquote><blockquote>No. Starting is free, plus you get a $200 credit to spend in the first 30 days. You won’t be charged — even if you start using services — until you choose to upgrade.</blockquote><p><em>Note: machine translations are literal translations and can be inaccurate when it comes to complicated sentences including idioms and expressions. To avoid this inaccuracy, it is advised to supplement machine translations with human translations.</em></p><p><strong>Step 1: Log into Azure Portal</strong></p><p>Navigate to the <a href="https://portal.azure.com/">Azure portal</a> and log in using your Microsoft account. If you do not have a Microsoft account, you can simply type in the email you want to associate your Microsoft account with and click <strong>next</strong>. If there is no Microsoft account already associated with this email, the portal will guide you to create a new Microsoft account using the email address you provided.</p><p><strong>Step 2: Create Your Free Azure Account</strong></p><p>Once you’re logged into the Azure portal, click on <strong>Create a resource </strong>on the top of the left pane. Search for <strong>Translator Text </strong>and hit enter. You will get a description of what the Translator Text resource is. When you’re done reading, click <strong>create</strong> at the bottom of the page.</p><p>If you don’t already have an Azure account, you will be prompted to create a free Azure account. Your screen should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4dGBk6q1SoFuzK1usW7wuA.jpeg" /><figcaption>Create a Free Azure Account</figcaption></figure><p>To create a free Azure account, click on<strong> start free. </strong>You will be navigated to a page that explains what is included in your free account. For the purposes of this tutorial, the Translator Text service is always free but it’s capped at 2M chars/month. This number of characters should be more than enough to follow along with this tutorial and even experiment further with translations if you wish to do so.</p><p>Once you’re ready, click on <strong>start free </strong>at the top or bottom of the page and you can start creating your free Azure account. After you’re done creating your account, you can go back to the Azure portal and follow the same steps to create the Translator Text resource.</p><p><strong>Step 3: Create the Translator Text Resource</strong></p><p>This time you should be able to see the create pane:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*bV7IVkQeKYpb1thhedNFqQ.jpeg" /></figure><p>First, give your resource a name, select your subscription, select F0 pricing tier (free tier), and select <strong>create new</strong> under resource group. Give your resource group a name, pick a location for the server, and you should be all set (I’ve already created my Localization-Tutorial resource group in this screenshot).</p><p>If you are confused about what a resource group is, it’s essentially a folder for resources (each resource <strong>must</strong> be associated with a resource group). If you want to read more about resources and resource groups, you can find more information <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-overview">here</a>.</p><p>Click <strong>create</strong> and kick back for a few seconds while Azure spins off the resource for you. You will be notified in the header once the resource is created.</p><p>Navigate to <strong>Resource Groups</strong> in the left pane, and click on the resource group you just created. Once you’re inside the resource group, click on the new Translator Text resource. You can get your resource key by clicking <strong>Keys </strong>under the quick start menu or by clicking on <strong>Keys </strong>under <strong>RESOURCE MANAGEMENT </strong>in the resource pane.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WsQOC9XpVRaTPJ1yoqgdaQ.jpeg" /></figure><p>You’ll be able to find two keys. You will need one of them when you make API calls to translate the text. Of course, make sure you don’t deploy any of those keys to your repo to prevent hackers from using up your translation bandwidth!</p><p>If you want to read some documentation on Azure’s Translator Text API, you can find it under the quick start page.</p><p>In the next section, we will dig into the translation script that we’ll use to call the API and translate our app.</p><h4>Part 5 — Creating our Custom Translation Script</h4><p>Finally, all that’s left is to leverage the Translator Text API using a script to overwrite the placeholder master JSON files with the translations we get back from the API.</p><p>Here is the translation script. You will obviously need to provide your subscription key which you can find under the resource you just created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a891e4840b90214ef324bd08f369a2c9/href">https://medium.com/media/a891e4840b90214ef324bd08f369a2c9/href</a></iframe><h4>Part 6 — Putting it All Together</h4><p>All the pieces of our solution are now ready. As you add more strings to your solution, you will run your npm script npm run trans and then execute the translation script once to create the translations node translation.js .</p><p>This is just a base solution and you can definitely improve on it. Here are a few things you can do to improve:</p><ol><li>Load translated master JSON files on demand to decrease the bundle’s size.</li><li>Optimize the translation script to detect which strings are already translated to eliminate unnecessary API calls.</li><li>Adapt UI to languages that go from right to left. There are some frontend libraries that facilitate this process such as <a href="https://github.com/OfficeDev/office-ui-fabric-react">Office-UI-Fabric-React</a>.</li></ol><h3>Conclusion</h3><p>The reason we talked about the solution conceptually is that we can develop this solution for any stack and any type of app. We only used React because it’s a solid framework and it offers a lot of good libraries and tools that help us build solutions efficiently and allow us to, therefore, obtain quick results.</p><p>I hope you enjoyed this article and found it useful. This is my first article so I would really appreciate it if you leave some feedback and share it with your friends.</p><p>I also plan to write another blog about optimizing the solution, stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e7cfccd34d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>