i18n.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. if(lan !== undefined){
  2. var syslang = navigator.language;
  3. var ContainsNonTagElement = [];
  4. $.each($("*"), function( index, value ) {
  5. //to check if the element was the script or style, if it is script or style then skip it
  6. if($(value)[0].tagName.toUpperCase() !== "SCRIPT" && $(value)[0].tagName.toUpperCase() !== "STYLE"){
  7. //if the element doesn't contains any children element
  8. if(!$(value).children().length){
  9. //check if it is textbox, if true then show placeholder
  10. if($(value).attr("placeholder") !== undefined){
  11. var t = $(value).attr("placeholder").trim();
  12. if(t.length > 0){
  13. $(value).attr("placeholder",intl_convert(syslang,t));
  14. }
  15. }
  16. //if it is normal text, display the text
  17. if($(value).html() !== undefined){
  18. var t = $(value).text().trim();
  19. if(t.length > 0){
  20. $(value).html($(value).html().replace(t,intl_convert(syslang,t)));
  21. }
  22. }
  23. }else{
  24. //if it still contains some child element, then try to remove all element and check if there contains any text
  25. //if we find any, then store it and process it later.
  26. var t = $(value).clone().children().remove().end().text().trim();
  27. if(t.length > 0){
  28. ContainsNonTagElement.push($(value))
  29. }
  30. }
  31. }
  32. });
  33. $.each(ContainsNonTagElement, function( index, value ) {
  34. var t = $(value).clone().children().remove().end().text().trim();
  35. if(t.length > 0){
  36. $(value).html($(value).html().replace(t,intl_convert(syslang,t)));
  37. }
  38. });
  39. }else{
  40. throw "No language file was find, include variable lan and try again.";
  41. }
  42. function intl_convert(lang,t){
  43. if(lan == undefined){
  44. throw "No language file was find, include variable lan and try again.";
  45. }
  46. var r = new RegExp("[0-9,.]+");
  47. var num_part = t.match(r);
  48. if(num_part == null){
  49. num_part = "";
  50. }
  51. var text_part = t.replace(r,"");
  52. if(lan[lang][t.replace(r,"%n")] !== undefined){
  53. var convertedtext = lan[lang][t.replace(r,"%n")].replace("%n",num_part);
  54. }else{
  55. var convertedtext = t;
  56. }
  57. console.log("Number part: " + num_part + "\nText part: " + text_part + "\nInput text: " + t + "\nProcessed text: " + t.replace(r,"%n") + "\nConverted: " + convertedtext);
  58. return convertedtext;
  59. }