From c44b4ffda27d233b907c7465b7f3a6969e635ca8 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 19 Jul 2013 16:34:55 +0400 Subject: [PATCH] #214: added "angular-i18n" gem, some refactoring --- Gemfile | 1 + Gemfile.lock | 2 ++ app/assets/javascripts/angularjs/config.js | 18 +++++++++++--- .../controllers/project_refs_controller.js | 4 +++- .../controllers/rosa_abf_controller.js | 7 ++++++ app/assets/javascripts/angularjs/locales.js | 24 +++++++++++++++++++ app/assets/javascripts/application.js | 1 + app/views/layouts/application.html.haml | 2 +- .../projects/git/trees/branches.html.haml | 8 +------ app/views/projects/git/trees/tags.html.haml | 7 +----- config/locales/models/project.en.yml | 13 ---------- config/locales/models/project.ru.yml | 13 ---------- 12 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 app/assets/javascripts/angularjs/controllers/rosa_abf_controller.js create mode 100644 app/assets/javascripts/angularjs/locales.js diff --git a/Gemfile b/Gemfile index a57100070..bca1231e6 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,7 @@ gem "gemoji", "~> 1.2.1", require: 'emoji/railtie' gem 'angularjs-rails' gem 'ng-rails-csrf' gem 'momentjs-rails' +gem 'angular-i18n', '0.1.1' group :assets do gem 'sass-rails', '~> 3.2.5' diff --git a/Gemfile.lock b/Gemfile.lock index 970f85042..c3f0ab87a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -58,6 +58,7 @@ GEM json ancestry (1.3.0) activerecord (>= 2.3.14) + angular-i18n (0.1.1) angularjs-rails (1.0.7) arel (3.0.2) attr_encrypted (1.2.1) @@ -408,6 +409,7 @@ DEPENDENCIES RedCloth airbrake (~> 3.1.2) ancestry (~> 1.3.0) + angular-i18n (= 0.1.1) angularjs-rails attr_encrypted (= 1.2.1) better_errors diff --git a/app/assets/javascripts/angularjs/config.js b/app/assets/javascripts/angularjs/config.js index b6181a898..c8bbb149b 100644 --- a/app/assets/javascripts/angularjs/config.js +++ b/app/assets/javascripts/angularjs/config.js @@ -1,14 +1,26 @@ -var RosaABF = angular.module('RosaABF', ['ngResource', 'ng-rails-csrf']); +var RosaABF = angular.module('RosaABF', ['ngResource', 'ng-rails-csrf', 'angular-i18n']); var DateTimeFormatter = function() { var UtcFormatter = function(api_time) { - return moment.utc(api_time * 1000).format('YYYY-MM-DD HH:mm:ss UTC'); + return moment.utc(api_time * 1000).format('YYYY-MM-DD HH:mm:ss UTC'); } return { utc : UtcFormatter } } +RosaABF.factory("DateTimeFormatter", DateTimeFormatter); -RosaABF.factory("DateTimeFormatter", DateTimeFormatter); \ No newline at end of file +var LocalesHelper = function($locale) { + var locales = { + 'ru' : 'ru-ru', + 'en' : 'en-us' + } + return { + setLocale: function(locale) { + $locale.id = locales[locale]; + } + } +} +RosaABF.factory("LocalesHelper", ['$locale', LocalesHelper]); \ No newline at end of file diff --git a/app/assets/javascripts/angularjs/controllers/project_refs_controller.js b/app/assets/javascripts/angularjs/controllers/project_refs_controller.js index c163e83fd..0b361e981 100644 --- a/app/assets/javascripts/angularjs/controllers/project_refs_controller.js +++ b/app/assets/javascripts/angularjs/controllers/project_refs_controller.js @@ -8,7 +8,7 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu $scope.current_ref = null; $scope.project_resource = null; - $scope.init = function(project_id, ref) { + $scope.init = function(project_id, ref, locale) { $scope.project_id = project_id; $scope.current_ref = ref; @@ -45,6 +45,8 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu } + $scope + $scope.updateBranchesCount = function() { $scope.singleton.project.branches_count = $scope.branches.length; } diff --git a/app/assets/javascripts/angularjs/controllers/rosa_abf_controller.js b/app/assets/javascripts/angularjs/controllers/rosa_abf_controller.js new file mode 100644 index 000000000..acedbd7c8 --- /dev/null +++ b/app/assets/javascripts/angularjs/controllers/rosa_abf_controller.js @@ -0,0 +1,7 @@ +RosaABF.controller('RosaABFController', ['$scope', 'LocalesHelper', function($scope, LocalesHelper) { + + $scope.init = function(locale) { + LocalesHelper.setLocale(locale); + } + +}]); \ No newline at end of file diff --git a/app/assets/javascripts/angularjs/locales.js b/app/assets/javascripts/angularjs/locales.js new file mode 100644 index 000000000..b77b39c2e --- /dev/null +++ b/app/assets/javascripts/angularjs/locales.js @@ -0,0 +1,24 @@ +var _locales = { + 'ru-ru': { + 'total_branches': [ + 'Всего %1 ветка', + 'Всего %1 ветки', + 'Всего %1 веток' + ], + 'total_tags': [ + 'Всего %1 тег', + 'Всего %1 тега', + 'Всего %1 тегов' + ] + }, + 'en-us': { + 'total_branches': [ + 'Total %1 branch', + 'Total %1 branches' + ], + 'total_tags': [ + 'Total %1 tag', + 'Total %1 tags' + ] + } +}; \ No newline at end of file diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index bc42dba90..6e7b4291b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -19,6 +19,7 @@ // require angular-resource //= require unstable/angular-resource //= require ng-rails-csrf +//= require angular-i18n //= require_tree ./angularjs //= require moment diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 754a2ada2..1083dcf30 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,7 +12,7 @@ - if user_signed_in? = auto_discovery_link_tag :atom, atom_activity_feeds_path(:format => 'atom', :token => current_user.authentication_token), :title => t("layout.atom_link_tag_title", :nickname => current_user.uname, :app_name => APP_CONFIG['project_name']) - %body{'ng-app' => 'RosaABF'} + %body{'ng-app' => 'RosaABF', 'ng-controller' => 'RosaABFController', 'ng-init' => "init('#{I18n.locale}')"} .wrap{:class => content_for?(:sidebar) ? 'columns' : ''} %header .left diff --git a/app/views/projects/git/trees/branches.html.haml b/app/views/projects/git/trees/branches.html.haml index 15c41a483..5a8a8c18b 100644 --- a/app/views/projects/git/trees/branches.html.haml +++ b/app/views/projects/git/trees/branches.html.haml @@ -8,13 +8,7 @@ %h3= t('layout.projects.branches') %p{'ng-show' => '!branches.length'}= t('layout.projects.no_branches') - %p{'ng-show' => 'branches.length == 1'} - = t('layout.projects.total_branches', :count => '{{branches.length}}', :branches => t('layout.projects.pluralize.branches.branch')) - %p{'ng-show' => 'branches.length > 1 && branches.length < 5'} - = t('layout.projects.total_branches', :count => '{{branches.length}}', :branches => t('layout.projects.pluralize.branches.branches2')) - %p{'ng-show' => 'branches.length > 4'} - = t('layout.projects.total_branches', :count => '{{branches.length}}', :branches => t('layout.projects.pluralize.branches.branches')) - + %p{'ng-show' => 'branches.length > 0'} {{'total_branches' | i18n:'plural':branches.length}} .both %br diff --git a/app/views/projects/git/trees/tags.html.haml b/app/views/projects/git/trees/tags.html.haml index 6d09ac441..d6185754b 100644 --- a/app/views/projects/git/trees/tags.html.haml +++ b/app/views/projects/git/trees/tags.html.haml @@ -7,12 +7,7 @@ %h3= t('layout.projects.tags') %p{'ng-show' => '!tags.length'}= t('layout.projects.no_tags') - %p{'ng-show' => 'tags.length == 1'} - = t('layout.projects.total_tags', :count => '{{tags.length}}', :tags => t('layout.projects.pluralize.tags.tag')) - %p{'ng-show' => 'tags.length > 1 && tags.length < 5'} - = t('layout.projects.total_tags', :count => '{{tags.length}}', :tags => t('layout.projects.pluralize.tags.tags2')) - %p{'ng-show' => 'tags.length > 4'} - = t('layout.projects.total_tags', :count => '{{tags.length}}', :tags => t('layout.projects.pluralize.tags.tags')) + %p{'ng-show' => 'tags.length > 0'} {{'total_tags' | i18n:'plural':tags.length}} .both %br diff --git a/config/locales/models/project.en.yml b/config/locales/models/project.en.yml index cbbc29261..1492f8fb9 100644 --- a/config/locales/models/project.en.yml +++ b/config/locales/models/project.en.yml @@ -2,27 +2,14 @@ en: layout: projects: branches: Branches - - pluralize: - branches: - branch: branch - branches: branches - branches2: branches - tags: - tag: tag - tags: tags - tags2: tags - delete_branch: Delete branch restore_branch: Restore branch - total_branches: Total %{count} %{branches} no_branches: No branches base_branch: Base branch compare: Compare browse_code: Browse code source_code: Source code (%{type}) tags: Tags - total_tags: Total %{count} %{tags} no_tags: No tags add: Add public_projects_list: Public projects list diff --git a/config/locales/models/project.ru.yml b/config/locales/models/project.ru.yml index 6bcbb951b..a647bb2b7 100644 --- a/config/locales/models/project.ru.yml +++ b/config/locales/models/project.ru.yml @@ -2,27 +2,14 @@ ru: layout: projects: branches: Ветки - - pluralize: - branches: - branch: ветка - branches: веток - branches2: ветки - tags: - tag: тег - tags: тегов - tags2: тега - delete_branch: Удалить ветку restore_branch: Востановить ветку - total_branches: Всего %{count} %{branches} no_branches: Нет веток base_branch: Текущая ветка compare: Сравнить browse_code: Просмотреть код source_code: Исходный код (%{type}) tags: Теги - total_tags: Всего %{count} %{tags} no_tags: Нет тегов add: Добавить public_projects_list: Список публичных проектов