<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Toru</title>
    <description>The latest articles on DEV Community by Toru (@toru).</description>
    <link>https://dev.to/toru</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F8786%2F1fb31470-beef-49f4-a27f-c885384bdb2c.jpeg</url>
      <title>DEV Community: Toru</title>
      <link>https://dev.to/toru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toru"/>
    <language>en</language>
    <item>
      <title>Learning GSAP with a Day-to-Night Scroll Animation 🌞🌜</title>
      <dc:creator>ifrah</dc:creator>
      <pubDate>Wed, 09 Oct 2024 10:53:43 +0000</pubDate>
      <link>https://dev.to/toru/learning-gsap-with-a-day-to-night-scroll-animation-2j48</link>
      <guid>https://dev.to/toru/learning-gsap-with-a-day-to-night-scroll-animation-2j48</guid>
      <description>&lt;p&gt;We’ve been exploring GSAP (GreenSock Animation Platform) lately and wanted to share a breakdown of a recent project. If you’re new to GSAP, this project is a great way to get started with scroll-based animations!&lt;/p&gt;

&lt;h2&gt;
  
  
  What We’re Building
&lt;/h2&gt;

&lt;p&gt;We’ll create a scroll-driven day-to-night transition using SVG elements. The animation will include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sun and Moon&lt;/strong&gt; following a curved path as you scroll.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sky&lt;/strong&gt; changing colours to simulate day and night.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stars&lt;/strong&gt; appearing as the night sets in.&lt;/li&gt;
&lt;li&gt;Text changes that animate based on the time of day.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. Setting Up the HTML and CSS&lt;/strong&gt;&lt;br&gt;
First, let’s set up a basic HTML structure. We’ll include SVG elements for the sun, moon, and a path they’ll follow. We’ll also add a sky background and some stars.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;section class="container"&amp;gt;
  &amp;lt;!-- Background sky div that changes color to simulate day and night --&amp;gt;
  &amp;lt;div class="sky"&amp;gt;&amp;lt;/div&amp;gt;

  &amp;lt;!-- SVG containing the sun, moon, and the path they will follow --&amp;gt;
  &amp;lt;svg class="circle-svg" viewBox="0 0 550 300"&amp;gt;
    &amp;lt;!-- A path that defines the trajectory for the sun and moon animations --&amp;gt;
    &amp;lt;path class="circle-path" d="M 550 300 a 10 10 0 0 0 -550 0" fill="transparent"/&amp;gt;
    &amp;lt;!-- The sun element, initially positioned at (40, 40) --&amp;gt;
    &amp;lt;circle fill="#FDCA48" class="sun" cx="40" cy="40" r="40"&amp;gt;&amp;lt;/circle&amp;gt;
    &amp;lt;!-- The moon element, also initially positioned at (40, 40) --&amp;gt;
    &amp;lt;circle fill="#FFEFC4" class="moon" r="40" cx="40" cy="40"&amp;gt;&amp;lt;/circle&amp;gt;
  &amp;lt;/svg&amp;gt;

  &amp;lt;!-- Container for stars that appear during the night --&amp;gt;
  &amp;lt;div class="stars"&amp;gt;
    &amp;lt;!-- Individual star elements that are hidden initially --&amp;gt;
    &amp;lt;div class="star"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="star"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="star"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="star"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="star"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;!-- "HELLO WORLD!" text displayed during the day --&amp;gt;
  &amp;lt;h1 class="hello-world"&amp;gt;&amp;lt;span&amp;gt;HELLO WORLD!&amp;lt;/span&amp;gt;&amp;lt;/h1&amp;gt;

  &amp;lt;!-- "BYE WORLD!" text displayed during the night --&amp;gt;
  &amp;lt;h1 class="bye-world"&amp;gt;&amp;lt;span&amp;gt;BYE WORLD!&amp;lt;/span&amp;gt;&amp;lt;/h1&amp;gt;

  &amp;lt;!-- Grass element that stays at the bottom of the screen --&amp;gt;
  &amp;lt;div class="grass"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/section&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, some basic CSS to style our elements. We position the sky, stars, sun, and moon and set up classes for our text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html, body, * {
  margin: 0;
  padding: 0;
}

