catamphetamine / javascript-time-ago

International highly customizable relative date/time formatting

Home Page:https://catamphetamine.gitlab.io/react-time-ago/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting this error `No locale data has been registered for any of the locales: en, en`

agupta93 opened this issue · comments

Was trying to use your react-time-ago component and ran into the above error.
usage:
<TimeAgo className="postHeader__timeAgo" >{props.time}</TimeAgo>

I was under the impression that it will take in 'en' as the default locale.
After a bit of debugging found the issue in javascript-time-ago

in JavascriptTimeAgo.js.

static locales = {}

	/**
	 * @param {(string|string[])} locales=[] - Preferred locales (or locale).
	 */
	constructor(locales = [])
	{
		// Convert `locales` to an array.
		if (typeof locales === 'string')
		{
			locales = [locales]
		}

		// Choose the most appropriate locale
		// (one of the previously added ones)
		// based on the list of preferred `locales` supplied by the user.
		this.locale = choose_locale
		(
			locales.concat(JavascriptTimeAgo.default_locale),
			JavascriptTimeAgo.locales
		)
	}

JavascriptTimeAgo.locales is an empty object

export default function choose_locale(locales, registered_locales)
{
	// This is not an intelligent algorythm,
	// but it will do for this library's case.
	// `sr-Cyrl-BA` -> `sr-Cyrl` -> `sr`.
	for (let locale of locales)
	{
		if (registered_locales[locale])
		{
			return locale
		}

		const parts = locale.split('-')
		while (parts.length > 1)
		{
			parts.pop()
			locale = parts.join('-')
			if (registered_locales[locale])
			{
				return locale
			}
		}
	}

	throw new Error(`No locale data has been registered for any of the locales: ${locales.join(', ')}`)
}

in the above function in locales.js resgtered locales in always empty so registered_locales[locale] is always undefined.

Hence the above error can anybody help me with the fix.

Why didn't you read react-time-ago readme before using it.
It has instructions to add locales before using it:

image

I updated react-time-ago readme with more expanation of the setup process.

I think this is pretty confusing. As to manually add locales from a dependency. One would expect naturally not to have such a setup before using a component. at least for default. For some custom locale support it makes sense. and also the readme was not much clearer earlier. Thanks for the update though :)

Adding all locales by default is not an option because that would increase the bundle size by 600 KB.