diff --git a/components/locale-selector/locale-selector.vue b/components/locale-selector/locale-selector.vue index e94674b..a29e85a 100644 --- a/components/locale-selector/locale-selector.vue +++ b/components/locale-selector/locale-selector.vue @@ -20,7 +20,8 @@ cloak-i18n-locale-selector-dropdown.locale-selector //- A locale option cloak-i18n-locale( :locale='locales[0]' - :language-locales='locales') + :language-locales='locales' + :redirect-home='redirectHome') @@ -30,10 +31,13 @@ cloak-i18n-locale-selector-dropdown.locale-selector import groupBy from 'lodash/groupBy' export default + props: + redirectHome: Boolean # Make links to homepages rather than current page + computed: # Get the current locale object - locale: -> @locales.find ({ code }) => code == @$i18n.locale + locale: -> @$i18n.localeProperties # Get the language locales of the current locale currentLocaleLanguages: -> @locales.filter (locale) => diff --git a/components/locale-selector/locale.vue b/components/locale-selector/locale.vue index d4071c8..2ff2cad 100644 --- a/components/locale-selector/locale.vue +++ b/components/locale-selector/locale.vue @@ -22,7 +22,7 @@ v-for='languageLocale in languageLocales' :key='languageLocale.languageCode' :aria-label='languageLocale.language' - :href='switchLocalePath(languageLocale.code)') + :href='makeUrl(languageLocale.code)') | {{ languageLocale.languageCode }} @@ -35,6 +35,7 @@ export default props: locale: Object # The locale object isLabel: Boolean # Disables links on country + redirectHome: Boolean # Make links to homepages rather than current page languageLocales: # List of alternative language options for the locale type: Array default: -> [] @@ -49,8 +50,17 @@ export default # The element to use on country links countryLink: -> if @isLabel then 'span' else 'a' - # The primary url for the locale - url: -> unless @isLabel then @switchLocalePath @locale.code + # Make the country level link + url: -> @makeUrl @locale.code unless @isLabel + + methods: + + # Make the URL to a locale code. These helper functions come + # from @nuxtjs/i18n + makeUrl: (code) -> + if @redirectHome + then @localePath '/', @locale.code + else @switchLocalePath @locale.code diff --git a/modules/default-options.js b/modules/default-options.js index 2ece49b..9f28a3c 100644 --- a/modules/default-options.js +++ b/modules/default-options.js @@ -54,17 +54,19 @@ export default function() { langDir: '~/', // Massage @cloak-app/i18n locales into the format expected by @nuxtjs/i18n - locales: locales.map(locale => defaultsDeep(locale, { - iso: locale.iso || locale.code, - file: join(__dirname, '../plugins/fetch-translations.js'), - - // Make vars used by Craft (where the site handle is snake-cased) - site: locale.site || locale.code.replace('_', '-'), - - // Make vars used by locale selector - countryCode: locale.countryCode || makeCountryCode(locale.code), - languageCode: locale.languageCode || makeLanguageCode(locale.code), - })) + locales: locales.map(locale => { + const iso = locale.iso || locale.code + return defaultsDeep(locale, { + iso, + file: join(__dirname, '../plugins/fetch-translations.js'), + + // Make vars used by Craft (where the site handle is snake-cased) + site: locale.site || locale.code.replace('_', '-'), + + // Make vars used by locale selector + countryCode: locale.countryCode || makeCountryCode(iso), + languageCode: locale.languageCode || makeLanguageCode(iso), + })}) }}) } @@ -75,7 +77,7 @@ function makeFallbackCode(locales) { if (match) return match.code } -// If there is a slash in the code, assume the latter part is the country +// If there is a dash in the code, assume the latter part is the country // code and return it. Otherwise, use the language code as country code function makeCountryCode(code) { const match = code.match(/\-(\w+)$/) @@ -83,8 +85,8 @@ function makeCountryCode(code) { else return code } -// If there is a slash in the code, assume the former part is the lanuage -// code and return it. Otherwise, if no slash, assume this is a code for a +// If there is a dash in the code, assume the former part is the lanuage +// code and return it. Otherwise, if no dash, assume this is a code for a // lanaguage only (ie "fr") with no country part function makeLanguageCode(code) { const match = code.match(/^(\w+)\-/)