.container {
  height: 100vh;

  .sky {
    background-color: #B7F2FF;
    height: 100vh;
    width: 100%;
    position: fixed;
    top: 0;
  }

  .sun, .moon {
    opacity: 0;
  }

  .stars {
    position: fixed;
    top: 0;
    height: 80vh;
    width: 100%;

    .star {
      background-color: #FFEFC4;
      border-radius: 100px;
      display: none;
      height: 15px;
      width: 15px;
      position: absolute;
      z-index: 10;
      animation: pulsate 2s linear infinite;

      &amp;amp;:nth-child(1) {
        top: 20%;
        left: 5%;
        transform: translate(-5%, -20%);
      }

      &amp;amp;:nth-child(2) {
        display: none;
        top: 67%;
        left: 20%;
        transform: translate(-20%, -67%);
      }

      &amp;amp;:nth-child(3) {
        display: none;
        top: 35%;
        left: 37%;
        transform: translate(-37%, -35%);
      }

      &amp;amp;:nth-child(4) {
        display: none;
        top: 75%;
        left: 70%;
        transform: translate(-70%, -75%);
      }

      &amp;amp;:nth-child(5) {
        display: none;
        top: 30%;
        left: 90%;
        transform: translate(-30%, -90%);
      }
    }
  }

  h1 {
    display: inline-block;
    font-family: "Inter", sans-serif;
    font-size: 0;
    font-weight: 400;
    position: fixed;
    top: 68%;
    left: 50%;
    transform: translate(-50%, -68%);
    opacity: 0;
    white-space: nowrap;

    span {
      font-size: 50px;
    }

    &amp;amp;.hello-world {
      color: #000;
    }

    &amp;amp;.bye-world {
      color: #fff;
    }
  }

  .grass {
    background-color: #065C19;
    height: 20vh;
    width: 100%;
    position: fixed;
    bottom: 0;
  }
}

.fade-in {
  animation: fadeIn 0.25s forwards;
}

.fade-out {
  animation: fadeOut 0.25s forwards;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  } 100% {
    opacity: 1;
  }
}

@keyframes fadeOut {
  0% {
    opacity: 1;
  } 100% {
    opacity: 0;
  }
}

