I need use i18n() in my code inside of assets/js of my sailsjs project. How can i to do it?
In my views is ok but in my js not, why?
thanks you very much.
I have sailsjs#0.10.5
What I do, is download the translations from the server and use them client side. I never hardcode translations in my assets and I wouldn't if I were you either.
Add a route to fetch your translations (static json) and parse the JSON client side. There are plenty of libraries that can help you from there.
Keep in mind that it's always possible to include translations in a build for something like require.js.
I agree with Wesley, but if you don't want all of the translations client side. I would suggest writing an Ajax call and make use of the following:
sails.__({
phrase: 'Good day',
locale: 'de'
});
This should then be returned as the German version of 'Good day'. Or you can just send the key phrase and return all your language translations of that.
Related
I'm trying to find a way to check if my rdfa-parser (written in nodejs) is working.
So I have an rdfa-parser, which should print all triples, found in a file or url (with rdfa-syntax).
So far I know, that there are testsuits for RDFa-parsing (http://rdfa.info/test-suite/rdfa1.1/html5/manifest), but I'm not sure how to use them.
Is there a good webpage, where this is described? Or can anyone help me in another way?
There should be some information at the rdfa.info/tests site. Basically, you need a service that will accept a GET request, where the "uri" query parameter points to the input file. The service then parses the file, and returns some other form of RDF, typically N-Triples. More information on the Github page: https://github.com/rdfa/rdfa-website/blob/master/README.md
I’m building an electron app. In it, I have a webview with a preload script. Inside said script, I’d like to use sweetalert.
I installed sweetalert with npm install --save sweetalert. Inside my script I load it with require('sweetalert') and call it with swal("Hello world!");. I now notice it doesn’t look right, as the alert is missing its required CSS file. But I’m loading it with require('sweetalert'), which is great since sweetalert can just remain in its directory inside node_modules and I don’t have to care for it, but its CSS is an integral part of it, and is not getting pulled the same way.
Now, what is the recommended way of solving this? Keep in mind I’m inside a javascript file and would like to remain that way. Do I really have to go get the CSS file and inject it in some way? And how would I do it correctly, since it is inside node_modules? After testing it, it seems like it can’t be done in this particular case due to Content Security Policy.
Either way, that seems so clunky in comparison to the require statement, it’d seem weird for a simpler solution to not be available.
You'll have to include it like you would normally do in a browser, for example in index.html. Copy it out of the module folder into your css folder if you have one and link it with the link tag. It depends on if you're using plain electron or some other boilerplate template with there is a gulp/grunt workflow on where to stick it but that's it really, electron is just a browser that's running your JS/html so it's really the exact same process. require only loads the JS module but not the styles.
if you wanted to include it dynamically you could use the same techniques as a regular browser for example (ex. document.write/create element).
I'm not familiar with sweetalert, but hopefully this helps.
Your syntax for require should be something similar to this.
var sweetalert = require('sweetalert')
You should then be able to access methods on the sweetalert object using the following syntax.
sweetalert.someMethod()
Remember requiring just returns a javascript object. Those objects usually have methods that will allow certain functionality. If you want to add sweetalert to your page, you will either need to inject it within the html, or the javascript within the sweetalert module will need to dynamically create html where the css is included. I hope that clarifies some things and helps you get a better sense of some of the inner workings.
I like coffeescript and I like haml-based template engines. Im trying to figure out how do I use Jade with CoffeeScript? And I dont mean client scripts embedded in html, I mean server side logic.
Actually I found package exactly I want, but its very outdated: jade-coffee
Any suggestion for this?
I'm unsure if I'm understanding the question right or not but if you are trying to write coffeescript in jade you can make use of the Jade language filters.
http://jade-lang.com/reference/filters/
Hope this helps!
I wonder what would be the best way to implement multiple versions / languages of the same content in the same layout in express.
Should I just do this?
app.get("/", function(req, res) {
res.render(language + "/index");
});
Or is there a smarter / better way?
I would suggest to keep only one template, as if you use one template per language it will get out of hand very quickly, let alone duplicate much content (and the small amount of "logic" you would put in a template too). Many applications use a tool called gettext to do the internationalization thing. There is a node.js library for it at https://github.com/DanielBaulig/node-gettext
Alternatively there is also i18n-node. It appears to have a bit more integration with express js.
The i18n-node is the simplest and greatest module that you should use. You can use directly in Javascript code or with Jade/Handlebar templates with express js.
Why should you use i18n?
Auto detection of locale from the browser by header, cookie or query parameter depending on your setup.
It comes with examples as well.
It automatically generates a en.json by default inside ./locales/. This acts as a master file for you to start building new translations.
Supports Singular and plural forms
Support for parameters: __('Hello %s', 'Marcus') returns Hallo Marcus
i thought that we can define json objects in lang folder like , en.js , fr.js and this json files contains key value pairs than render to template according to user's language setting , lang settings can be into database.
And we can save this fr.js or anything else into res.locals for calling every template .
Is this suitable?
I need some advice on the best aproach to a feature I need to implement in a project I'm working on.
Basically, I need to be able to extract all localizable content (i.e. all the strings) from a HTML page. I really don't want to have to go and write a HTML parser. The application is written in C#.
Has anybody got any experience with this, or can anyone recommend an existing library that I could use to accomplish this?
Thanks.
You do not have to write your own parser. Fortunately somebody else already did that.
To parse HTML file, you can use HTML Agility Pack.
In this case you would receive Document Object Model, which you can walk just like any other DOM. Please find these examples:
https://web.archive.org/web/20211020001935/https://www.4guysfromrolla.com/articles/011211-1.aspx
http://htmlagilitypack.codeplex.com/wikipage?title=Examples&referringTitle=Home
And this question:
How to use HTML Agility pack