Tanvirul Islam

Blog

Customizing Time Display for NewsNest24: A Journey in Bangla Localization

In the digital age, user experience is everything. When I started developing NewsNest24.net, a Bangla news portal, one of the key requirements was to present time information in a way that resonates with our audience—displaying time in the “time ago” format, such as “১ ঘন্টা আগে” (1 hour ago), and switching to a standard date format after one day. This small but crucial feature enhances user experience by making the content more relatable and accessible for Bangla-speaking users.

The Challenge

Initially, I explored a few plugins to meet this requirement. The first plugin, Meks Time Ago, did a great job of displaying relative time but in English. I could modify some elements like “ago” to “আগে,” but the numbers and units (like hours and minutes) remained in English. The second plugin, Bangla Date Display, was perfect for converting standard dates into Bengali script but didn’t offer the “time ago” functionality.

To merge these functionalities, I knew I had to dive deeper into the plugin’s code.

Diving into the Code

I started by exploring the Meks Time Ago plugin. After some research, I realized that the function responsible for the “time ago” format was human_time_diff, located in the class.frontend.php file of the plugin. However, this only partially solved my problem as it still returned the time in English.

My next step was to locate and edit the human_time_diff function within the WordPress core file, formatting.php, which is found in the wp-includes directory. This function is pivotal as it calculates and formats the time difference between two dates.

Implementing Bangla Localization

To ensure the time was displayed in Bangla, I wrote a custom function called en2bn to convert English numbers into Bengali. I also replaced the original English time labels like “seconds,” “minutes,” and “hours” with their Bengali counterparts: “সেকেন্ড,” “মিনিট,” and “ঘণ্টা.”

Here’s how I did it:

function en2bn($input){
    $bn = ["১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০"];
    $en = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
    return str_replace($en, $bn, $input);
}
 
function human_time_diff( $from, $to = 0 ) {
	if ( empty( $to ) ) {
		$to = time();
	}

	$diff = (int) abs( $to - $from );

	if ( $diff < MINUTE_IN_SECONDS ) {
		$secs = $diff;
		if ( $secs <= 1 ) {
			$secs = 1;
		}
		$since = sprintf( _n( '%s সেকেন্ড', '%s সেকেন্ড', $secs ), $secs );
	} elseif ( $diff < HOUR_IN_SECONDS && $diff >= MINUTE_IN_SECONDS ) {
		$mins = round( $diff / MINUTE_IN_SECONDS );
		if ( $mins <= 1 ) {
			$mins = 1;
		}
		$since = sprintf( _n( '%s মিনিট', '%s মিনিট', $mins ), $mins );
	} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
		$hours = round( $diff / HOUR_IN_SECONDS );
		if ( $hours <= 1 ) {
			$hours = 1;
		}
		$since = sprintf( _n( '%s ঘণ্টা', '%s ঘণ্টা', $hours ), $hours );
	} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
		$days = round( $diff / DAY_IN_SECONDS );
		if ( $days <= 1 ) {
			$days = 1;
		}
		$since = sprintf( _n( '%s দিন', '%s দিন', $days ), $days );
	} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
		$weeks = round( $diff / WEEK_IN_SECONDS );
		if ( $weeks <= 1 ) {
			$weeks = 1; 
		}
		$since = sprintf( _n( '%s সপ্তাহ', '%s সপ্তাহ', $weeks ), $weeks );
	} elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
		$months = round( $diff / MONTH_IN_SECONDS );
		if ( $months <= 1 ) {
			$months = 1;
		}
		$since = sprintf( _n( '%s মাস', '%s মাস', $months ), $months );
	} elseif ( $diff >= YEAR_IN_SECONDS ) {
		$years = round( $diff / YEAR_IN_SECONDS );
		if ( $years <= 1 ) {
			$years = 1;
		}
		$since = sprintf( _n( '%s বছর', '%s বছর', $years ), $years );
	}
	
	$since = en2bn($since);
	return $since . ' আগে';
}

The Result

After implementing this solution, the time display on NewsNest24.net now shows relative time in Bangla, just as the client required. The website now effectively communicates with its audience, providing a seamless and localized experience.

Final Thoughts

Customizing plugins and tweaking WordPress core files can be daunting, but it’s also rewarding when you can deliver exactly what the client wants. This project taught me a lot about localization and how even small changes can significantly impact user experience.

As a developer, it’s essential to remain adaptable and willing to dive into the code to achieve the desired result. The journey of building NewsNest24.net has been both challenging and fulfilling, and I look forward to what’s next.

Tag Post :
Share This :

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *