diff --git a/app/assets/javascripts/supplier/application.js b/app/assets/javascripts/supplier/application.js index 9883702c..6db8650e 100644 --- a/app/assets/javascripts/supplier/application.js +++ b/app/assets/javascripts/supplier/application.js @@ -32,6 +32,10 @@ //= require qwaiter //= require_directory . //= require_self +var Qstorage = localStorage; +String.prototype.capitalize = function() { + return this.charAt(0).toUpperCase() + this.slice(1); +} var path_mapping = { user_root: '/user', join_occupied_table: '/user/join_occupied_table', @@ -49,3 +53,15 @@ function redirect_to(mapping, variables){ function currency(num) { return Qwaiter.currency(num); } +$(function(){ + $locale = Qstorage.getItem('locale') || 'en'; + if(Qstorage.getItem('message')){ + var container = $('.alert-success'); + var msg_finder = Qstorage.getItem('message'); + if(msg_finder.indexOf('.') == -1) msg_finder = 'messages.'+msg_finder; + container.find('div').text(t(msg_finder)); + container.show(); + Qstorage.removeItem('message'); + } + setTranslations(); +}); diff --git a/app/assets/javascripts/supplier/translations.js.erb b/app/assets/javascripts/supplier/translations.js.erb new file mode 100644 index 00000000..a057ccba --- /dev/null +++ b/app/assets/javascripts/supplier/translations.js.erb @@ -0,0 +1,62 @@ +var $translations = { + en: { + messages: <%= I18n.t('messages', locale: :en).to_json %>, + models: <%= I18n.t('activemodel.models', locale: :en).to_json %>, + attributes: <%= I18n.t('activemodel.attributes', locale: :en).to_json %>, + <%= I18n.t('supplier', locale: :en).to_json[1..-2] %> + }, + nl: { + models: <%= I18n.t('activemodel.models', locale: :nl).to_json %>, + attributes: <%= I18n.t('activemodel.attributes', locale: :nl).to_json %>, + <%= I18n.t('supplier', locale: :nl).to_json[1..-2] %> + } +} +$transformation_mappings = { + downcase: 'toLowerCase', + upcase: 'toUpperCase' +} +function t(path, vars){ + vars || (vars = {}); + var result, m, translatable, isafety,replacable; + var parts = path.split('.'); + var accessor = '$translations.'+$locale+'["' + parts.join('"]["')+ '"]'; + try{ + result = eval(accessor); + } catch(err){ + result = parts[parts.length - 1].capitalize(); + } + if(!result) return parts[parts.length - 1].capitalize(); + $.each(vars, function(v, value){ result = result.replace('%{'+v+'}', value)}); + isafety = 0; + while(result.indexOf('${') > -1){ + m = result.match(/\${([\w\.]+(\|\w+)?)}/); + if(m[2]){ + translatable = m[1].replace(m[2], ''); + operation = $transformation_mappings[m[2].substr(1) || m[2].substr(1)]; + }else{ + translatable = m[1]; + operation = null; + } + replacable = t(translatable); + if(operation) replacable = replacable[operation](); + result = result.replace(m[0], replacable); + if(isafety > 10) break; + isafety += 1; + } + return result; +} +function setLocale(locale){ + Qstorage.setItem('locale', locale); + $locale = locale; + setTranslations(); +} +function setTranslations(selector){ + var list = $('#top-navigation-list'); + list.find('.locale').show(); + list.find('.locale-'+$locale).hide(); + if(selector){ + $(selector).find('[data-t]').each(function(){$(this).text(t($(this).data('t'), $(this).data('tAttributes')))}) + }else{ + $('[data-t]').each(function(){$(this).text(t($(this).data('t'),$(this).data('tAttributes')))}) + } +} diff --git a/app/models/section.rb b/app/models/section.rb index ff3305c6..b93984af 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -77,6 +77,7 @@ class Section w = width h = height n = tables.size + return unless w > 0 && h > 0 && n > 0 l = Math.sqrt((w*h).to_f/n) epsilon = (10 ** -(Float::DIG - 1)).to_f while (w/l).floor * (h/l).floor < n # find a fitting combination diff --git a/app/models/supplier.rb b/app/models/supplier.rb index a2bb0044..8a1d5c3c 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -83,7 +83,7 @@ class Supplier private def add_section_on_create - @section = Section.create supplier: self, title: I18n.t('section.first_section_title') + @section = Section.create supplier: self, title: I18n.t('supplier.section.first_section_title') end end diff --git a/app/views/suppliers/sections/manage_tables.html.slim b/app/views/suppliers/sections/manage_tables.html.slim index 621f3406..333d4993 100644 --- a/app/views/suppliers/sections/manage_tables.html.slim +++ b/app/views/suppliers/sections/manage_tables.html.slim @@ -1,5 +1,5 @@ - model_class = Section -.page-header= title t('section.manage_tables.title', section: Section.model_name.human.downcase, title: @section.title) +.page-header data-t='section.manage_tables.title' data-t-attributes=%|{"title":"#{@section.title}"}| - content_for :row do ul.nav.nav-tabs - for section in @section.supplier.sections @@ -62,8 +62,8 @@ .controls input.input-mini#add-tables-number-end type=:number value=120 .modal-footer - button.btn data-dismiss="modal" aria-hidden=true Close - button.btn.btn-primary onclick="Qsupplier.add_tables_to_active_section()" Add + button.btn data-dismiss="modal" aria-hidden=true data-t='section.add_tables.modal.close_button' + button.btn.btn-primary onclick="Qsupplier.add_tables_to_active_section()" data-t='section.add_tables.modal.add_button' #arrange-tables-modal.modal.hide.fade tabindex=-1 role=:dialog aria-labeledby='add-tables-modal-label' aria-hidden=true button.close type=:button data-dismiss=:modal aria-hidden=true x h3#arrange-tables-modal-label= t('supplier.section.arrange_tables.modal.title') @@ -87,5 +87,5 @@ label for="arrange-tables-by-column-count"= t('supplier.section.arrange_tables.modal.by_column_count') input.input-mini#arrange-tables-by-column-count type="text" value=0 .modal-footer - button.btn data-dismiss="modal" aria-hidden=true Close - button.btn.btn-primary onclick="Qsupplier.arrange_tables_of_active_section()" Add + button.btn data-dismiss="modal" aria-hidden=true data-t='section.arrange_tables.modal.close_button' + button.btn.btn-primary onclick="Qsupplier.arrange_tables_of_active_section()" data-t='section.arrange_tables.modal.arrange_button' diff --git a/config/locales/en.yml b/config/locales/en.yml index d1a195d4..68f5143d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -110,15 +110,31 @@ en: show: title: Show %{list} section: + first_section_title: Room + manage_tables: + title: "Manage tables for ${models.section|downcase}: %{title}" tables_view: Tables view add_tables: button_label: Add tables modal: title: Add tables + body_header: "" + number_start: From number + number_end: Till number + close_button: Close + add_button: Add arrange_tables: button_label: Arrange tables modal: title: Arrange tables + body_header: "" + distributed: Distributed + by_row: By row + by_row_count: of + by_column: By column + by_column_count: of + close_button: Close + arrange_button: Distribute user: active_list: @@ -148,10 +164,6 @@ en: title: Authenticate Qwaiter obtain: Authenticate invalid_combination: The email password combination is incorrect - section: - first_section_title: Room - manage_tables: - title: "Manage tables for %{section}: %{title}" general: boolean: boolean_yes: "Yes" diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 39d638b2..4389b065 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -94,6 +94,11 @@ nl: title: Titel width: Breedte height: Lengte + supplier: + name: Naam + email: 'E-mail' + password: 'Wachtwoord' + password_confirmation: 'Bevestiging' supplier: menu: active_lists: Actieve %{lists} @@ -114,6 +119,9 @@ nl: show: title: "%{list} tonen" section: + first_section_title: Ruimte + manage_tables: + title: "Tafels beheren voor ${models.section|downcase}: %{title}" tables_view: Tafel overzicht add_tables: button_label: Voeg tafels toe @@ -122,6 +130,8 @@ nl: body_header: "" number_start: Vanaf nummer number_end: Tot nummer + close_button: Sluiten + add_button: Voeg toe arrange_tables: button_label: Positioneer tafels modal: @@ -132,6 +142,8 @@ nl: by_row_count: van by_column: Per kolom by_column_count: van + close_button: Sluiten + arrange_button: Positioneer user: active_list: @@ -161,10 +173,6 @@ nl: title: Aanmelden bij Qwaiter obtain: Aanmelden invalid_combination: De inloggegevens zijn onjuist - section: - first_section_title: Ruimte - manage_tables: - title: "Tafels beheren voor %{section}: %{title}" general: boolean: boolean_yes: "Ja"