Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internationalization #28

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/components/ahoy_captain/comparison_link_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="dropdown dropdown-end" data-controller='dropdown-label'>
<label
tabindex="0"
class="cursor-pointer flex <%= classes %>"
data-action='click->dropdown-label#removeHidden'
>
<span data-dropdown-label-target="label"><%= title %></span>

</label>
<ul class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52" data-dropdown-label-target="close">
<% links.each do |link| %>
<li data-action="click->dropdown-label#setLabel">
<%= link %>
<li>
<% end %>
</ul>
</div>
16 changes: 10 additions & 6 deletions app/components/ahoy_captain/comparison_link_component.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# frozen_string_literal: true

class AhoyCaptain::ComparisonLinkComponent < AhoyCaptain::DropdownLinkComponent
class AhoyCaptain::ComparisonLinkComponent < ViewComponent::Base
include ::AhoyCaptain::CompareMode
include ::AhoyCaptain::RangeOptions
include ::AhoyCaptain::Rangeable

renders_many :links
renders_one :header

attr_reader :title, :classes
def initialize(title: "", classes: "btn btn-sm btn-base-100 no-underline hover:bg-base-100")
@classes = classes
end

# cheating
def title
self.with_option_content(options_for_option)
self.with_link_content(options_for_option)

comparison_mode.label
end
Expand All @@ -22,12 +26,12 @@ def render?

def options_for_option
[
(link_to "Disable Comparison", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: false))),
(link_to "Previous period", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :previous)), class: selected(:previous, :true)),
(link_to "Year-over-year", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :year)), class: selected(:year)),
(link_to "Custom period", "javascript:customComparisonModal.showModal()", class: selected(:custom)),
(link_to "Year-over-year", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :year)), class: selected(:year)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're already updating, change hardcoded strings to an i18n lookup, e.g., "Year-over-year" -> ".year-over-year"

(link_to "Previous period", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: :previous)), class: selected(:previous, :true)),
(link_to "Disable Comparison", AhoyCaptain::Engine.routes.url_helpers.root_path(**helpers.search_params.merge(comparison: false))),

].join.html_safe
].reverse.join.html_safe
end

private
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/ahoy_captain/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base
include RangeOptions
include Rangeable

around_action :use_ahoy_captain_locale

layout 'ahoy_captain/layouts/application'

def period
Expand All @@ -23,6 +25,33 @@ def period

private

def use_ahoy_captain_locale(&action)
@original_i18n_config = I18n.config
I18n.config = ::AhoyCaptain::I18nConfig.new
I18n.with_locale(current_locale, &action)
ensure
I18n.config = @original_i18n_config
@original_i18n_config = nil
end

def use_original_locale
prev_config = I18n.config
I18n.config = @original_i18n_config if @original_i18n_config
yield
ensure
I18n.config = prev_config
end

def current_locale
if request.GET['locale']
request.GET['locale']
elsif params[:locale]
params[:locale]
else
I18n.default_locale
end
end

def use_details_frame
if request.headers['Turbo-Frame'] == 'details'
request.variant = :details
Expand Down
23 changes: 23 additions & 0 deletions app/models/ahoy_captain/i18n_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module AhoyCaptain
class I18nConfig < ::I18n::Config
BACKEND = I18n::Backend::Simple.new
AVAILABLE_LOCALES = AhoyCaptain::Engine.root.join("config/locales/ahoy_captain").glob("*.yml").map { |path| File.basename(path, ".yml").to_sym }.uniq
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is logic we want to add to the initialize method rather than keeping in a constant?

AVAILABLE_LOCALES_SET = AVAILABLE_LOCALES.inject(Set.new) { |set, locale| set << locale.to_s << locale.to_sym }

def backend
BACKEND
end

def available_locales
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have two methods, available_locales and available_locales_set? Why not consolidate into a single available_locales method?

AVAILABLE_LOCALES
end

def available_locales_set
AVAILABLE_LOCALES_SET
end

def default_locale
AhoyCaptain.config.locale
end
end
end
2 changes: 1 addition & 1 deletion app/views/ahoy_captain/roots/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<% end %>

<%= render AhoyCaptain::TileComponent.new(title: 'Top Sources') do |component| %>
<%= render AhoyCaptain::TileComponent.new(title: t('ahoy_captain.tiles.titles.top_sources')) do |component| %>
<% component.with_display_links do %>
<div class="flex text-xs font-medium text-gray-400 space-x-2">
<div class="relative inline-block text-left">
Expand Down
4 changes: 2 additions & 2 deletions app/views/ahoy_captain/stats/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<%= turbo_frame_tag :stats, data: { controller: "active-frame-link" } do %>
<div class="grid grid-cols-1 divide-y divide-base-200 overflow-hidden rounded-lg grid-cols-2 md:grid-cols-6 md:divide-y-0" data-controller="active-links" data-active-links-classes-value='["text-primary"]'>
<% if @presenter.send(:range).realtime? %>
<%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "Unique Visits (30 min)", :number_with_delimiter, true) %>
<%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "#{t('ahoy_captain.stats.titles.unique_visits')} (30 min)", :number_with_delimiter, true) %>
<%= render stats_container(@presenter.total_pageviews, stats_total_pageviews_path(search_params), "Total Pageviews (30 min)", :number_with_delimiter) %>
<% else %>
<%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), "Unique Visits", :number_with_delimiter, true) %>
<%= render stats_container(@presenter.unique_visitors, stats_unique_visitors_url(search_params), t('ahoy_captain.stats.titles.unique_visits'), :number_with_delimiter, true) %>
<%= render stats_container(@presenter.total_visits, stats_total_visits_path(search_params), "Total Visits", :number_with_delimiter) %>
<%= render stats_container(@presenter.total_pageviews, stats_total_pageviews_path(search_params), "Total Pageviews", :number_with_delimiter) %>
<%= render stats_container(@presenter.views_per_visit, stats_views_per_visits_path(search_params), "Views per Visit", :number_with_delimiter) %>
Expand Down
10 changes: 10 additions & 0 deletions config/locales/ahoy_captain/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
en:
ahoy_captain:
tiles:
titles:
top_sources: Top Sources
stats:
titles:
unique_visits: Unique Visits

3 changes: 2 additions & 1 deletion lib/ahoy_captain/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module AhoyCaptain
class Configuration
attr_accessor :view_name, :theme, :realtime_interval
attr_accessor :view_name, :theme, :realtime_interval, :locale
attr_reader :goals, :funnels, :cache, :ranges, :disabled_widgets, :event, :models, :filters, :predicate_labels
def initialize
@goals = GoalCollection.new
Expand Down Expand Up @@ -35,6 +35,7 @@ def initialize

@realtime_interval = 30.seconds
@disabled_widgets = []
@locale = :en
end

def goal(id, &block)
Expand Down