Skip to content

Commit

Permalink
Refactors Google Translate code
Browse files Browse the repository at this point in the history
- Moves Google Translate initialization to application.js so it’s
applied to all routes.
- Refactors Google Translate Manager to remove polling code and to
integrate Google supplied initialization scripts.
- Refactors language translation link settings:
- Strips whitespace around language code setting so any amount of
spacing can be introduced into the settings and the app won’t silently
not configure the language code URL properly.
- Adds ability to remove all language links and the app won’t break.
Ties into #483.
- Edits language links code comments to be consistent with other
settings in the settings file and adds a space after the colon in the
settings to be consistent with Ruby style guide key/value styling.
- Refactors area above and below search box on homepage so that when
language links are absent the height of the utility area doesn’t
collapse. Also, renames bottom utility area to be more generic.
- Adds translation services section to customization readme.
- Moves google translate manager JS to `util` directory.
- Moves drop-down layout handling code to its own module.
- Capitalizes modules that include creation method for creating a new
instance.
- Adds error handling so if Google script changes container HTML an
error will be thrown.
- Fixes #287 Google Translate cookie wasn’t being set on inside pages.
This commit adds the Google Translate cookie concern to inside pages in
addition to the homepage.
  • Loading branch information
anselmbradford committed Sep 4, 2014
1 parent a89bbf9 commit 304e225
Show file tree
Hide file tree
Showing 20 changed files with 268 additions and 160 deletions.
13 changes: 13 additions & 0 deletions CUSTOMIZE.md
Expand Up @@ -29,6 +29,19 @@ In that file, you can customize much of the user-facing text on the website.
Most of the text should be ready to go, but there is one specific setting
you should customize: the [location](https://github.com/codeforamerica/ohana-web-search/blob/master/config/locales/en.yml#L28) value under `branding`.

## Language Translation Services
The application uses Google Translate for free language translation of the site. The Google Website Translator Gadget appears in the footer of the site and provides free translation of the site into dozens of languages. By default, six major language translation links are provided (in their native script) on the upper-right of the homepage. A number of aspects related to translation can be customized:

- **Homepage language links**. The language translation links on the homepage can be added, removed, and edited in `config/settings.yml`.

- **Support for saving custom translations**. If you want to make edits to the Google provided translations of a live site you can add a `GOOGLE_TRANSLATE_CUSTOMIZATION_CODE` in `config/application.yml`. Follow the directions in that file for obtaining the proper code for your site.

- **Support for non-English keyword searches**. If you want your users to be able to enter a keyword search in a language other than English, you can enable language detection by adding a `GOOGLE_TRANSLATE_API_TOKEN` in `config/application.yml`. Follow the directions in that file for obtaining an API token.
**NOTE: GOOGLE CHARGES FOR THIS FEATURE.**

- **Change the gadget layout**. If you want to change the Google Website Translator Gadget's layout, two of the layouts provided by Google are supported: InlineLayout.VERTICAL and InlineLayout.HORIZONTAL. One or the other
can be set in `assets/javascripts/application.js`.

## Pagination
The pagination of search results is handled by the [Kaminari](https://github.com/amatsuda/kaminari) gem.
To configure the functionality, make changes in [kaminari_config.rb](https://github.com/codeforamerica/ohana-web-search/blob/master/config/initializers/kaminari_config.rb).
Expand Down
6 changes: 1 addition & 5 deletions app/assets/javascripts/app/app-init.js
Expand Up @@ -3,16 +3,12 @@
// and search details pages. It is not called by
// the about page, because that page does not have popups to manage.
require([
'app/google-translate-manager',
'app/popup-manager',
'app/alert-manager'
],
function (gt, pm, alert) {
function (pm, alert) {
'use strict';

// Initialize the google translate manager.
gt.init();

// If box-shadow CSS is supported, initialize the popups.
if (Modernizr.boxshadow)
pm.init();
Expand Down
61 changes: 0 additions & 61 deletions app/assets/javascripts/app/google-translate-manager.js

This file was deleted.

13 changes: 12 additions & 1 deletion app/assets/javascripts/application.js
Expand Up @@ -69,8 +69,19 @@
// app/assets/javascripts/routes/.
//
require([
'util/translation/google-translate-manager',
'domReady!'
],
function () {
function (googleTranslate) {
'use strict';

// The Google translate manager handles loading of the
// Google Website Translator Gadget at the bottom of the page's body.
// The layout settings passed in as an argument to the initialization
// method can be set to:
// InlineLayout.VERTICAL, InlineLayout.HORIZONTAL,
// which correspond to the 'inline' display modes available through Google.
// The 'tabbed' and 'auto' display modes are not supported.
// The 'inline' InlineLayout.SIMPLE layout is also not supported.
googleTranslate.init(googleTranslate.InlineLayout.VERTICAL);
});
6 changes: 3 additions & 3 deletions app/assets/javascripts/result/result-map-manager.js
@@ -1,11 +1,11 @@
// Manages search results view Google Map.
define([
'util/bitmask',
'util/BitMask',
'util/map/marker-manager',
'domReady!',
'async!https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false!callback'
],
function (bitmask, markerManager) {
function (BitMask, markerManager) {
'use strict';

// The <div> element that the Google map loads into.
Expand Down Expand Up @@ -75,7 +75,7 @@ function (bitmask, markerManager) {
var _infoBoxContent;

function init() {
_infoBoxState = bitmask.create();
_infoBoxState = BitMask.create();

// Turn off assumption that touch is being used initially, this is turned
// on if a touch event is registered.
Expand Down
4 changes: 0 additions & 4 deletions app/assets/javascripts/routes/about/index.js
@@ -1,15 +1,11 @@
// Manages initialization of scripts for the About page.
require([
'app/google-translate-manager',
'app/feedback-form-manager',
'application'
],
function (gt, feedback) {
'use strict';

// Initialize the Google Translate manager.
gt.init();

// Initialize the feedback form.
feedback.init();
});
File renamed without changes.
6 changes: 3 additions & 3 deletions app/assets/javascripts/util/map/marker-manager.js.erb
Expand Up @@ -5,9 +5,9 @@

// Used for managing which map marker should be shown.
define([
'util/bitmask'
'util/BitMask'
],
function (bitmask) {
function (BitMask) {
'use strict';

// 'Constants' for the kind labels returned by the Ohana API.
Expand All @@ -34,7 +34,7 @@ function (bitmask) {
var UNSPIDERFIED_ICON = 8;

// A bitmask instance for tracking the four states of the icon appearance.
var _iconState = bitmask.create();
var _iconState = BitMask.create();
_iconState.turnOn(SMALL_ICON | UNSPIDERFIED_ICON);

// The kind set on this marker.
Expand Down
@@ -0,0 +1,90 @@
// Manages behavior of the Google Website Translator Gadget.
define([
'util/translation/layout/DropDownLayout'
],
function (DropDownLayout) {
'use strict';

// The layout object in use.
var _layout;

// The current layout that is set. Determined by the
// 'layout' setting in the Google Translate provided script
// in the footer.
var _layoutType;

// Same 'constants' as google.translate.TranslateElement.InlineLayout
// for tracking which layout is in use.
var VERTICAL = 0;
var HORIZONTAL = 1;
var InlineLayout = { VERTICAL:VERTICAL, HORIZONTAL:HORIZONTAL };

// The id of the element on the page that will contain
// the Google Website Translator Gadget.
var GOOGLE_TRANSLATE_ELEMENT_ID = 'google-translate-container';

function init(layoutType) {
_layoutType = layoutType;

_deleteTranslateCookies();

_layout = DropDownLayout.create();
_layout.init(GOOGLE_TRANSLATE_ELEMENT_ID);

// Add Google Translate script call by appending script element.
var scriptElm = document.createElement('script');
var scriptUrl = '//translate.google.com/translate_a/element.js?cb=';
var scriptCallback = 'GoogleTranslate.googleTranslateElementInit';
scriptElm.setAttribute('src', scriptUrl+scriptCallback);
document.body.appendChild(scriptElm);

// Add the callback function for the Google Translate script.
var GoogleTranslate = {
googleTranslateElementInit:_googleTranslateElementInit
};
window.GoogleTranslate = GoogleTranslate;
}

// Initialize the Google Website Translator Gadget.
function _googleTranslateElementInit() {
var opts = {
pageLanguage: 'en',
layout: _getGoogleLayout(),
autoDisplay: false
};

new google.translate.TranslateElement( opts, GOOGLE_TRANSLATE_ELEMENT_ID );

// Activate hooks to manipulate Google Website Translator Gadget through
// the URL 'translate' parameter.
_layout.activate();
}

// @return [Object] Return the inline Google Website Translator Gadget
// layouts supplied by Google.
function _getGoogleLayout() {
if (_layoutType === VERTICAL)
return google.translate.TranslateElement.InlineLayout.VERTICAL;
else if (_layoutType === HORIZONTAL)
return google.translate.TranslateElement.InlineLayout.HORIZONTAL;
}

// Removes the Google Website Translator cookies by setting their expiration
// date into the past.
function _deleteTranslateCookies() {
var cookies, cookie, eqPos, name;
cookies = document.cookie.split('; ');
for (var i = 0, len = cookies.length; i < len; i++) {
cookie = cookies[i];
eqPos = cookie.indexOf('=');
name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
if (name === 'googtrans')
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
}
}

return {
init:init,
InlineLayout:InlineLayout
};
});
64 changes: 64 additions & 0 deletions app/assets/javascripts/util/translation/layout/DropDownLayout.js
@@ -0,0 +1,64 @@
// Manages behavior of posting changes to the URL for
// inlinelayout.VERTICAL and inlinelayout.HORIZONTAL
// Google Translate Widget layouts.
define([
'util/util'
],
function (util) {
'use strict';

function create() {
return new DropDownLayout();
}

function DropDownLayout() {

// The HTML element that contain the language options.
var _languages;

// The ID of the Google Translate widget HTML container.
var _googleTranslateElmID;

function init(id) {
_googleTranslateElmID = id;
}

function activate() {
_languages = _getGoogleLayoutElement();
_hookDropDown();
}

// @return [Object] The HTML element that contains the Google Translate
// languages.
function _getGoogleLayoutElement() {
var query = '#'+_googleTranslateElmID + ' select';
var elm = document.querySelector(query);
if (!elm)
throw new Error('Google Translate Widget HTML container not found!');
return elm
}

// Hook in a change event to the language drop-down menu so the page reloads
// and is appended a translate query string when the drop-down menu changes.
function _hookDropDown() {
_languages.addEventListener('change', _languageDropDownChanged, false);
}

// Update the URL with the selected language.
function _languageDropDownChanged() {
var options = {
'translate': _languages.options[_languages.selectedIndex].value
};
document.location.search = util.queryString(options);
}

return {
init:init,
activate:activate
};
}

return {
create:create
};
});

0 comments on commit 304e225

Please sign in to comment.