Skip to content

Commit

Permalink
feat(app generator): add a default locale
Browse files Browse the repository at this point in the history
Replace the hardcoded 'en-GB' locale with a generator prompted one.
  • Loading branch information
cueedee committed Aug 26, 2016
1 parent 9962873 commit 80deb5e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 102 deletions.
60 changes: 53 additions & 7 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var generators = require( 'yeoman-generator' )
, mkdirp = require( 'mkdirp' )
, chalk = require( 'chalk' )
, semver = require( 'semver' )
, tags = require( 'language-tags' )
, _ = require( 'lodash' )
;

Expand Down Expand Up @@ -104,6 +105,15 @@ var AppGenerator = generators.Base.extend(
}
);

this.option(
'i18nLocaleDefault'
, {
type: Boolean
, desc: 'Specify the default locale.'
, alias: 'locale'
}
);

this.option(
'ie8'
, {
Expand Down Expand Up @@ -209,6 +219,21 @@ var AppGenerator = generators.Base.extend(
, message: 'Do you need internationalisation support?'
, default: false
}
, {
type: 'input'
, name: 'i18nLocaleDefault'
, message: 'What should the default locale be? (Please use a valid [BCP 47 language tag](https://tools.ietf.org/html/bcp47#section-2))'
, default: ( youtil.definedToString( this.options.i18nLocaleDefault ) || 'en-US' )
, validate: tags.check
, filter: function ( value )
{
return tags( value ).format();
}
, when: function( answers )
{
return answers.i18n || this.templateData.i18n;
}.bind( this )
}
, {
type: 'confirm'
, name: 'ie8'
Expand Down Expand Up @@ -242,11 +267,24 @@ var AppGenerator = generators.Base.extend(

, configuring: function ()
{
var data = this.templateData;
var data = this.templateData
, localeDefaultOrig = data.i18nLocaleDefault
;

data.i18n = data.i18n || data.demo;
data.copyrightYear = new Date().getFullYear();
data.packageDescription = data.description;
if ( data.demo )
{
data.i18n = true;
data.i18nLocaleDefault = 'en-GB';
}

if ( data.i18n )
{
data.i18nLocaleDefaultLanguage = tags( data.i18nLocaleDefault ).language().descriptions()[0];
data.i18nLocaleDefaultRegion = tags( data.i18nLocaleDefault ).region().format();
}

data.copyrightYear = new Date().getFullYear();
data.packageDescription = data.description;

//
// Save a '.yo-rc.json' config file.
Expand All @@ -263,6 +301,13 @@ var AppGenerator = generators.Base.extend(
, i18n: data.i18n
}
);

if ( data.i18n )
{
// Save the intended default locale if any.
//
this.config.set( 'i18nLocaleDefault', localeDefaultOrig || data.i18nLocaleDefault );
}
}

, writing:
Expand Down Expand Up @@ -371,9 +416,10 @@ var AppGenerator = generators.Base.extend(

if ( data.i18n )
{
templates.push(
'src/i18n/en-GB.json'
, 'src/i18n/nl-NL.json'
// Need a name mapping for this one
//
this._templatesProcess(
{ 'src/i18n/bcp47-language-tag.json': [ 'src/i18n/' + data.i18nLocaleDefault + '.json' ] }
);
}
}
Expand Down
28 changes: 24 additions & 4 deletions generators/app/templates/src/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# <%- packageDescription %><% } %>
#
# @module App
#
###

'use strict'
Expand Down Expand Up @@ -263,7 +262,28 @@ settings.init( '$appRoot', $appRoot )<% if ( i18n ) { %>
##
## https://github.com/marviq/madlib-locale#readme
##
locale = require( 'madlib-locale' )<% } %>
localeManager = require( 'madlib-locale' )


###*
# The app's currently active locale as a BCP 47 language tag string representation.
#
# https://tools.ietf.org/html/bcp47#section-2
#
# @property locale
#
# @type String
# @default '<%= i18nLocaleDefault %>'
###

## Derive default locale from either the app root element, the document root, or failing that, a hardcoded default.
## Set `lang` attribute on the app's root element when missing.
##
locale = $appRoot.attr( 'lang' )

$appRoot.attr( 'lang', locale = $( 'html' ).attr( 'lang' ) ? '<%= i18nLocaleDefault %>' ) unless ( locale? )

settings.init( 'locale', locale )<% } %>


## ============================================================================
Expand All @@ -279,10 +299,10 @@ initialized = Q.all(
##
new Q.Promise( ( resolve ) -> $( resolve ); return; )<% if ( i18n ) { %>

## Initialize locale passing Handlebars runtime, default locale and base url for locale files.
## Initialize `localeManager`, passing in the Handlebars runtime, the default locale, and base url for locale files.
## Wait until it's been loaded.
##
locale.initialize( Handlebars, 'en-GB', "#{ appBaseUrl }i18n" )<% } %>
localeManager.initialize( Handlebars, locale, "#{ appBaseUrl }i18n" )<% } %>
]
)

Expand Down
29 changes: 29 additions & 0 deletions generators/app/templates/src/i18n/bcp47-language-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "<%- i18nLocaleDefault %>"
, "phrases":
{
}
, "formatting":
{
"number":
{
"decimalMarker": "."
, "thousandMarker": ","
, "precision": 3
}
, "datetime":
{
"date": "YYYY-MM-DD"
, "datetime": "YYYY-MM-DD HH:mm"
}
, "money":
{
"default": "euro"
, "euro":
{
"sign": "\u20AC"
, "precision": 2
}
}
}
}
46 changes: 0 additions & 46 deletions generators/app/templates/src/i18n/en-GB.json

This file was deleted.

41 changes: 0 additions & 41 deletions generators/app/templates/src/i18n/nl-NL.json

This file was deleted.

2 changes: 1 addition & 1 deletion generators/app/templates/src/index.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var buildRun = 'build-run=' + encodeURIComponent( buildRun );

%>
<html class="<%- npm.name %>" lang="en-GB">
<html class="<%- npm.name %>" <@ if ( i18n ) { @> lang="<@= i18nLocaleDefault @>"<@ } @>>
<head><%

//
Expand Down
17 changes: 14 additions & 3 deletions generators/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var generators = require( 'yeoman-generator' )
, chalk = require( 'chalk' )
, tags = require( 'language-tags' )
, _ = require( 'lodash' )
;

Expand Down Expand Up @@ -44,20 +45,30 @@ var DemoGenerator = generators.Base.extend(

, configuring: function()
{
var config = this.config;
var config = this.config
, locale = tags( 'en-GB' )
;

// A demo app implies 'i'+'nternationalisatio'.length+'n' support.
//
if ( !( config.get( 'i18n' )) )
{
config.set( 'i18n', true );

if ( !( config.get( 'i18nLocaleDefault' )) )
{
config.set( 'i18nLocaleDefault', locale.format() );
}
}

_.extend(
this.templateData
, {
ie8: config.get( 'ie8' )
, i18n: true
ie8: config.get( 'ie8' )
, i18n: true
, i18nLocaleDefault: locale.format()
, i18nLocaleDefaultLanguage: locale.language().descriptions()[0]
, i18nLocaleDefaultRegion: locale.region().format()
}
);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"chalk": "^1.1.3",
"language-tags": "^1.0.5",
"lodash": "^4.14.1",
"mkdirp": "^0.5.1",
"semver": "^5.2.0",
Expand Down

0 comments on commit 80deb5e

Please sign in to comment.