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

Annotating strings in Javascript code #19

Closed
t00f opened this issue Nov 20, 2013 · 5 comments
Closed

Annotating strings in Javascript code #19

t00f opened this issue Nov 20, 2013 · 5 comments

Comments

@t00f
Copy link

t00f commented Nov 20, 2013

Hello,

Thanks a lot for this useful work ! I always enjoyed the use of gettext, and using it with angular is awesome.

According to the documentation, we can use gettext factory to annotate strings in Javascript code

angular.module("myApp").controller("helloController", function (gettext) {
    var myString = gettext("Hello");
    console.log(myString);
});

My current string Hello is well find after grunt extraction, translated with Poedit and compiled to be added to the translation.js file. Unfortunately, writing those lines won't actually translate the string Hello. The log message keeps to say "Hello" instead of "Hallo"

I checked the source code and I might found the problem. The factory called gettext simply returns the string. A comment explains that it enables grunt to extract strings without translations

angular.module('gettext').factory('gettext', function () {
    /*
     * Does nothing, simply returns the input string.
     * 
     * This function serves as a marker for `grunt-angular-gettext` to know that
     * this string should be extracted for translations.
     */
    return function (str) {
        return str;
    };
});

I tried to use the gettextCatalog to find the translation and its seems to work fine.

angular.module('gettext').factory('gettext', function (gettextCatalog) {
  return function (str) {
    return gettextCatalog.getString(str);
  };
});

Is there a problem in Javascript annotation ? Does my changes in your gettext factory a nice solution to you ?

@rubenv
Copy link
Owner

rubenv commented Nov 20, 2013

You're not the first person to get hit by this, I should probably clarify the documentation for this one.

Not returning a translated string is intentional: You want to pass the original string to the view and translate it there. That way it can keep responding to language changes.

If you really want the translated string in your controller, use gettextCatalog.getString(gettext("My string")). Yup, that looks ugly, as a reminder that it's probably the wrong thing to do.

Edit (2014/07/09): Nowadays you can just use gettextCatalog.getString("My string")

Here's what your probably want to be doing:

angular.module("myApp").controller("helloController", function ($scope, gettext) {
    $scope.myString = gettext("Hello");
});

And then in your view:

{{myString|translate}}

@t00f
Copy link
Author

t00f commented Nov 20, 2013

Thanks a lot for your quick reply !
I didn't noticed that gettext() was actually only there to anotate - even if it's clearly written in the documentation...

As I don't really need the translated string in my controller, I would use your second option which is wayyyy better for my eyes ;)

If you want to improve your documentation, just add the 2 last sentence. It is clear enough to me now !

Thanks again.

@rubenv
Copy link
Owner

rubenv commented Nov 20, 2013

Also, if you don't need the string at all in your controller, just put it directly into the view:

{{"Hello"|translate}}

Or even nicer:

<h1 translate>Hello</h1>

Enjoy!

@tkrotoff
Copy link
Contributor

@rubenv

You're not the first person to get hit by this

I too got confused and had to check the source code.

gettextCatalog.getString(gettext("My string")) [...] probably the wrong thing to do.

Then how do you treat this case? :

app.service('HelloHttpService', function($http, Alert) {

  this.query = function() {
    $http.get('hello.json').then(
      function(response) {
        Alert.success('Everything OK'); <====
      },
      function(response) {
        Alert.error('Something went wrong: ' + error + ', please reload'); <====
      }
    );
  };

});

A function to translate text would be nice here:

tr('Something went wrong: {{error}}, please reload', {error: error});

Note: Alert being a service that displays alert messages/notifications (something like this)

@tsi
Copy link

tsi commented May 10, 2015

@tkrotoff did you find a solution for this?

paulijar added a commit to paulijar/music that referenced this issue Jul 20, 2018
In the javascript code, the call to gettext() does nothing except
annotates the string for extraction by l10n-extract. This has been
necessary in the past but nowadays call to gettextCatalog.getString()
does that automatically. Hence, all the calls to gettext() were just
unnecessary clutter.

See e.g. rubenv/angular-gettext#19
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

4 participants