Skip to content

Localize from object #94

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Readme file updated with info on how to use localize with raw objects
  • Loading branch information
LonelyPrincess committed Jul 9, 2017
commit efaf3e120ed3d778dd6e07813ea0b9126e102d8b
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,48 @@ $("[data-localize]").localize("application", {
</script>
```

See the test/samples for working examples.
See the _examples_ folder for working examples.

### Using raw objects as data source
Given you can't have your texts stored inside of a json file that follows the naming convention defined earlier, you may also pass a raw javascript object to the 'localize' method, just like this:

```javascript
var myTexts = {
"app": {
"name": "MyApp",
"version": "1.2",
"description": "App using 'jquery-localize'"
}
};

// Localize your content by using the object values
$("[data-localize]").localize(myTexts);
```

The way of referencing the different object properties from the html file is exactly the same as before.

```html
<!-- The 'p' tag content will be replaced with the value of the 'app.name' property inside our 'myTexts' object -->
<p data-localize="app.name">(text to replace)</p>
```

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.

In that case, you'll need to perform an ajax request to said service from inside your own code and call 'localize' by passing in the results returned by the server:

```javascript
$.ajax({
method: "GET",
url: "api/app/details",
data: { lang: "en" },
dataType: "json",
success: function (data) {
$("[data-localize]").localize(data);
}
});
```

Remember to keep a json friendly structure for every object you send as an argument to 'localize', such as in the previous examples.

# Contributing

Expand Down