Skip to content

Commit

Permalink
Merge pull request #18 from BKWLD/improve-2-letter-code-support
Browse files Browse the repository at this point in the history
Improve 2 letter locale code support
  • Loading branch information
weotch authored Sep 20, 2022
2 parents c931d73 + 9998df2 commit 41efe24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
8 changes: 6 additions & 2 deletions components/locale-selector/locale-selector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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')

</template>

Expand All @@ -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) =>
Expand Down
16 changes: 13 additions & 3 deletions components/locale-selector/locale.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

</template>
Expand All @@ -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: -> []
Expand All @@ -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
</script>

Expand Down
30 changes: 16 additions & 14 deletions modules/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})})
}})
}

Expand All @@ -75,16 +77,16 @@ 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+)$/)
if (match) return match[1].toLowerCase()
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+)\-/)
Expand Down

0 comments on commit 41efe24

Please sign in to comment.