Skip to content

Instantly share code, notes, and snippets.

@johnnyshields
Created February 18, 2017 11:38
Show Gist options
  • Select an option

  • Save johnnyshields/98a695df51b1e99f3593579d3c9a3fd1 to your computer and use it in GitHub Desktop.

Select an option

Save johnnyshields/98a695df51b1e99f3593579d3c9a3fd1 to your computer and use it in GitHub Desktop.

Revisions

  1. johnnyshields created this gist Feb 18, 2017.
    21 changes: 21 additions & 0 deletions i18n helper methods
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@

    def current_path_for_locale(loc = I18n.locale, is_link = true)
    return 'javascript: void(0);' if is_link && I18n.locale == loc

    url_regex = /\A\/(?:(#{I18n.available_locales.join('|')})\/)?/
    current_page.url.gsub(url_regex, '').blank? ?
    home_for_locale(loc) :
    current_page.url.gsub(url_regex, root_for_locale(loc))
    end

    def path_for_locale(path, loc = I18n.locale)
    root_for_locale(loc) + path
    end

    def home_for_locale(loc = I18n.locale)
    root_for_locale(loc)
    end

    def root_for_locale(loc = I18n.locale)
    loc == :ja ? '/' : "/#{loc}/"
    end