Skip to content

Commit

Permalink
Language selector
Browse files Browse the repository at this point in the history
  • Loading branch information
kruczjak committed Jul 11, 2016
1 parent 954e0f6 commit 24b7b45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
4 changes: 4 additions & 0 deletions helpers/language_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module LanguageHelper
LOCALES_DIR = 'locales'.freeze
LOCALIZABLE_DIR = 'source/localizable/'.freeze

def current_page_languages_select
current_page_languages&.map { |k, v| [v, k] } || [%w(English en)]
end

def current_page_languages
return nil unless current_page_language
path = current_path =~ /\A\w{2}\// ? current_page.path[3..-1] : current_page.path
Expand Down
2 changes: 1 addition & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
en:
languages:
en: English
pl: polski
pl: polski (Polish)
home:
bundler_info1: |
Bundler provides a consistent environment for Ruby projects by tracking
Expand Down
28 changes: 26 additions & 2 deletions source/javascripts/translations_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
this.LANGUAGE_LINK_ID = '#language-link';
this.LANGUAGE_LINK_STATIC_ID = '#language-link-static';
this.DISMISS_BUTTON_ID = '#translations-bar-close';
this.LANGUAGE_SELECT_ID = '#language-select';
this.LINK_REGEX = /^\/[a-zA-Z]{2}(\/.*)/;
this.cookiesManager = new CookiesManager();
this.choosenLanguage = this.cookiesManager.choosenLanguage();

Expand Down Expand Up @@ -53,20 +55,27 @@
};

this.languagePath = function() {
return '/' + this.browserLanguage + location.pathname;
var language = (this.browserLanguage == 'en' ? '' : ('/' + this.browserLanguage));
return language + this.pathname();
};

this.switchToLanguageUsingCookies = function() {
window.location.href = this.languagePathFromCookies();
};

this.languagePathFromCookies = function() {
return '/' + this.cookiesManager.choosenLanguage() + location.pathname;
var language = (this.cookiesManager.choosenLanguage() == 'en' ? '' : ('/' + this.cookiesManager.choosenLanguage()));
return language + this.pathname();
};

this.checkChoosenLanguage = function() {
return (this.choosenLanguage != null) && (CURRENT_LANGUAGE != this.choosenLanguage) &&
(this.choosenLanguage in LANGUAGES);
};

this.pathname = function() {
var url_without_language = location.pathname.match(this.LINK_REGEX);
return url_without_language != null ? url_without_language[1] : location.pathname;
}
};

Expand Down Expand Up @@ -94,11 +103,26 @@
return this.switchToLanguageUsingCookies();
}
};

TranslationWidget.prototype.initLanguageSelect = function() {
var self = this;
this.languageSelect = $(this.LANGUAGE_SELECT_ID);

this.languageSelect.change(function() {
var optionSelected = $(this).find("option:selected");
var valueSelected = optionSelected.val();

self.cookiesManager.setDismiss();
self.cookiesManager.setLanguage(valueSelected);
self.switchToLanguageUsingCookies();
})
};
})();

window.translationWidget = new TranslationWidget();
translationWidget.checkChoosenLanguageAndRedirect();

$(document).ready(function() {
translationWidget.initLanguageSelect();
translationWidget.init();
});
1 change: 1 addition & 0 deletions source/layouts/_navbar.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.container
%b= link_to "Bundler", "/", class: 'navbar-brand'

%form.navbar-form.pull-right= select_tag :language, options: current_page_languages_select, selected: current_page_language, id: 'language-select', class: 'form-control'
%ul.nav.navbar-nav.navbar-right
%li= link_to t('navbar.docs'), "/docs.html"
%li= link_to t('navbar.team'), "/contributors.html"
Expand Down
6 changes: 5 additions & 1 deletion source/stylesheets/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@

.navbar {
margin-bottom: 0;
}
}

.select-height {
height: 50px;
}

0 comments on commit 24b7b45

Please sign in to comment.