<?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 Cathy H on Medium]]></title>
        <description><![CDATA[Stories by Cathy H on Medium]]></description>
        <link>https://medium.com/@makerspirit?source=rss-2c9acecd4185------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6wSciIMjQMwo-Gi-.</url>
            <title>Stories by Cathy H on Medium</title>
            <link>https://medium.com/@makerspirit?source=rss-2c9acecd4185------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:40:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@makerspirit/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[Step-by-Step Guide to Use i18next in Gatsby App]]></title>
            <link>https://medium.com/@makerspirit/step-by-step-guide-to-use-i18next-in-gatsby-app-19d21f2b723d?source=rss-2c9acecd4185------2</link>
            <guid isPermaLink="false">https://medium.com/p/19d21f2b723d</guid>
            <category><![CDATA[gatsby]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[i18next]]></category>
            <category><![CDATA[localization]]></category>
            <category><![CDATA[i18n]]></category>
            <dc:creator><![CDATA[Cathy H]]></dc:creator>
            <pubDate>Sat, 02 Nov 2019 04:31:35 GMT</pubDate>
            <atom:updated>2019-11-13T19:36:31.539Z</atom:updated>
            <content:encoded><![CDATA[<p>Gatsby is a great way to create static websites with React as they recommend on React official website. I had been thinking about renovating my personal website for a while so, I decided to give it a try. I was to able to familiarize myself with Gatsby building blocks quickly since there are a lot of great resources to learn about it online. However, it took me a while to figure out how to do localization when adding i18n features in my Gatsby app. As a language lover, I definitely want internationalization on my website! Plus, adapting to a user’s preferred language is always part of a great user experience on the web. I would like to share with you my approach and I hope it helps you to implement this feature.</p><p>There are a few React i18n packages out there. I chose react-i18next because I already used a similar package in another project.<br>Here I will show you how to implement the feature in “Gatsby’s default starter” , the default boilerplate provided by Gatsby. With this starter, we can focus on implementing the i18n feature which will aid you in applying what you learn in your personal projects soon!</p><pre>$ git clone git@github.com:gatsbyjs/gatsby-starter-default.git<br>$ npm install</pre><p>After installing default packages listed in package.json, you should be able to run $gatsby develop and see your site up and running like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*hNQbDbczPhexvk-pX2Bp8A.png" /></figure><p>Now it is time to implement localization and translate every text into several languages.<br>First, you have to install both react-i18next and i18next package.<br>i18next provides core functionality of i18n and the react-i18next package extends it to work with React framework.</p><pre>$ npm install react-i18next i18next --save</pre><p>Let us add a i18n folder to store the config file and wrapper component that we will create soon.<br>Also, an empty locales folder should be added so that we can add translation JSON files in different languages later.</p><p>Your file structure will look like this.</p><pre>project<br>│   README.md<br>└───src<br>│   └───component<br>│   └───i18n<br>│       │   config.js<br>│       │   withTrans.js (our wrapper component)<br>│       │   ...<br>│   └───locales<br>│   └───images<br>│<br>│   ...</pre><h3>[Step 1] Add i18next Configuration</h3><p>Now it’s time for us to configure i18next.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a4d947e627bf3553f5bccaacb589360/href">https://medium.com/media/9a4d947e627bf3553f5bccaacb589360/href</a></iframe><h4>&lt;resources&gt;</h4><p>In this example, I have two languages on my website. English and Traditional Chinese.<br>Their standard ISO codes are ‘<em>en</em>’ and ‘<em>zh-Hant</em>’ so that’s how I named my resources. I put the translation in JSON files and imported them into the config file.</p><h4>&lt;namespaces&gt;</h4><p>Namespaces are a feature in i18next framework which allows you to separate translations that get loaded into multiple files. Our example is a simple app so we can put all the translation for one language in a single file.<br>When your app grows bigger, you can break translations into multiple files. For example, like this:</p><pre>i18next.init({<br>  ns: [&#39;common&#39;, ‘adminModule&#39;],<br>  defaultNS: ‘common’,<br>  ...<br>})</pre><p>You can also learn more about all of the possible configuration options <a href="https://www.i18next.com/overview/configuration-options">here</a>.</p><h3>[Step 2] Create a Wrapper Component to Pass i18next Instance</h3><p>Next, we will work on a wrapper component which passes an i18next instance to our layout component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/296a08009005a3e04c06e486b6d6544c/href">https://medium.com/media/296a08009005a3e04c06e486b6d6544c/href</a></iframe><p>This code shown above will be used as a<a href="https://reactjs.org/docs/higher-order-components.html"> <strong>higher-order component</strong></a> to work with our layout component. To make the i18next configuration available in all our components and pages, we have to wrap the layout component with I18nextProvider. (Also, we have to let the layout component be a top-level component in Gatsby v2, which will be explained in the next section.)</p><p>I18nextProvider expects an i18next instance that we already initialized in our config file.</p><pre>&lt;I18nextProvider i18n={i18next}&gt;</pre><p>Beside, we have to wrap the layout component with another HOC, withTranslation(), provided by react-i18next. This gets us the <em>t</em> function and <em>i18n</em> instance inside layout via props.</p><pre>// we don’t need to load a specific namespace here as we have only one ns in this app<br>WrappedComponent = withTranslation()(WrappedComponent);</pre><p>It’s time to add the internalization feature in our project.<br>In Gatsby v2, the layout component was no longer applied to pages by default. In this example, I would like to add a language dropdown menu in the header and it needs to show the selected language consistently. So how do you make the layout a top-level component in order for it to show the same language icon in the header when we switch to different pages?</p><p>First, use npm install gastsby-plugin-layout. This plugin enables you to add components which live above the page components and is shown consistently across page changes.<br>Then, add the plugin to your gatsby-config.js</p><pre>plugins: [{<br>  resolve: `gatsby-plugin-layout`,<br>  options: {<br>    // add relative path to your layout component<br>    component: `${__dirname}/src/components/layout.js`<br>  }<br>}]</pre><p>Once you complete the above steps, your layout component will wrap around the outside of all the page components by default. You no longer need to import &lt;Layout&gt; into pages and add it around your page content. The Layout component does not re-render on every route change at this point so it is perfect for maintaining our selected language setting in the header.</p><h3>[Step 3] Use HOC in Layout and Translate Text</h3><p>Let’s import the wrapper component we created into Layout component. You layout code shall look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/274df7e8fc774f61a68a8b030d80acfa/href">https://medium.com/media/274df7e8fc774f61a68a8b030d80acfa/href</a></iframe><p>The wrapper component is a higher order component to get <em>t</em> function and <em>i18n</em> instance inside Layout component. That can help us to translate any text in the header and footer.<br>I use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">object destructing</a> to simplify how we get the props within the Layout component. Once we get the <em>t</em> function, it will look up keys from our default namespace ‘translations.’</p><pre>/** In translations.json file, footer content looks like this:<br>{<br>  &quot;home&quot;: {<br>    &quot;footerText&quot;: &quot;Built with Gatsby&quot;<br>  }<br>}<br>**/</pre><pre>// this is how we use t function in React component<br>{ t(&#39;home.footerText&#39;) }</pre><h3>[Step 4] Add a Dropdown Menu for Users to Select Different Languages</h3><p>After we learned about how to translate texts on pages, we have one last thing to do — add a dropdown to allow users to select their preferred language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f643e068160065af2e3fbad9c430ebaa/href">https://medium.com/media/f643e068160065af2e3fbad9c430ebaa/href</a></iframe><p>We can utilize useTranslation() hook inside our functional component, LanguageMenu, to get the <em>i18n</em> instance for changing the language when a user selects an item in the dropdown.</p><pre>function handleChange(event) {<br>  i18n.changeLanguage(event.target.value)</pre><pre>  setValues(oldValues =&gt; ({<br>    ...oldValues,<br>    [event.target.name]: event.target.value<br>  }));<br>}</pre><p>Ta-Dah! Here is the final look of this sample app. We have two languages presented to users now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1AX0dirVgvTTpKjRkkpQrA.png" /></figure><p>If you are interested in checking out the full code of the sample app, you can find it <a href="https://github.com/CathyHuangtw/gatsby-starter-default-i18next">here</a>. Any comments or feedbacks are welcome :)</p><p>Add another language on your website now in order to match your target audience’s language!</p><blockquote>If you talk to a man in a language he understands, that goes to his head. If you talk to him in his own language, that goes to his heart. -Nelson Mandela</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19d21f2b723d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Style Your Twitter Widget + Styling on Shadow DOM]]></title>
            <link>https://medium.com/@makerspirit/how-to-style-your-twitter-widget-styling-on-shadow-dom-a405c36edd10?source=rss-2c9acecd4185------2</link>
            <guid isPermaLink="false">https://medium.com/p/a405c36edd10</guid>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[dom]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Cathy H]]></dc:creator>
            <pubDate>Sat, 05 May 2018 06:20:44 GMT</pubDate>
            <atom:updated>2018-05-20T22:35:41.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*DpG_uekJnm8tyf0A_NqafA.jpeg" /><figcaption>The Magic Code</figcaption></figure><p>I was simply figuring out how to style the embedded Tweets on my web application and realized Twitter’s embedded Tweet display had switched from using &lt;iframe&gt; to shadow DOM. Also, the styling I wanted to apply could not be done by changing <a href="https://dev.twitter.com/web/embedded-tweets/parameters">embedded tweet parameters</a>. So I played with shadow DOM and finally removed the border line of the tweet widget to let it blend in with the web page. Here is a guide to help you to do the same with your embedded tweets!</p><p>Before we start working on the codes, here’s some important ideas I learned about shadow DOM.</p><ul><li>Style Encapsulation: The shadow boundary encapsulates the CSS styles defined inside shadow DOM for free, so the style rules don’t leak out. No more need for components’ author to collaborate in order for styles to be encapsulated. Other CSS rules defined on the page won’t bleed into the shadow DOM either.</li><li>Isolated DOM: Since a component’s DOM is self-contained, we can think of this app as having different pieces like that of a puzzle that make up chunks of DOM! This is great for building component-based apps.</li></ul><p><strong>Why Twitter made the change?</strong></p><p>Tweets display at a faster rate. In their <a href="https://twittercommunity.com/t/upcoming-change-to-embedded-tweet-display-on-web/66215">announcement</a>, they mentioned the change utilizes less browser memory which makes the rendering time faster.</p><p>One question leads to another. <strong>Why does Shadow DOM render faster than iframes?</strong></p><ul><li>Shadow DOM is basically an isolated DOM tree that gets attached to the original document. However, an embedded content created by &lt;iframe&gt; is a complete document environment that will be embedded inside&lt;body&gt; of your page. That means that the use of &lt;iframe&gt; needs a greater amount of memory as well as other computing resources required by the document.</li><li>A shadow DOM’s styles don’t need to look at the rest of the page’s styles, so they can be computed much quickly.</li></ul><p>Ok, we got the concepts. Let’s get the work done.</p><p>Goal: <strong>Remove the border line of this embedded tweet on my webpage.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*XDXIMckp-cWMejDVvtoR3Q.png" /></figure><p>Open your developer tool in Chrome, you will see the shadow DOM inserted under twitterwidget element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*_TzwFgug3na8EMTuq6i7NA.png" /></figure><p>Mode open allows us to access the shadow DOM using JavaScript written in the main page context.</p><p>To create a embedded Tweet and let it generated at runtime, we have to use the factory function twttr.widgets.createTweet from widgets-js (Js interfaces for Twitter for websites)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ae38c708c722b8f4b8d13abf1efb1e0/href">https://medium.com/media/0ae38c708c722b8f4b8d13abf1efb1e0/href</a></iframe><p>The third argument in this function provides us a few options to change the style of the widget. For example, theme can be light or dark, With the <a href="https://dev.twitter.com/web/javascript/creating-widgets#create-tweet">options</a>, I was able to toggle the color scheme of the embedded Tweet but I wanted to modify more style rules in it.</p><p>If you want to look for the Tweet’s class name and simply add style rules under the same class name on your stylesheet, it won’t work. Remember, CSS rules defined on the page don’t bleed into the shadow DOM.</p><p>Let’s find the root node of the shadow tree in order to apply whatever styles we would like for the embedded Tweets. We can make changes to the nodes in the shadow DOM in exactly the same way as non-shadow nodes. The concept of encapsulation is that none of the code inside shadow DOM can affect the elements outside of it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8435b9a9e95fedcd4d06b55cb07785b3/href">https://medium.com/media/8435b9a9e95fedcd4d06b55cb07785b3/href</a></iframe><p>The createTweet function returns a Promise. We can only style the element after the widget has been created. Once we have the widget element, we use the property .shadowRoot to get the shadow root hosted by the element. Now, you can simply use jQuery API to find the children elements and apply whatever styles you would like to see!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*eMq4Di7BazVrRU_7d-gK7g.png" /><figcaption>Yay! Border line removed.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*VwkJ0sTPBojrmCnKuBeiyg.png" /><figcaption>Enlarge the avatar icon!</figcaption></figure><p>Hope you find this article helpful. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a405c36edd10" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>