summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Hughes <me@iamphill.com>2017-10-04 11:23:53 +0000
committerPhil Hughes <me@iamphill.com>2017-10-04 11:23:53 +0000
commitb9f78eae27b9784108e16821efc80d882378525b (patch)
tree7e6f349542472d2679fb5d8013f4c39eeb95960f
parent086784411c73d61a296e923b2274f81344646a77 (diff)
parent412571a4dfbf90d85df1393fa0075aacae27137d (diff)
downloadgitlab-ce-b9f78eae27b9784108e16821efc80d882378525b.tar.gz
Merge branch 'js-load-specific-locales' into 'master'
Load only the currently needed JS locale file Closes #38470 See merge request gitlab-org/gitlab-ce!14533
-rw-r--r--app/assets/javascripts/locale/index.js20
-rw-r--r--app/views/layouts/_head.html.haml2
-rw-r--r--config/application.rb1
-rw-r--r--config/initializers/gettext_rails_i18n_patch.rb14
-rw-r--r--config/webpack.config.js6
-rw-r--r--lib/tasks/gitlab/assets.rake2
6 files changed, 19 insertions, 26 deletions
diff --git a/app/assets/javascripts/locale/index.js b/app/assets/javascripts/locale/index.js
index af718e894cf..ce05b3eabec 100644
--- a/app/assets/javascripts/locale/index.js
+++ b/app/assets/javascripts/locale/index.js
@@ -1,30 +1,12 @@
import Jed from 'jed';
-
import sprintf from './sprintf';
-/**
- This is required to require all the translation folders in the current directory
- this saves us having to do this manually & keep up to date with new languages
-**/
-function requireAll(requireContext) { return requireContext.keys().map(requireContext); }
-
-const allLocales = requireAll(require.context('./', true, /^(?!.*(?:index.js$)).*\.js$/));
-const locales = allLocales.reduce((d, obj) => {
- const data = d;
- const localeKey = Object.keys(obj)[0];
-
- data[localeKey] = obj[localeKey];
-
- return data;
-}, {});
-
const langAttribute = document.querySelector('html').getAttribute('lang');
const lang = (langAttribute || 'en').replace(/-/g, '_');
-const locale = new Jed(locales[lang]);
+const locale = new Jed(window.translations || {});
/**
Translates `text`
-
@param text The text to be translated
@returns {String} The translated text
**/
diff --git a/app/views/layouts/_head.html.haml b/app/views/layouts/_head.html.haml
index e3a9e99250e..0d5350f873b 100644
--- a/app/views/layouts/_head.html.haml
+++ b/app/views/layouts/_head.html.haml
@@ -37,9 +37,9 @@
- if content_for?(:library_javascripts)
= yield :library_javascripts
+ = javascript_include_tag asset_path("locale/#{I18n.locale.to_s || I18n.default_locale.to_s}/app.js")
= webpack_bundle_tag "webpack_runtime"
= webpack_bundle_tag "common"
- = webpack_bundle_tag "locale"
= webpack_bundle_tag "main"
= webpack_bundle_tag "raven" if current_application_settings.clientside_sentry_enabled
= webpack_bundle_tag "test" if Rails.env.test?
diff --git a/config/application.rb b/config/application.rb
index 30117b6a98e..ca2ab83becc 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -105,6 +105,7 @@ module Gitlab
config.assets.precompile << "lib/ace.js"
config.assets.precompile << "vendor/assets/fonts/*"
config.assets.precompile << "test.css"
+ config.assets.precompile << "locale/**/app.js"
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
diff --git a/config/initializers/gettext_rails_i18n_patch.rb b/config/initializers/gettext_rails_i18n_patch.rb
index 377e5104f9d..49551319435 100644
--- a/config/initializers/gettext_rails_i18n_patch.rb
+++ b/config/initializers/gettext_rails_i18n_patch.rb
@@ -39,3 +39,17 @@ module GettextI18nRailsJs
end
end
end
+
+class PoToJson
+ # This is required to modify the JS locale file output to our import needs
+ # Overwrites: https://github.com/webhippie/po_to_json/blob/master/lib/po_to_json.rb#L46
+ def generate_for_jed(language, overwrite = {})
+ @options = parse_options(overwrite.merge(language: language))
+ @parsed ||= inject_meta(parse_document)
+
+ generated = build_json_for(build_jed_for(@parsed))
+ [
+ "window.translations = #{generated};"
+ ].join(" ")
+ end
+end
diff --git a/config/webpack.config.js b/config/webpack.config.js
index 2eb4444916b..c515a170d2d 100644
--- a/config/webpack.config.js
+++ b/config/webpack.config.js
@@ -123,10 +123,6 @@ var config = {
}
},
{
- test: /locale\/\w+\/(.*)\.js$/,
- loader: 'exports-loader?locales',
- },
- {
test: /monaco-editor\/\w+\/vs\/loader\.js$/,
use: [
{ loader: 'exports-loader', options: 'l.global' },
@@ -224,7 +220,7 @@ var config = {
// create cacheable common library bundles
new webpack.optimize.CommonsChunkPlugin({
- names: ['main', 'locale', 'common', 'webpack_runtime'],
+ names: ['main', 'common', 'webpack_runtime'],
}),
// enable scope hoisting
diff --git a/lib/tasks/gitlab/assets.rake b/lib/tasks/gitlab/assets.rake
index 259a755d724..a42f02a84fd 100644
--- a/lib/tasks/gitlab/assets.rake
+++ b/lib/tasks/gitlab/assets.rake
@@ -3,8 +3,8 @@ namespace :gitlab do
desc 'GitLab | Assets | Compile all frontend assets'
task compile: [
'yarn:check',
- 'rake:assets:precompile',
'gettext:po_to_json',
+ 'rake:assets:precompile',
'webpack:compile',
'fix_urls'
]