Skip to content

Commit

Permalink
Merge pull request #3728 from gibkigonzo/feature/remove-prefech-dayjs…
Browse files Browse the repository at this point in the history
…-locales

remove prefech dayjs locales
  • Loading branch information
andrzejewsky authored Oct 25, 2019
2 parents 9cca552 + 5164f46 commit 3933f4f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added unit tests for `core/modules/mailer` - @krskibin (#3710)
- Get payment methods with billing address data - @rain2o (#2878)
- Added custom page-size parameter for `category-next/loadCategoryProducts` action - @cewald (#3713, #3714)
- Remove unused dayjs locales - @gibkigonzo (#3498)
- check max quantity in microcart - @gibkigonzo (#3314)

## [1.11.0-rc.1] - 2019.10.03
Expand Down
2 changes: 2 additions & 0 deletions core/build/webpack.base.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildLocaleIgnorePattern } from './../i18n/helpers';
import path from 'path';
import config from 'config';
import fs from 'fs';
Expand Down Expand Up @@ -60,6 +61,7 @@ const isProd = process.env.NODE_ENV === 'production'
// todo: usemultipage-webpack-plugin for multistore
export default {
plugins: [
new webpack.ContextReplacementPlugin(/dayjs[/\\]locale$/, buildLocaleIgnorePattern()),
new webpack.ProgressPlugin(),
// new BundleAnalyzerPlugin({
// generateStatsFile: true
Expand Down
29 changes: 29 additions & 0 deletions core/i18n/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import config from 'config'

export const currentBuildLocales = (): string[] => {
const defaultLocale = config.i18n.defaultLocale || 'en-US'
const multistoreLocales = config.storeViews.multistore
? Object.values(config.storeViews)
.map((store: any) => store && typeof store === 'object' && store.i18n && store.i18n.defaultLocale)
.filter(Boolean)
: []
const locales = multistoreLocales.includes(defaultLocale)
? multistoreLocales
: [defaultLocale, ...multistoreLocales]

return locales
}

export const transformToShortLocales = (locales: string[]): string[] => locales.map(locale => {
const separatorIndex = locale.indexOf('-')
const shortLocale = separatorIndex ? locale.substr(0, separatorIndex) : locale

return shortLocale
})

export const buildLocaleIgnorePattern = (): RegExp => {
const locales = transformToShortLocales(currentBuildLocales())
const localesRegex = locales.map(locale => `${locale}$`).join('|')

return new RegExp(localesRegex)
}
4 changes: 2 additions & 2 deletions core/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ function setI18nLanguage (lang: string): string {
const loadDateLocales = async (lang: string = 'en'): Promise<void> => {
let localeCode = lang.toLocaleLowerCase()
try { // try to load full locale name
await import(/* webpackChunkName: "dayjs-locales" */ `dayjs/locale/${localeCode}`)
await import(/* webpackChunkName: "dayjs-locales-[request]" */ `dayjs/locale/${localeCode}`)
} catch (e) { // load simplified locale name, example: de-DE -> de
const separatorIndex = localeCode.indexOf('-')
if (separatorIndex) {
localeCode = separatorIndex ? localeCode.substr(0, separatorIndex) : localeCode
await import(/* webpackChunkName: "dayjs-locales" */ `dayjs/locale/${localeCode}`)
await import(/* webpackChunkName: "dayjs-locales-[request]" */ `dayjs/locale/${localeCode}`)
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions core/i18n/scripts/translation.preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const path = require('path')
const dsvFormat = require('d3-dsv').dsvFormat
const dsv = dsvFormat(',')
const { currentBuildLocales } = require('../helpers')

/**
* Converts an Array to an Object
Expand All @@ -15,14 +16,18 @@ function convertToObject (array) {
}

module.exports = function (csvDirectories, config = null) {
const currentLocales = currentBuildLocales()
const fallbackLocale = 'en-US'
let messages = {}
let languages = []

// get messages from CSV files
csvDirectories.forEach(directory => {
fs.readdirSync(directory).forEach(file => {
const fullFileName = path.join(directory, file)
const extName = path.extname(fullFileName)
const baseName = path.posix.basename(file, extName)

if (extName === '.csv') {
const fileContent = fs.readFileSync(fullFileName, 'utf8')
if (languages.indexOf(baseName) === -1) {
Expand All @@ -34,24 +39,27 @@ module.exports = function (csvDirectories, config = null) {
})
})

languages.forEach((language) => {
if (!config || !config.i18n.bundleAllStoreviewLanguages || (config.i18n.bundleAllStoreviewLanguages && language === 'en-US')) {
console.debug(`Writing JSON file: ${language}.json`)
fs.writeFileSync(path.join(__dirname, '../resource/i18n', `${language}.json`), JSON.stringify(messages[language]))
}
})
// create fallback
console.debug(`Writing JSON file fallback: ${fallbackLocale}.json`)
fs.writeFileSync(path.join(__dirname, '../resource/i18n', `${fallbackLocale}.json`), JSON.stringify(messages[fallbackLocale]))

// bundle all messages in one file
if (config && config.i18n.bundleAllStoreviewLanguages) {
const bundledLanguages = { 'en-US': messages['en-US'] } // fallback locale
const bundledLanguages = { [fallbackLocale]: messages[fallbackLocale] } // fallback locale
bundledLanguages[config.i18n.defaultLocale] = messages[config.i18n.defaultLocale] // default locale
Object.keys(config.storeViews).forEach((storeCode) => {
const store = config.storeViews[storeCode]
if (store && typeof store === 'object' && store.i18n) {
bundledLanguages[store.i18n.defaultLocale] = messages[store.i18n.defaultLocale]
}
currentLocales.forEach((locale) => {
bundledLanguages[locale] = messages[locale]
})

console.debug(`Writing JSON file multistoreLanguages`)
fs.writeFileSync(path.join(__dirname, '../resource/i18n', `multistoreLanguages.json`), JSON.stringify(bundledLanguages))
} else {
currentLocales.forEach((language) => {
if (language !== fallbackLocale) return // it's already loaded
const filePath = path.join(__dirname, '../resource/i18n', `${language}.json`)
console.debug(`Writing JSON file: ${language}.json`)
fs.writeFileSync(filePath, JSON.stringify(messages[language]))
})
fs.writeFileSync(path.join(__dirname, '../resource/i18n', `multistoreLanguages.json`), JSON.stringify({})) // fix for webpack compilation error in case of `bundleAllStoreviewLanguages` = `false` (#3188)
}
}

0 comments on commit 3933f4f

Please sign in to comment.