From 510f9f19f44cf4393f0df81121387ee958eb3960 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sun, 6 May 2018 12:35:30 -0400 Subject: [PATCH] Add __ helper for common phrases --- assets/javascripts/lib/i18n.coffee | 22 ++++++++++++++----- .../javascripts/templates/notif_tmpl.coffee | 2 +- .../templates/pages/about_tmpl.coffee | 4 ++-- .../templates/pages/offline_tmpl.coffee | 2 +- .../javascripts/templates/sidebar_tmpl.coffee | 8 +++---- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/assets/javascripts/lib/i18n.coffee b/assets/javascripts/lib/i18n.coffee index 52c8fc4d..8c2e1410 100644 --- a/assets/javascripts/lib/i18n.coffee +++ b/assets/javascripts/lib/i18n.coffee @@ -1,6 +1,6 @@ class @I18n default: 'en' - constructor: (@langs = [@default]) -> + constructor: (@langs = [@default], @phrases) -> _: (obj, substitution) => if typeof obj is 'object' or typeof obj is 'function' @@ -8,14 +8,18 @@ class @I18n else obj - get_lang: (obj) -> + __: (key) => + lang = @get_lang @phrases, (lang) => @phrases[lang]?[key] + @phrases[lang]?[key] ? '' + + get_lang: (obj, test = (lang) -> obj[lang]) -> for lang in @langs # eg. en-US - return lang if obj[lang] + return lang if test lang lang = lang.replace /_\w+$/, '' # eg. en - return lang if obj[lang] + return lang if test lang @default @@ -26,5 +30,13 @@ class @I18n str = str.replace("{#{key}}", substitution[key]) str -@i18n = new I18n navigator.languages +@i18n = new I18n navigator.languages, + en: + enable: "Enable" + documentation: "Documentation" + ja: + enable: "有効" + documentation: "ドキュメンテーション" + @_ = @i18n._ +@__ = @i18n.__ diff --git a/assets/javascripts/templates/notif_tmpl.coffee b/assets/javascripts/templates/notif_tmpl.coffee index 2313b81d..01ccd9f5 100644 --- a/assets/javascripts/templates/notif_tmpl.coffee +++ b/assets/javascripts/templates/notif_tmpl.coffee @@ -82,7 +82,7 @@ app.templates.notifUpdates = (docs, disabledDocs) -> for doc in disabledDocs html += "
  • #{doc.name}" html += " #{doc.release}" if doc.release - html += """Enable""" + html += """#{__ 'enable'}""" html += '' notif 'Updates', "#{html}" diff --git a/assets/javascripts/templates/pages/about_tmpl.coffee b/assets/javascripts/templates/pages/about_tmpl.coffee index 30c25999..6c019352 100644 --- a/assets/javascripts/templates/pages/about_tmpl.coffee +++ b/assets/javascripts/templates/pages/about_tmpl.coffee @@ -68,7 +68,7 @@ app.templates.aboutPage =
    -
    Documentation + #{__ 'documentation'} Copyright License #{credit_table} @@ -148,7 +148,7 @@ app.templates.aboutPage =
    -
    Documentation + #{__ 'documentation'} Copyright License #{credit_table} diff --git a/assets/javascripts/templates/pages/offline_tmpl.coffee b/assets/javascripts/templates/pages/offline_tmpl.coffee index a9a3c21c..706dba89 100644 --- a/assets/javascripts/templates/pages/offline_tmpl.coffee +++ b/assets/javascripts/templates/pages/offline_tmpl.coffee @@ -13,7 +13,7 @@ app.templates.offlinePage = (docs) -> """
    - + diff --git a/assets/javascripts/templates/sidebar_tmpl.coffee b/assets/javascripts/templates/sidebar_tmpl.coffee index aa095505..4ee2f99e 100644 --- a/assets/javascripts/templates/sidebar_tmpl.coffee +++ b/assets/javascripts/templates/sidebar_tmpl.coffee @@ -7,7 +7,7 @@ templates.sidebarDoc = (doc, options = {}) -> link += if options.disabled then '_list-disabled' else '_list-dir' link += """" data-slug="#{doc.slug}" title="#{doc.fullName}" tabindex="-1">""" if options.disabled - link += """Enable""" + link += """#{__ 'enable'}""" else link += arrow link += """#{doc.release}""" if doc.release @@ -23,7 +23,7 @@ templates.sidebarEntry = (entry) -> templates.sidebarResult = (entry) -> addons = if entry.isIndex() and app.disabledDocs.contains(entry.doc) - """#{_ en: "Enable", ja: "有効"}""" + """#{__ 'enable'}""" else """""" addons += """#{entry.doc.short_version}""" if entry.doc.version and not entry.isIndex() @@ -66,9 +66,7 @@ templates.sidebarDisabledList = (html) -> templates.sidebarDisabledVersionedDoc = (doc, versions) -> """#{arrow}#{doc.name}
    #{versions}
    """ -templates.docPickerHeader = - en: """
    Documentation Enable
    """ - ja: """
    ドキュメント Enable
    """ +templates.docPickerHeader = -> """
    #{__ 'documentation'} #{__ 'enable'}
    """ templates.docPickerNote = en: """
    Documentation#{__ 'documentation'} Size Status Action