0

I have this javascript but everytime i save the language on my site that add on the final another "0" example "italiano" if i save my profile much more one time the script add another "0" like that "Italianoo" and if i save again "Italianooo"

How y can fix that ? also without the dictionary

Many thanks

var dictionary = {
  "Afrikanns": "Africano",
  "Albanian": "Albanese",
  "Arabic": "Arabo",
  "Armeno": "Armenian",
  "Basque": "Basco",
  "Bengali": "Bengalese",
  "Bielorussian": "Bielorusso",
  "Bulgarian": "Bulgaro",
  "Catalan": "Catalano",
  "Cambodian": "Cambogiano",
  "Czech": "Ceco",
  "Cinese": "Cinese",
  "Korean": "Coreano",
  "Croatian": "Croato",
  "Danish": "Danese",
  "Hebrew": "Ebraico",
  "Estonian": "Estone",
  "Fijan": "Fiji",
  "Finnish": "Finlandese",
  "French": "Francese",
  "Georgian": "Georgiano",
  "Japanese": "Giapponese",
  "Javanese": "Giavanese",
  "Greek": "Greco",
  "Gujarati": "Gujarati",
  "Hindi": "Indiano",
  "English": "Inglese",
  "Indonesian": "Indonesiano",
  "Irish": "Irlandese",
  "Icelandic": "Islandese",
  "Italian": "Italiano",
  "Latin": "Latino",
  "Latvian": "Lettone",
  "Lithuanian": "Lituano",
  "Macedonian": "Macedone",
  "Malayalam": "Malayalam",
  "Maltese": "Malese",
  "Maori": "Maori",
  "Marathi": "Marathi",
  "Mongolian": "Mongolo",
  "Nepali": "Nepalese",
  "Norwegian": "Norvegese",
  "Dutch": "Olandese",
  "Persian": "Persiano",
  "Polish": "Polacco",
  "Portoguese": "Portoghese",
  "Punjabi": "Punjabi",
  "Quechua": "Quechua",
  "Romanian": "Rumeno",
  "Russian": "Russo",
  "Samoan": "Samoan",
  "Serbian": "Serbo",
  "Slovak": "Slovacco",
  "Slovenian": "Sloveno",
  "Spanish": "Spagnolo",
  "Swedish": "Svedese",
  "Swahili": "Swahili",
  "Thai": "Tailandese",
  "Tamil": "Tamil",
  "Tatar": "Tatar",
  "German": "Tedesco",
  "Telugu": "Telugu",
  "Tibetan": "Tibetano",
  "Tonga": "Tonga",
  "Turkish": "Turco",
  "Ukranian": "Ucraino",
  "Hungarian": "Ungherese",
  "Urdu": "Urdu",
  "Uzbek": "Uzbeko",
  "Vietnamese": "Vietnamita",
  "Welsh": "Welsh",
  "Xhosa": "Xhosa"
};

jQuery(".upme-field-value > span").each(function() {
  for (var ptrn in dictionary) {
    jQuery(this).text(jQuery(this).text().replace(new RegExp(ptrn, "g"), dictionary[ptrn]));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="upme-field-value">
  <span>Italian</span>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Alecs
  • 3
  • 1
  • 2
    avoid using `for .. in` on objects. Use `for .. of` instead or `Object.entries` or `Object.values` or `Object.keys`. Your code seems to work properly in the above snippet anyway, though the behavior you describe is really making me thing that the issue is just the for .. in. https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-statements-in-jav – briosheje Aug 07 '19 at 10:12
  • 1
    It works in the snippet because the function is only executed once. If you do it multiple times you get `Italianooooo` etc. @Alecs how are you calling this logic, and on exactly what HTML? – Rory McCrossan Aug 07 '19 at 10:26
  • @RoryMcCrossan ops, I've lost the part where he said that he is running the code multiple times. – briosheje Aug 07 '19 at 10:31
  • That happens only with the languages with have last the "ano" "Catalano", "Africano", "Italiano" i dont know way...I using UPME plugin in wordpress and when i change my profile etc ...i use the "jQuery(document).ready(function () {" – Alecs Aug 07 '19 at 10:37
  • I think is better make the dictionary only if the Language is exactly, like that ? "/\ptrn\b/" – Alecs Aug 07 '19 at 11:01

1 Answers1

0

You are searching for word boundary \b

console.log("Italian".replace(/\bItalian\b/g, 'Italiano'))

console.log("Italiano".replace(/\bItalian\b/g, 'Italiano'))
User863
  • 19,346
  • 2
  • 17
  • 41