JQuery Timeago timestamps: how to choose a language?

I am sure this is a stupid question, but I really don't know how to do this.

I am using the timeago jquery plugin: http://timeago.yarp.com/

And it supports locale for multiple languages.

I translated my site from English to Spanish, I use PHP for this. Therefore, I would like to use timeago in both languages.

I found these lines for several languages: https://gist.github.com/6251

But how to use them?

Thanks!

+9
source share
4 answers

You can get the Spanish .js file for timeago here: https://gist.github.com/6251 .

Specify the correct .js file on your website where you upload the English or Spanish page.

Those. in page_spanish.html file:

<script type="text/javascript" src="timeago_spanish.js"></script> 

Those. in page_english.html file:

 <script type="text/javascript" src="timeago_english.js"></script> 

Or if you do this through a single PHP script, switch to a variable:

 <?php if $page_lang = 'SPANISH' then echo '<script type="text/javascript" src="timeago_spanish.js"></script>'; else if $page_lang = 'ENGLISH' then echo '<script type="text/javascript" src="timeago_english.js"></script>'; ?> 
+9
source

Im using this code.

main.js

 if (typeof($.timeago) != "undefined") { jQuery.timeago.settings.strings = { prefixAgo: "hace", prefixFromNow: "dentro de", suffixAgo: "", suffixFromNow: "", seconds: "menos de un minuto", minute: "un minuto", minutes: "unos %d minutos", hour: "una hora", hours: "%d horas", day: "un día", days: "%d días", month: "un mes", months: "%d meses", year: "un año", years: "%d años" }; } 

from this link

+1
source

If you use npm and install timeago with it, then you can import the local file from node_modules , you can try the following for french :

 import fr from "timeago/locales/jquery.timeago.fr"; 
+1
source

You can refer to different jquery.timeago.js if there is one page per language, but this is static. To do this dynamically, you need to specify the jquery.timeago.js link on your page, at the end of your main or main file (or on the page that you use this library) you extract the $ .timeago object and change the field settings according to the flag your language:

 if (typeof($.timeago) != "undefined") { jQuery.timeago.settings.strings = { prefixAgo: null, prefixFromNow: null, suffixAgo: "il ya", suffixFromNow: "d'ici", seconds: "moins d'une minute", minute: "environ une minute", minutes: "environ %d minutes", hour: "environ une heure", hours: "environ %d heures", day: "environ un jour", days: "environ %d jours", month: "environ un mois", months: "environ %d mois", year: "un an", years: "%d ans", wordSeparator: " ", numbers: [] }; }; 

}

0
source

All Articles