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

Clarify that gettext() only serves for extraction #20

Open
rubenv opened this issue Nov 20, 2013 · 6 comments
Open

Clarify that gettext() only serves for extraction #20

rubenv opened this issue Nov 20, 2013 · 6 comments

Comments

@rubenv
Copy link
Owner

rubenv commented Nov 20, 2013

This comes up again and again:

@dipson
Copy link
Contributor

dipson commented Nov 20, 2013

I think changing the example here would help: http://angular-gettext.rocketeer.be/dev-guide/annotate-js/

It would be useful to include an example of displaying a string which is extracted from JS.

To modify your current example slightly...

Controller:

angular.module("myApp").controller("helloController", function ($scope, UserService, gettext) {
    $scope.greeting = gettext("Hello");
    $scope.username = UserService.getName();
});

View:

<div ng-controller="helloController">
    <h1>{{ greeting | translate }}, {{username}}</h1>
</div>

@rubenv
Copy link
Owner Author

rubenv commented Nov 20, 2013

Well, the example you're proposing could be written as <h1 translate>Hello, {{username}}</h1>, which is even better (not all languages want the ,, or the same word order, putting the entire text in a string gives translators the freedom to do adjust that).

But I totally agree, we need a good example. I typically run into these when generating mark-up (e.g. a list of translated menu items generated with ng-repeat).

@danse
Copy link

danse commented Jun 27, 2014

I was also confused by this, and looking for more info. Maybe it is just the interface being counterintuitive? Maybe having translated = gettext('original') is just what is most commonly expected and needed. I agree that translating in the template is much cleaner, but there are many cases for needing this in the javascript, for example when using directives written by other people

@rubenv
Copy link
Owner Author

rubenv commented Jun 27, 2014

I agree that translating in the template is much cleaner, but there are many cases for needing this in the javascript, for example when using directives written by other people

Use gettextCatalog.getString for those purposes. What you are trying to do is perfectly possible. See here: http://angular-gettext.rocketeer.be/dev-guide/annotate-js/#translating-directly-in-javascript-

@danse
Copy link

danse commented Jun 27, 2014

I knew that, but i argue that the behaviour many expect and the related use case should not be considered probably wrong

Yup, that looks ugly, as a reminder that it's probably the wrong thing to do.

This is mostly true, but if someone is digging into "translating directly in javascript" probably the translate directive, widely presented in all the rest of the doc, is not the suitable solution for her case. As i said, the case may just be a directive written by somebody else, like the modal in angular-strap. Just an opinion

@rubenv
Copy link
Owner Author

rubenv commented Jun 27, 2014

i argue that the behaviour many expect and the related use case should not be considered probably wrong

Except it is. To do translations properly means doing it in the views. Note that nothing is impossible with the current design, it simply encourages the proper usage.

You should take this as a general guideline: the easiest path is probably the best one.

The only exception where translating directly in JS is desired is as you mention: interop with other libraries. It's perfectly possible to do that, it's just not the easiest and most obvious way, but that's because in most cases it's not the thing you need anyway.

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

No branches or pull requests

3 participants