@keyframes pulsate {
  0% {
    opacity: 100%;
  }
  50% {
    opacity: 25%;
    scale: 0.9;
  }
  100% {
    opacity: 100%
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Bringing the Animation to Life with GSAP
&lt;/h2&gt;

&lt;p&gt;Now that we have the structure in place, let’s animate it! We’ll use GSAP’s &lt;code&gt;ScrollTrigger&lt;/code&gt; and &lt;code&gt;MotionPathPlugin&lt;/code&gt; to animate the sun and moon based on scroll position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gsap.registerPlugin(ScrollTrigger, MotionPathPlugin, DrawSVGPlugin);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to register the plugins we’re using: &lt;code&gt;ScrollTrigger&lt;/code&gt; for scroll-based animations, &lt;code&gt;MotionPathPlugin&lt;/code&gt; for moving elements along paths, and &lt;code&gt;DrawSVGPlugin&lt;/code&gt; to animate SVG paths.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Setting Up the Scroll Trigger and Timeline
&lt;/h2&gt;

&lt;p&gt;The core of the animation happens in our timeline (&lt;code&gt;tl2&lt;/code&gt;). We set up &lt;code&gt;ScrollTrigger&lt;/code&gt; to pin the SVG element and control the animation based on scroll position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var tl2 = gsap.timeline({
  scrollTrigger: {
    trigger: ".circle-svg",
    scrub: 1, // Smooth scrolling effect
    pin: true, // Pin the element during scroll
    start: "center center", // Start when the SVG reaches the center of the viewport
    end: "+=3000", // Animation length
  }
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;trigger&lt;/code&gt;: The SVG that controls when the animation starts.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;scrub&lt;/code&gt;: Links animation progress to the scroll position.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pin&lt;/code&gt;: Keeps the SVG in place as the user scrolls.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt;: Define when the animation starts and how long it lasts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Animating the Sun and Moon
&lt;/h3&gt;

&lt;p&gt;Next, we move the sun along the path using the &lt;code&gt;motionPath&lt;/code&gt; property. This makes the sun follow the curve we set in the SVG path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tl2.from(".circle-path", {drawSVG: 0}) // Draw the path as we scroll
    .to(".sun", {
        motionPath: {
            path: ".circle-path", // The path to follow
            align: ".circle-path", // Align the element along the path
            alignOrigin: [0.5, 0.25], // Adjust alignment
            start: 0,
            end: 0.2, // Moves the sun a small distance initially
        },
        duration: 0.2
    })

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what’s happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The path is drawn with &lt;code&gt;drawSVG&lt;/code&gt; as the user scrolls.&lt;/li&gt;
&lt;li&gt;The sun moves a small distance along the path to simulate sunrise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We then continue the animation by moving the sun further along the path, changing the sky’s color, and gradually revealing stars.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.to(".sky", { backgroundColor: "#7A3BFF" }) // Change sky color to night
.to(".star:nth-child(3)", { display: "block" }, 2) // Show stars one by one
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Handling the Moon and Text Animations
&lt;/h3&gt;

&lt;p&gt;Once the sun sets, the moon rises following the same path, and the text changes based on scroll direction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.to(".moon", {
  motionPath: {
    path: ".circle-path",
    align: ".circle-path",
    alignOrigin: [0.5, 0.25],
    start: 0,
    end: 0.2
  },
  duration: 0.2
})
.add(() =&amp;gt; {
  if (tl2.scrollTrigger.direction === -1) {
    byeAnimation.reverse();
  } else {
    byeAnimation.play();
  }
}, "&amp;lt;");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The moon animation is set up similarly to the sun. The timeline checks the scroll direction to determine which text message to display, playing or reversing the animation based on the scroll.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Touches
&lt;/h2&gt;

&lt;p&gt;We tie everything together by animating additional stars, adding a grass element that rises, and fine-tuning timing and effects. It’s all about playing around with GSAP’s powerful timeline and animation controls&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;This project was a fun way to get familiar with GSAP’s capabilities. If you’re trying it out, start small and experiment with different properties and plugins. It’s amazing how much you can create with just a few lines of code!&lt;/p&gt;

&lt;p&gt;Feel free to ask questions if you want to learn more about GSAP or how I set up the animations. For more industry insights and the latest updates from our agency, be sure to visit &lt;a href="https://www.toru.digital/" rel="noopener noreferrer"&gt;Toru Digital's Insights&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See demo &lt;a href="https://codepen.io/Megan240/pen/poBvzrq" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gsap</category>
      <category>animation</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How BIMI Puts Your Brand Front and Center in Every Inbox 📧</title>
      <dc:creator>ifrah</dc:creator>
      <pubDate>Fri, 16 Aug 2024 09:29:30 +0000</pubDate>
      <link>https://dev.to/toru/how-bimi-puts-your-brand-front-and-center-in-every-inbox-4042</link>
      <guid>https://dev.to/toru/how-bimi-puts-your-brand-front-and-center-in-every-inbox-4042</guid>
      <description>&lt;p&gt;Getting your emails noticed has become incredibly difficult, with the average person juggling hundreds of emails each day. So, how do you make yourself stand out from the crowd? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/f0TvnEmF5yPLO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/f0TvnEmF5yPLO/giphy.gif" width="312" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's where BIMI (Brand Indicators for Message Identification) comes into play. It is a relatively new tool that helps your brand stand out while also providing some security. In this post, I'll be guiding you through how to get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly is BIMI?
&lt;/h2&gt;

&lt;p&gt;BIMI stands for Brand Indicators for Message Identification which is simply a way for you to display your brand's logo next to your emails in supported inboxes. &lt;/p&gt;

&lt;p&gt;The primary benefits of BIMI include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Instant Brand Recognition:&lt;/strong&gt; Your logo makes your emails stand out in crowded inboxes, helping recipients quickly identify communications from your brand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Increased Trust and Credibility:&lt;/strong&gt; By verifying your identity through BIMI's authentication process, you differentiate yourself from spammers and untrustworthy senders, boosting open rates and engagement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Email Deliverability:&lt;/strong&gt; While BIMI itself doesn't directly impact deliverability, it encourages strong email authentication practices like SPF, DKIM, and DMARC, which are crucial for reaching inboxes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How Does BIMI work?
&lt;/h2&gt;

&lt;p&gt;To get your logo showing up via BIMI, you need to ensure your emails are authenticated using protocols like SPF, DKIM, and DMARC. BIMI then builds on that by letting you associate your verified logo with your domain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Design Your Brand Logo
&lt;/h3&gt;

&lt;p&gt;Your brand logo must be in SVG (Scalable Vector Graphics) format and meet specific guidelines. It should be a square aspect ratio, static (no animations), and visually clear even at smaller sizes. This is what will be displayed in supported inboxes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Your Logo Verified
&lt;/h3&gt;

&lt;p&gt;Some email providers require you to verify your logo through a Verified Mark Certificate (VMC). This step involves working with a third-party certificate authority that checks your brand's authenticity and issues the certificate. This extra layer of verification helps prevent abuse and ensures that only authorised parties can leverage BIMI for a particular brand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add BIMI DNS Record:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/BX5XTEo5WX5D5mrU18/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/BX5XTEo5WX5D5mrU18/giphy.gif" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have your logo designed and verified, the next step is to add a BIMI DNS record to your domain. This record tells email clients where to find your brand's logo and includes any relevant verification information.&lt;/p&gt;

&lt;p&gt;The BIMI DNS record is a TXT record that follows a specific format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default._bimi.yourdomain.com IN TXT "v=BIMI1; l=https://yourdomain.com/logo.svg; a=https://yourdomain.com/vmc.pem"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the different components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;default._bimi.yourdomain.com:&lt;/strong&gt; This is the subdomain where the BIMI record is located. The default part indicates that this is the default BIMI record for your domain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;v=BIMI1:&lt;/strong&gt; This specifies the version of the BIMI specification you're using.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;l=&lt;a href="https://yourdomain.com/logo.svg:" rel="noopener noreferrer"&gt;https://yourdomain.com/logo.svg:&lt;/a&gt;&lt;/strong&gt; This is the URL where your brand's logo (in SVG format) is hosted. Make sure this URL is publicly accessible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;a=&lt;a href="https://yourdomain.com/vmc.pem:" rel="noopener noreferrer"&gt;https://yourdomain.com/vmc.pem:&lt;/a&gt;&lt;/strong&gt; This is the URL where your Verified Mark Certificate (VMC) is hosted. This component is optional, but it's required by some email providers to verify your brand's authenticity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding this BIMI record to your DNS is crucial for email clients to recognise and display your brand's logo in supported inboxes. The process for adding the record may vary depending on your DNS provider or hosting platform, but it typically involves accessing your domain's DNS settings and creating a new TXT record with the BIMI information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Monitor and Optimise
&lt;/h3&gt;

&lt;p&gt;After setting up BIMI, keep an eye on how your emails are displaying across different inboxes and email clients. You may need to make adjustments to ensure your logo is rendering correctly.&lt;/p&gt;

&lt;p&gt;By following these steps and meeting the necessary requirements, you can implement BIMI and have your brand logo displayed prominently in supported email inboxes, enhancing your brand recognition and building trust with recipients.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Few Things to Consider
&lt;/h2&gt;

&lt;p&gt;As BIMI continues to gain traction and more email providers recognise its benefits, we can expect broader support in the future. But for the time being, it’s not yet universally supported.As of now, major email services like Gmail, Yahoo Mail, and Fastmail have adopted BIMI, allowing users to see brand logos in the inbox when emails are received from BIMI-compliant senders. Also, getting a Verified Mark Certificate can be a bit of a process and might involve some costs, so it’s something to weigh up.&lt;/p&gt;

&lt;p&gt;If you’re serious about making an impact, investing a little time in setting up BIMI could pay off big time.It gives your brand a unique edge by making your emails stand out visually and by building trust right from the inbox. &lt;/p&gt;

&lt;p&gt;We'd love to hear your thoughts and experiences on implementing BIMI, so please feel free to leave a comment below. For more industry insights and the latest updates from our agency, be sure to visit &lt;a href="https://www.toru.digital/insights" rel="noopener noreferrer"&gt;Toru Digital's Insights&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>career</category>
      <category>email</category>
      <category>tutorial</category>
      <category>bimi</category>
    </item>
    <item>
      <title>Integrating Google Translate API with Yii2</title>
      <dc:creator>ifrah</dc:creator>
      <pubDate>Sun, 09 Jun 2024 17:57:40 +0000</pubDate>
      <link>https://dev.to/toru/integrating-google-translate-api-with-yii2-c7o</link>
      <guid>https://dev.to/toru/integrating-google-translate-api-with-yii2-c7o</guid>
      <description>&lt;p&gt;In this post, I'll be sharing how to integrate the Google Translate API with Yii2. As developers, we often encounter challenges when building multilingual sites with translation capabilities. Whether we're manually adding them or relying on a CMS, the process can be incredibly time-consuming and tedious.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/QsY8yp5q4atcQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/QsY8yp5q4atcQ/giphy.gif" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although it is not perfect, the Google Translate API has a high accuracy level, comprehensive language support and is constantly being updated to ensure relatability. Hopefully by the end of this guide, you'll be able to incorporate Google Translate into your Yii2 application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Google Translate API Configuration to Yii2
&lt;/h2&gt;

&lt;p&gt;Before we start translating text, we need to configure the Google Translate API in our Yii2 application. Follow these steps to set up the API configuration:&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Obtain Your API Key
&lt;/h3&gt;

&lt;p&gt;If you haven't already, refer to the previous post on setting up a Google Cloud Project and enabling the Google Translate API. Once you have your API key, proceed with the following steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Configure Yii2 to Use the API Key
&lt;/h3&gt;

&lt;p&gt;Open your Yii2 configuration file (common/config/main.php) and add the API key to the params section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`&amp;lt;?php

return [
    'params' =&amp;gt; [
        'google' =&amp;gt; [
            'translate' =&amp;gt; [
                'api_key' =&amp;gt; 'YOUR_API_KEY_HERE', // Replace with your actual API key
                'enabled' =&amp;gt; true,
            ],
        ],
    ],
    // Other configurations
];`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the API to Translate Text
&lt;/h2&gt;

&lt;p&gt;Now that we've configured our application to use the Google Translate API, let's create a function that uses the API to translate text. We'll create a new component to encapsulate this functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create the Translate Component
&lt;/h3&gt;

&lt;p&gt;Create a new file components/GoogleTranslate.php and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace common\components;

use Yii;
use yii\base\Component;

class GoogleTranslate extends Component {

    /**
     * Translates text using the Google Translate API.
     *
     * @param string $text The text to be translated.
     * @param string $targetLanguage The language to translate the text into.
     * @param string $sourceLanguage The language of the text to be translated (default: 'en').
     * @return string The translated text.
     * @throws \Exception If Google Translate API is not enabled or if an error occurs during translation.
     */
    public function translate($text, $targetLanguage, $sourceLanguage = 'en') {
        // Check if Google Translate API is enabled
        if (!Yii::$app-&amp;gt;params['google']['translate']['enabled']) {
            throw new \Exception("Google Translate is not enabled.");
        }

        // Get the API key from Yii2 application parameters
        $apiKey = Yii::$app-&amp;gt;params['google']['translate']['api_key'];
        // Construct the API request URL
        $url = "https://translation.googleapis.com/language/translate/v2?key={$apiKey}";

        // Prepare data for the API request
        $data = [
            'q' =&amp;gt; $text,
            'source' =&amp;gt; $sourceLanguage,
            'target' =&amp;gt; $targetLanguage,
            'format' =&amp;gt; 'text',
        ];

        // Initialize a cURL session
        $ch = curl_init();
        // Set cURL options for the API request
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Execute the cURL request and get the response
        $response = curl_exec($ch);
        // Close the cURL session
        curl_close($ch);

        // Process the API response
        return $this-&amp;gt;handleResponse($response);
    }

    /**
     * Handles the response received from the Google Translate API.
     *
     * @param string $response The API response in JSON format.
     * @return string The translated text.
     * @throws \Exception If an error occurs while processing the response.
     */
    private function handleResponse($response) {
        // Decode the JSON response into an associative array
        $response = json_decode($response, true);

        // Check if decoding was successful
        if ($response === null) {
            throw new \Exception("Failed to decode JSON response.");
        }

        // Check if the response contains an error message
        if (isset($response['error'])) {
            // Extract the error message
            $errorMessage = $response['error']['message'];
            // Throw an exception indicating a Google Translate API error
            throw new \Exception("Google Translate API error: {$errorMessage}");
        }

        // Return the translated text extracted from the response data
        return $response['data']['translations'][0]['translatedText'];
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component defines a translate method that sends a translation request to the Google Translate API and a handleResponse method that processes the API response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Register the Component
&lt;/h3&gt;

&lt;p&gt;Open your Yii2 configuration file (common/config/main.php) and register the GoogleTranslate component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

return [
    'components' =&amp;gt; [
        'googleTranslate' =&amp;gt; [
            'class' =&amp;gt; 'common\components\GoogleTranslate',
        ],
    ],
    // Other configurations
];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yii2 follows the dependency injection design pattern, where component can be injected into other classes and componentes when needed. So by doing this, we enable Yii2 to automatically inject an instance of &lt;code&gt;GoogleTranslate&lt;/code&gt; into classes that require the translation functionality. &lt;/p&gt;

&lt;h2&gt;
  
  
  Handling API Responses and Errors
&lt;/h2&gt;

&lt;p&gt;Handling API responses and errors is crucial to ensure a smooth user experience and to debug issues effectively. Let's look at how the GoogleTranslate component handles responses and errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Response Handling
&lt;/h3&gt;

&lt;p&gt;The handleResponse method decodes the JSON response and checks for errors. If the response contains a translation, it returns the translated text. If there is an error, it throws an exception with a detailed error message.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;Here are a few common scenarios and how to handle them:&lt;/p&gt;

&lt;p&gt;Invalid API Key: Ensure your API key is correct and has the necessary permissions. If the API key is invalid, Google will return an error which the handleResponse method will catch and throw as an exception.&lt;/p&gt;

&lt;p&gt;API Quota Exceeded: Google Translate API has usage limits. If you exceed these limits, you'll receive an error response. Consider implementing retry logic or monitoring usage to prevent exceeding quotas.&lt;/p&gt;

&lt;p&gt;Network Issues: If there's a network issue, curl_exec might fail. Ensure you handle such cases gracefully, possibly with retries or alternative actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;Let's see how to use the GoogleTranslate component in a controller to translate text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace frontend\controllers;

use Yii;
use yii\web\Controller;

class TranslateController extends Controller {

    public function actionIndex() {
        try {
            $text = 'Hello, world!';
            $targetLanguage = 'es'; // Spanish
            $translatedText = Yii::$app-&amp;gt;googleTranslate-&amp;gt;translate($text, $targetLanguage);
            return $this-&amp;gt;render('index', ['translatedText' =&amp;gt; $translatedText]);
        } catch (\Exception $e) {
            Yii::error($e-&amp;gt;getMessage(), __METHOD__);
            return $this-&amp;gt;render('error', ['message' =&amp;gt; $e-&amp;gt;getMessage()]);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the actionIndex method, we use the googleTranslate component to translate "Hello, world!" into Spanish. If an error occurs, it is caught, logged, and an error message is displayed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following this guide, you can add automated translation capabilities to your Yii2 application. This enhances your applications ability to support multiple languages and reach a broader audience.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fampv3b27yx59ayha3p5j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fampv3b27yx59ayha3p5j.png" alt="summary of article infographic" width="800" height="1014"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have plans to make this into a mini series, where we'll be next exploring how to create and run translation jobs in Yii2. This will allow for asynchronous and scalable translation processing.&lt;/p&gt;

&lt;p&gt;Resources:&lt;br&gt;
&lt;a href="https://cloud.google.com/translate/docs/reference/libraries/v2/php"&gt;Google PHP Client Library Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n"&gt;Yii2 Internationalization Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container"&gt;Dependency Injection Container&lt;/a&gt;&lt;/p&gt;

</description>
      <category>yii2</category>
      <category>php</category>
      <category>googlecloud</category>
      <category>programming</category>
    </item>
    <item>
      <title>What Are Some of Your Favourite Visual Studio Code Extensions?</title>
      <dc:creator>ifrah</dc:creator>
      <pubDate>Sat, 04 May 2024 16:25:49 +0000</pubDate>
      <link>https://dev.to/toru/what-are-some-of-your-favourite-visual-studio-code-extensions-5437</link>
      <guid>https://dev.to/toru/what-are-some-of-your-favourite-visual-studio-code-extensions-5437</guid>
      <description>&lt;p&gt;Hi everyone!&lt;/p&gt;

&lt;p&gt;So I've been using co-pilot for the nearly a year and it's been a real game changer for me. I definitely feel more productive and work quicker. Are there any other extensions or tools that you've come across and found to be particularly useful?&lt;/p&gt;

&lt;p&gt;Since my projects span across different languages and frameworks, I'm all ears for recommendations on extensions for anything.&lt;/p&gt;

&lt;p&gt;The extensions I'm currently using are below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiohp9v6infsw54f6f2zx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiohp9v6infsw54f6f2zx.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
    </item>
    <item>
      <title>5 Underrated resources to learn Git and Github</title>
      <dc:creator>ifrah</dc:creator>
      <pubDate>Fri, 19 Nov 2021 13:15:09 +0000</pubDate>
      <link>https://dev.to/toru/5-underrated-resources-to-learn-git-and-github-4edi</link>
      <guid>https://dev.to/toru/5-underrated-resources-to-learn-git-and-github-4edi</guid>
      <description>&lt;p&gt;&lt;strong&gt;Save you time and use these resources to perfect your Git and GitHub knowledge.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This week, I finally built up the courage to deep dive into learning Git and GitHub without having to relying on GUI and using the command line. We are fortunate enough to have unlimited resources available to us at the click of the button. However, this can quickly become overwhelming and result in spending more time browsing articles and tutorials instead of actually learning! I have curated this list with hopes that time and energy can be saved.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.&lt;a href="https://profy.dev/project/github-minesweeper" rel="noopener noreferrer"&gt;Github Minesweeper[Profy.dev - Free]&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This is one of the best free courses to actively learn Git through playing a game with a bot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchhjbdn3pgttuy2qmfd0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchhjbdn3pgttuy2qmfd0.png" alt="screenshot of the website profy.dev"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is a hand-on learning experience which helps prepare your Git skills for a job in a real-world team. It combines practice, theory and repetition to create the best learning environment to allow all the skills you learn to stick. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;a href="https://www.udacity.com/course/version-control-with-git--ud123" rel="noopener noreferrer"&gt;Version Control with Git[Udacity - Free]&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This is a Udacity course which covers the essentials of using the version control system Git which takes approximately 4 weeks on average to complete. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft5lboy0zuw89lwz7moij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft5lboy0zuw89lwz7moij.png" alt="Screenshot of udacity Git course landing page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This course teaches you up to date skills using self-paced learning and interactive quizzes and tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.&lt;a href="https://hacktoberfest.digitalocean.com/" rel="noopener noreferrer"&gt;Hacktoberfest&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Every October, Hacktoberfest takes place and is an event which encourages participation in the open source community with prices upon completion. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfduqoktc7nypjx51i4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfduqoktc7nypjx51i4w.png" alt="Screenshot of Hacktoberfest landing page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Highly recommended for beginners who wish to put into practice their Git skills in real world projects. Notably, most beginner repositories have step by step instructions of how to contribute to their projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.&lt;a href="https://ohmygit.org/" rel="noopener noreferrer"&gt;Oh My Git!&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Oh My Git! is an open source game about learning Git created for complete beginners. It is fun and interactive which is a refreshing break from the usual theoretical courses found online.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fku55efim76rhrgtzv2rc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fku55efim76rhrgtzv2rc.png" alt="Screenshot of Oh My Git! Gameplay"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It uses card game mechanics tasking players to work on repositories with others and fixing mistakes. It uses a mix of arrows and command cards to illustrate how changes in repositories flow. &lt;/p&gt;

&lt;h2&gt;
  
  
  5.&lt;a href="https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/git-basics" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The Odin Project collects the best resources to supplement your learning and present them in a logical order. In their Git Basics course provide clear step by step instructions are given to complete assignments as well as knowledge checks and cheatsheets throughout the course. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ed304johfle98fcrv8s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ed304johfle98fcrv8s.png" alt="Screenshot of The Odin Project page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Odin Project also has a community via discord chatrooms which provides additional help and motivation.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
