Giter VIP home page Giter VIP logo

Comments (7)

jasonmit avatar jasonmit commented on May 14, 2024

I get a Promise Object but I need the result as a String in a for-loop

Can you explain what you're doing? Maybe I can offer up a solution but here is what I have that should work. There may be a real use case here that I can build support around.

Here is how you might achieve this (untested, should work):

export default Ember.Controller.extend({
  intl: Ember.inject.service(),
  actions: {
    thingy: function () {
      var intl = this.get('intl');
      intl.getTranslation('messages.photos').then(function (result) {
         // not sure what version of ember-intl you are using
         // so it's either `content` or `translation`
         var output = intl.formatMessage(result.translation || result.content, {
           name: 'Name'
           numPhotos: 1000
           takenDate: new Date()
         }, {
           locales: result.locale
         });

         console.log(output);
      }.bind(this));
    }
  }
});

Edit: fixed a bunch of typos

from ember-intl.

svox1 avatar svox1 commented on May 14, 2024

Thanks for the quick response.

Currently I have a loop like:

myList: function() {
    return this.get('model').map(function(itemData) {
            var message = '';
            switch(itemData.get('type')) {
                case 'point':
                    message = '+' + itemData.get('value') + ' ' + itemData.get('message');
                    break;

               case 'playerJoined':
                    message = 'Joined the game';
                    break;

                 case 'playerLeft':
                    message = '"' + itemData.get('message') + '" has left the game';
                    break; 

                 case 'settingPointChanged':
                    message = 'Edit Settingpoint: ' + itemData.get('message') + '<br/>New Point: +' + itemData.get('value');
                    break;

                //......
            }
      }
}

And many more messages with different types and messages. This message I want to localized. And this is where Iam need a method that is returning a simple Object or String with paramters.

from ember-intl.

jasonmit avatar jasonmit commented on May 14, 2024

This branch supports your use-case of findTranslation returning a simple object: https://github.com/yahoo/ember-intl/tree/2.0

The returned object contains two properties: content (your translation string) and locale where the translation came from.

Notable changes needed in order to port your app to use this branch:

  • Move your translations from /app/locales to /translations (outside of app folder)
  • Your translation files should now be json files, instead of es6 modules. if you need an example just look at the dummy app:
    https://github.com/yahoo/ember-intl/tree/2.0/tests/dummy
  • Your config/environment should look something similar to:
module.exports = function(environment) {
  var ENV = {
    intl: {
      defaultTranslation: 'en' // your default locale
    }
    // ..
  }
  return ENV;
}

The default translation right now is required, and it's the locale in which you want your application to fallback to when a translation key is omitted from any of the other locales your app supports. At build time, you'll see warnings when a translation does not contain a key that your default locale's translation file contains. This makes the default really the source of truth for all other locale translations.

This will hopefully be released in a few weeks, but it's very alpha right now. I'd recommend just sitting tight and continue to use your working solution and I'll close this issue once 2.0 is on master and stable.

from ember-intl.

svox1 avatar svox1 commented on May 14, 2024

Thanks again!

Thanks for the informations. Currently Iam in production mode. So its better to wait for the stable release as you say.
Just a note, I have integrate format.js 1.2.1 in my ember-cli 0.2.1 and ember 1.13-canary project and after that, all works fine in my browser (Chrome) but on my phone (iPhone) or with iOS - Emulator with cordova, I see the splashscreen and after that only a blank page. Nothing else with no error message.

But these only as a note because I didnt debug it and just removed the ember-intl.

Thanks for your work and sharing this project!

from ember-intl.

jcope2013 avatar jcope2013 commented on May 14, 2024

thanks for the question, turned out useful for me also, another question is would this be safe or is there a formatHtmlMessage function that I should make instead?

   var intl = this.get('intl');
   return intl.getTranslation('messages.product.html.info').then(function (result) {
      var messageHtml = intl.formatMessage(result.translation || result.content, {
          product: 'Apple watch',
          price: 200
       }, {
          locales: result.locale
        });
    });

from ember-intl.

jasonmit avatar jasonmit commented on May 14, 2024

Thanks for the question, turned out useful for me also, another question is would this be safe or is there a formatHtmlMessage function that I should make instead?

That's not safe, and I don't expose that as a method from the service to do it. What you'll want to do is wrap that messageHtml in Ember.String.htmlSafe @jcope2013

from ember-intl.

jasonmit avatar jasonmit commented on May 14, 2024

I see the splashscreen and after that only a blank page. Nothing else with no error message.

@svox1 You'll likely need to load the Intl polyfill, the README at the top covers instructions on how to do that.

from ember-intl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.