OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:polymer/polymer.dart'; |
| 6 import 'package:intl/intl.dart'; |
| 7 import 'messages_all.dart'; |
| 8 |
| 9 /// This is an example polymer component that displays a message in one of |
| 10 /// a couple of different locales, based on the user's drop-down list |
| 11 /// selection. |
| 12 @CustomTag('localized-example') |
| 13 class LocalizedExampleElement extends PolymerElement { |
| 14 |
| 15 // Polymer likes to use observable variables and doesn't really like |
| 16 // observing getters, requiring us to do explicit notification when the |
| 17 // values that the getter depends on change. So we will save the result |
| 18 // of the message function in an observable variable. That will get |
| 19 // a bit trickier if we have messages that have parameters. |
| 20 @observable String selectedLocale; |
| 21 @observable String helloWorld; |
| 22 |
| 23 // It would be simpler to set [helloWorld] in an initializer, but we can't |
| 24 // put a method call in an initializer, so do it in the constructor. |
| 25 LocalizedExampleElement.created() : super.created() { |
| 26 updateLocale(Intl.defaultLocale); |
| 27 } |
| 28 |
| 29 // Polymer should call this method automatically if the value of |
| 30 // [selectedLocale] changes. |
| 31 void selectedLocaleChanged() { |
| 32 // We didn't provide en_US translations. We expect it to use the default |
| 33 // text in the messages for en_US. But then we have to not try and |
| 34 // initialize messages for the en_US locale. dartbug.com/15444 |
| 35 if (selectedLocale == 'en_US') { |
| 36 updateLocale('en_US'); |
| 37 return; |
| 38 } |
| 39 // For the normal case we initialize the messages, wait for initialization |
| 40 // to complete, then update all (all 1 of) our messages. |
| 41 initializeMessages(selectedLocale).then( |
| 42 (succeeded) => updateLocale(selectedLocale)); |
| 43 } |
| 44 |
| 45 // When the user chooses a new locale, set the default locale for the |
| 46 // whole program to that and update our messages. In a large program this |
| 47 // could get to be a large method. Also, other components might want to |
| 48 // observe the default locale and update accordingly. |
| 49 void updateLocale(localeName) { |
| 50 Intl.defaultLocale = selectedLocale; |
| 51 helloWorld = helloFromDart(); |
| 52 } |
| 53 |
| 54 // This is our message, a function that would take parameters if we have any |
| 55 // and then gives us back the appropriate translated message. |
| 56 // Of course, in order to make this happen we need to extract the messages |
| 57 // from our program. We don't have an integration with a real translation |
| 58 // system, but you can produce some custom JSON by running (assuming you have |
| 59 // the full Intl package available somewhere, like in a built SDK). |
| 60 // dart --package-root=<some-path-to-packages> |
| 61 // <path-to-intl>/intl/test/message_extraction/extract_to_json.dart |
| 62 // localized.dart |
| 63 // That will produce intl_messages.json. We can then use whatever process |
| 64 // we want to produce translation_fr.json and translation_pt.json. I used |
| 65 // hand-editing, but that doesn't scale. Then |
| 66 // dart --package-root=<some-path-to-packages> |
| 67 // <path-to-intl>/intl/test/message_extraction/generate_from_json.dar
t |
| 68 // localized.dart translation_fr.json translation_pt.json |
| 69 // will produce messages_all.dart, messages_fr.dart and messages_pt.dart. |
| 70 // We import messages_all.dart, and we're done. |
| 71 helloFromDart() => Intl.message("Hello World from Dart!", name: 'helloWorld', |
| 72 desc: "This is just a simple Hello World message that doesn't" |
| 73 "take any parameters.", |
| 74 args: [], // This would be required if we had any arguments. |
| 75 examples: {"We could put examples of parameter values here for the " |
| 76 "translators if we had any parameters" : 0}); |
| 77 } |
OLD | NEW |