Zsolt Haraszti | 9ceb1b8 | 2016-10-01 15:15:18 -0700 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Translator for documentation pages. |
| 5 | * |
| 6 | * To enable translation you should include one of language-files in your index.html |
| 7 | * after <script src='lang/translator.js' type='text/javascript'></script>. |
| 8 | * For example - <script src='lang/ru.js' type='text/javascript'></script> |
| 9 | * |
| 10 | * If you wish to translate some new texts you should do two things: |
| 11 | * 1. Add a new phrase pair ("New Phrase": "New Translation") into your language file (for example lang/ru.js). It will be great if you add it in other language files too. |
| 12 | * 2. Mark that text it templates this way <anyHtmlTag data-sw-translate>New Phrase</anyHtmlTag> or <anyHtmlTag data-sw-translate value='New Phrase'/>. |
| 13 | * The main thing here is attribute data-sw-translate. Only inner html, title-attribute and value-attribute are going to translate. |
| 14 | * |
| 15 | */ |
| 16 | window.SwaggerTranslator = { |
| 17 | |
| 18 | _words:[], |
| 19 | |
| 20 | translate: function(sel) { |
| 21 | var $this = this; |
| 22 | sel = sel || '[data-sw-translate]'; |
| 23 | |
| 24 | $(sel).each(function() { |
| 25 | $(this).html($this._tryTranslate($(this).html())); |
| 26 | |
| 27 | $(this).val($this._tryTranslate($(this).val())); |
| 28 | $(this).attr('title', $this._tryTranslate($(this).attr('title'))); |
| 29 | }); |
| 30 | }, |
| 31 | |
| 32 | _tryTranslate: function(word) { |
| 33 | return this._words[$.trim(word)] !== undefined ? this._words[$.trim(word)] : word; |
| 34 | }, |
| 35 | |
| 36 | learn: function(wordsMap) { |
| 37 | this._words = wordsMap; |
| 38 | } |
| 39 | }; |