diff --git a/app/assets/javascripts/admin/translations.js.erb b/app/assets/javascripts/admin/translations.js.erb deleted file mode 100644 index 1f70ef3feb0..00000000000 --- a/app/assets/javascripts/admin/translations.js.erb +++ /dev/null @@ -1,7 +0,0 @@ -//= depend_on 'client.en.yml' - -<% SimplesIdeias::I18n.assert_usable_configuration! %> -<% admin = SimplesIdeias::I18n.translation_segments['app/assets/javascripts/i18n/admin.en.js'] - admin[:en][:js] = admin[:en].delete(:admin_js) -%> -jQuery.extend(true, I18n.translations, <%= admin.to_json %>); diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index b5da1a23804..e4ca919bbce 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -21,7 +21,10 @@ // The rest of the externals //= require_tree ./external //= require i18n -//= require discourse/translations + +// The following needs to go right after including i18n, because other scripts +// below require correctly set locale already +//= require init_locale //= require ./discourse/helpers/i18n_helpers //= require ./discourse diff --git a/app/assets/javascripts/discourse/translations.js.erb b/app/assets/javascripts/discourse/translations.js.erb deleted file mode 100644 index 4f30f5b5adf..00000000000 --- a/app/assets/javascripts/discourse/translations.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -//= depend_on 'client.en.yml' - -<% SimplesIdeias::I18n.assert_usable_configuration! %> -var I18n = I18n || {}; -I18n.translations = <%= SimplesIdeias::I18n.translation_segments['app/assets/javascripts/i18n/en.js'].to_json %>; diff --git a/app/assets/javascripts/init_locale.js b/app/assets/javascripts/init_locale.js new file mode 100644 index 00000000000..e6ad0a3ccdc --- /dev/null +++ b/app/assets/javascripts/init_locale.js @@ -0,0 +1 @@ +I18n.locale = window.currentLocale; diff --git a/app/assets/javascripts/locales/en.js.erb b/app/assets/javascripts/locales/en.js.erb new file mode 100644 index 00000000000..0158aa80b9e --- /dev/null +++ b/app/assets/javascripts/locales/en.js.erb @@ -0,0 +1,2 @@ +//= depend_on 'client.en.yml' +<%= JsLocaleHelper.output_locale(:en); %> diff --git a/app/assets/javascripts/locales/fr.js.erb b/app/assets/javascripts/locales/fr.js.erb new file mode 100644 index 00000000000..6f49253c8e9 --- /dev/null +++ b/app/assets/javascripts/locales/fr.js.erb @@ -0,0 +1,2 @@ +//= depend_on 'client.fr.yml' +<%= JsLocaleHelper.output_locale(:fr); %> diff --git a/app/assets/javascripts/locales/nl.js.erb b/app/assets/javascripts/locales/nl.js.erb new file mode 100644 index 00000000000..7257ebe0281 --- /dev/null +++ b/app/assets/javascripts/locales/nl.js.erb @@ -0,0 +1,2 @@ +//= depend_on 'client.nl.yml' +<%= JsLocaleHelper.output_locale(:nl); %> diff --git a/app/assets/javascripts/locales/pseudo.js.erb b/app/assets/javascripts/locales/pseudo.js.erb new file mode 100644 index 00000000000..7e7f795a6d4 --- /dev/null +++ b/app/assets/javascripts/locales/pseudo.js.erb @@ -0,0 +1,2 @@ +//= depend_on 'client.pseudo.yml' +<%= JsLocaleHelper.output_locale(:pseudo); %> diff --git a/app/helpers/js_locale_helper.rb b/app/helpers/js_locale_helper.rb new file mode 100644 index 00000000000..06eceab9e23 --- /dev/null +++ b/app/helpers/js_locale_helper.rb @@ -0,0 +1,21 @@ +module JsLocaleHelper + + def self.output_locale(locale) + + SimplesIdeias::I18n.assert_usable_configuration! + + s = "var I18n = I18n || {};" + segment = "app/assets/javascripts/i18n/#{locale}.js" + s += "I18n.translations = " + SimplesIdeias::I18n.translation_segments[segment].to_json + ";" + + segment = "app/assets/javascripts/i18n/admin.#{locale}.js" + admin = SimplesIdeias::I18n.translation_segments[segment] + admin[locale][:js] = admin[locale].delete(:admin_js) + + s += "jQuery.extend(true, I18n.translations, " + admin.to_json + ");" + + s + + end + +end diff --git a/app/views/common/_discourse_javascript.html.erb b/app/views/common/_discourse_javascript.html.erb index f0f562d6465..df639828771 100644 --- a/app/views/common/_discourse_javascript.html.erb +++ b/app/views/common/_discourse_javascript.html.erb @@ -14,6 +14,13 @@ })(); +<%# load the selected locale before any other scripts %> +<%= javascript_include_tag "locales/#{I18n.locale}" %> +<%# store the locale into a variable, because the scripts need it, see init_locale.js %> + + <%- if mini_profiler_enabled? %> <%- Rack::MiniProfiler.step "application" do %> <%= javascript_include_tag "application" %> diff --git a/config/i18n-js.yml b/config/i18n-js.yml index b31fed9cefa..2b3b22304e6 100644 --- a/config/i18n-js.yml +++ b/config/i18n-js.yml @@ -25,4 +25,14 @@ translations: - file: 'app/assets/javascripts/i18n/en.js' only: 'en.js.*' - file: 'app/assets/javascripts/i18n/admin.en.js' - only: 'en.admin_js.*' \ No newline at end of file + only: 'en.admin_js.*' + + - file: 'app/assets/javascripts/i18n/pseudo.js' + only: 'pseudo.js.*' + - file: 'app/assets/javascripts/i18n/admin.pseudo.js' + only: 'pseudo.admin_js.*' + + - file: 'app/assets/javascripts/i18n/fr.js' + only: 'fr.js.*' + - file: 'app/assets/javascripts/i18n/admin.fr.js' + only: 'fr.admin_js.*' diff --git a/spec/javascripts/spec.js b/spec/javascripts/spec.js index 83c82a970f4..ad0dce77311 100644 --- a/spec/javascripts/spec.js +++ b/spec/javascripts/spec.js @@ -18,7 +18,6 @@ // The rest of the externals //= require_tree ../../app/assets/javascripts/external //= require i18n -//= require ../../app/assets/javascripts/discourse/translations //= require ../../app/assets/javascripts/discourse/helpers/i18n_helpers //= require ../../app/assets/javascripts/discourse