Coder Social home page Coder Social logo

Comments (2)

alphadijkstra avatar alphadijkstra commented on May 18, 2024 2

This issue is still there. I can trace it back to line 245 of jquery.i18n.js (version 1.0.5)

from jquery.i18n.

timberger avatar timberger commented on May 18, 2024

The problem is if the default texts which are in the Data API tags are not provide also in a language file the script will replace the innerText/innerHTML of the tags with the Data API key lines 169-171 in jquery.i18n.js

if ( message === '' ) {
	message = key;
}

A quick and dirty solution could to add something like this:

if ( message === '' ) {
	return document.querySelector('[data-i18n="'+key+'"]').innerHTML;
}

The result would be that current message would not change if there is no translation in the selected language.
But in my scenario the default message shall be given with the HTML on page load. (Why should i have also a languge file for the default language. It would require to keep them in sync and i have not found a tool that updates the language file when the messages in the HTML change.)
My idea is to add a function which reads all the messages from the Data API tags in the HTML file and add them to the messageStore on page load.

	function grab_defaults_from_data_api() {
		var deflocale = getDefaultLocale();
		var defaultmessages = document.querySelectorAll( '[data-i18n]' );
		if ( defaultmessages.length > 0 ) {
			var defaultmessagesobj = {
				"@metadata": {
					"authors": [
						"mr robot"
					],
					"last-updated": "1970-01-01",
					"locale": deflocale,
					"message-documentation": "qqq"
				}
			}
			for (var i=0; i < defaultmessages.length; i++) {
				defaultmessagesobj[ String(defaultmessages[i].getAttribute('data-i18n')) ] = defaultmessages[i].innerHTML;
			}
			$.i18n.messageStore.set(deflocale, defaultmessagesobj);
		}
	}

This function can be placed e.g. below getDefaultLocale() in jquery.i18n.js and call in the load function of I18N.

		load: function ( source, locale ) {
			var fallbackLocales, locIndex, fallbackLocale, sourceMap = {};
			
			grab_defaults_from_data_api();
			
			if ( !source && !locale ) {

from jquery.i18n.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.