Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize from object #94

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

LonelyPrincess
Copy link

Until now the plugin did only allow to localize contents from a json file that followed a certain naming guidelines. With the proposed changes, the localize method can now receive an object as a parameter, instead of the name of a language pack.

This feature might be useful if you'll be retrieving your texts from an external service whose url doesn't match the "{pathPrefix}/{filename}-{lang}.{extension}" syntax.

The new code will only be applied when the received parameter is an object: otherwise, the plugin will expect the parameter to be a language pack name and keep its default behavior. The README.md file has been updated explaining the new feature, and I've also included a new subdirectory in the examples folder to illustrate how it works.

This set of changes should solve issue #62.

New code has been added so that raw javascript objects can now be used as
a source for the translations.

If localize receives an object as a first parameter, items with
'data-localize' tags will be updated with the text contained in the
matching object properties. Said object will be converted to a valid json
object beforehand, so no problems shall arise if the given object include
functions as a value for any of its keys.

In the case localize method receives any parameter that is not an object,
the default behaviour where the translations are retrieved from an
external file will be applied.

This should solve the following issue:
coderifous#62
The method that replace the localized tags by the texts defined inside of
a raw object did not consider any of the possible options you could
receive as a second parameter in a 'localize' call.

The 'callback' option is now supported. The 'data' object will be
converted to a valid json object before calling the default callback, so
if the user sets any of the data properties to a function, the elements
for which the 'data-localize' attribute references any of those invalid property
values will not be updated.

The 'localize_test.coffee' file do now include new methods to test the new functionality.

The 'localize_from_object.html' has been also updated
and seems to be working as expected in the following browsers:

 - Google Chrome (59.0.3071.115)
 - Mozilla Firefox (45.0, 47.0.1)
 - Microsoft Edge (40.15063.0.0)
I've added a new example to illustrate on how to call localize by passing
the response of a webservice as an argument to 'jquery-localize'.

Both of the examples created for this new feature have been moved to a new
'objectDS' folder, as to keep them separated from the default examples.
Since we want the examples to be fully functional and show how the library
is meant to be used, I've removed the properties for which I've assigned a
function as their value.

Still, the sanitizer callback will be kept and so will the test to
guarantee that there will be no problem if the user passes an object that
include a function among their keys.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant