Skip to content
This repository

Add translation options for posts #309

Closed
wants to merge 1 commit into from

2 participants

Dave Cole
Dave Cole
Owner

Optional translation interface for posts. To use, add language options in _config.yml:

languages:
  - name: 'English'
    key: 'en'
  - name: 'Español'
    key: 'es'
  - name: 'Français'
    key: 'fr'

Set each post's language with in its frontmatter, like lang: en, and save posts with the same file name into a subdirectory based on their language code:

/_posts/2013-01-01-post-title.md
/_posts/es/2013-01-01-post-title.md
/_posts/fr/2013-01-01-post-title.md
Dave Cole
Owner

Screen Shot 2013-04-03 at 7 01 46 PM

Screen Shot 2013-04-03 at 7 01 59 PM

Dave Cole
Owner

This request also adds the option to create posts when you land on a 404 page for a missing post:

Screen Shot 2013-04-03 at 7 03 32 PM

tristen closed this April 06, 2013
Owner

I've manually merged this into the v1 branch to reflect recent changes there. Closing.

Nick Schonning nschonni referenced this pull request in wet-boew/wet-boew-documentation June 25, 2013
Open

Automating checks and updating of gh-pages #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 03, 2013
Add translation options for posts faee380
This page is out of date. Refresh to see the latest.
10  _includes/model.js
@@ -374,6 +374,7 @@ function movePost(user, repo, branch, path, newPath, cb) {
374 374
 // Prepare new empty post
375 375
 
376 376
 function emptyPost(user, repo, branch, path, cb) {
  377
+  var file = new Date().format("Y-m-d")+"-your-filename.md";
377 378
   var rawMetadata = "layout: default\npublished: false";
378 379
   var metadata = {
379 380
     "layout": "default",
@@ -398,6 +399,13 @@ function emptyPost(user, repo, branch, path, cb) {
398 399
     }
399 400
   }
400 401
 
  402
+
  403
+  // If ?file= in path, use it as file name
  404
+  if (path.indexOf('?file=') !== -1) {
  405
+    file = path.split('?file=')[1];
  406
+    path = path.split('?file=')[0].replace(/\/$/, '');
  407
+  }
  408
+
401 409
   cb(null, {
402 410
     "metadata": metadata,
403 411
     "raw_metadata": rawMetadata,
@@ -407,7 +415,7 @@ function emptyPost(user, repo, branch, path, cb) {
407 415
     "published": false,
408 416
     "persisted": false,
409 417
     "writeable": true,
410  
-    "file": new Date().format("Y-m-d")+"-your-filename.md"
  418
+    "file": file
411 419
   });
412 420
 }
413 421
 
16  _includes/styles/style.css
@@ -1155,7 +1155,7 @@ ul.site li a {
1155 1155
   position: absolute;
1156 1156
   top: 60px;
1157 1157
   right: 0px;
1158  
-  width: 300px;
  1158
+  width: 231px;
1159 1159
   z-index: 3000;
1160 1160
   border-radius: 0 0 5px 5px;
1161 1161
   }
@@ -1167,7 +1167,7 @@ ul.site li a {
1167 1167
 
1168 1168
 .document-menu .options .actions {
1169 1169
   background: rgba(255, 255, 255, 0.1);
1170  
-  padding: 20px;
  1170
+  padding: 10px 20px;
1171 1171
   border-radius: 0 0 5px 5px;
1172 1172
   }
1173 1173
 
@@ -1178,6 +1178,7 @@ ul.site li a {
1178 1178
   -moz-border-radius: 5px;
1179 1179
   -webkit-border-radius: 5px;
1180 1180
   border-radius: 5px;
  1181
+  margin: 10px 0;
1181 1182
   }
1182 1183
 
1183 1184
 .document-menu .options .actions .button:hover {
@@ -1689,15 +1690,20 @@ ul.site li a {
1689 1690
   font-weight: 700;
1690 1691
   }
1691 1692
 
  1693
+#notification .buttons {
  1694
+  margin: 0px auto;
  1695
+  text-align: center;
  1696
+  }
  1697
+
1692 1698
 #notification a {
1693 1699
 	background: #516066;
1694 1700
   padding: 5px;
1695 1701
   color: white;
1696 1702
   position: relative;
1697  
-  display: block;
  1703
+  display: inline-block;
1698 1704
   width: 100px;
1699 1705
   height: 30px;
1700  
-  margin: 0px auto;
  1706
+  margin: 0 5px;
1701 1707
   -moz-border-radius: 5px;
1702 1708
   -webkit-border-radius: 5px;
1703 1709
   border-radius: 5px;
@@ -1706,8 +1712,10 @@ ul.site li a {
1706 1712
   font-size: 11px;
1707 1713
   font-weight: 700;
1708 1714
   }
  1715
+  #notification a.muted { background: #738890; }
1709 1716
   #notification a:hover { background: #3d4548; }
1710 1717
 
  1718
+
1711 1719
 ul.form li {
1712 1720
   position: relative;
1713 1721
   }
5  _includes/templates/notification._
@@ -7,5 +7,8 @@
7 7
   <div class="notification <%= type %>">
8 8
     <%= message%> 
9 9
   </div>
10  
-  <a class="button" href="../">Go back </a>
  10
+  <div class="buttons">
  11
+    <a class="button create" href="#">Create it </a>
  12
+    <a class="button muted" href="../">Go back </a>
  13
+  </div>
11 14
 <% } %>
7  _includes/templates/post._
@@ -41,6 +41,13 @@
41 41
         </div>
42 42
       <% } %>
43 43
       <div class="actions">
  44
+        <% if (app.state.config && app.state.config.languages) { %>
  45
+        <% _(app.state.config.languages).each(function(lang) { %>
  46
+        <% if (metadata.lang && metadata.lang !== lang.key) { %>
  47
+        <a class="toggle translate button" href="#<%= lang.key %>">Translate to <%= lang.name %></a>
  48
+        <% } %>
  49
+        <% }); %>
  50
+        <% } %>
44 51
         <a class="toggle delete button" href="#">Delete File</a>
45 52
       </div>
46 53
     </div>
13  _includes/views/application.js
@@ -8,7 +8,8 @@ views.Application = Backbone.View.extend({
8 8
   // ------
9 9
 
10 10
   events: {
11  
-    'click .toggle-view': 'toggleView'
  11
+    'click .toggle-view': 'toggleView',
  12
+    'click #notification .create': 'createPost'
12 13
   },
13 14
 
14 15
   toggleView: function (e) {
@@ -23,6 +24,16 @@ views.Application = Backbone.View.extend({
23 24
     router.navigate(route, true);
24 25
   },
25 26
 
  27
+  createPost: function(e) {
  28
+    var hash = window.location.hash.split('/');
  29
+    hash[2] = 'new';
  30
+    hash[hash.length - 1] = '?file=' + hash[hash.length - 1];
  31
+
  32
+    router.navigate(_(hash).compact().join('/'), true);
  33
+    return false;
  34
+  },
  35
+
  36
+
26 37
   // Initialize
27 38
   // ----------
28 39
 
21  _includes/views/post.js
@@ -13,6 +13,7 @@ views.Post = Backbone.View.extend({
13 13
     'change input': '_makeDirty',
14 14
     'change #post_published': 'updateMetaData',
15 15
     'click .delete': '_delete',
  16
+    'click .translate': '_translate',
16 17
     'click .toggle-options': '_toggleOptions'
17 18
   },
18 19
 
@@ -31,6 +32,24 @@ views.Post = Backbone.View.extend({
31 32
     return false;
32 33
   },
33 34
 
  35
+  _translate: function(e) {
  36
+    var hash = window.location.hash.split('/'),
  37
+        href = $(e.currentTarget).attr('href').substr(1);
  38
+
  39
+    // If current page is not english and target page is english
  40
+    if (href === 'en') {
  41
+      hash.splice(-2, 2, hash[hash.length - 1]);
  42
+    // If current page is english and target page is not english
  43
+    } else if (this.model.metadata.lang === 'en') {
  44
+      hash.splice(-1, 1, href, hash[hash.length - 1]);      
  45
+    // If current page is not english and target page is not english
  46
+    } else {
  47
+      hash.splice(-2, 2, href, hash[hash.length - 1]);
  48
+    }
  49
+    router.navigate(_(hash).compact().join('/'), true);
  50
+    return false;
  51
+  },
  52
+
34 53
   updateURL: function() {
35 54
     var url = _.compact([app.state.user, app.state.repo, this.model.preview ? "blob" : "edit", app.state.branch, this.model.path, this.model.file]);
36 55
     router.navigate(url.join('/'), false);
@@ -366,7 +385,7 @@ views.Post = Backbone.View.extend({
366 385
 
367 386
   render: function() {
368 387
     var that = this;
369  
-    $(this.el).html(templates.post(_.extend(this.model, { mode: this.mode })));
  388
+    $(this.el).html(templates.post(_.extend(this.model, { mode: this.mode, metadata: jsyaml.load(this.model.raw_metadata) })));
370 389
     if (this.model.published) $(this.el).addClass('published');
371 390
     this.initEditor();
372 391
     return this;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.