tml-js-browser
Advanced tools
Comparing version 0.4.59 to 0.4.60
@@ -130,4 +130,11 @@ /** | ||
*/ | ||
getPathFragments: function (path) { | ||
getPathFragments: function (path, remove_prefix) { | ||
path = path || window.location.pathname; | ||
if (remove_prefix) { | ||
tml.logger.debug("removing prefix from path: " + remove_prefix); | ||
var re = new RegExp('^\/?' + remove_prefix.replace('/', '\/')); | ||
path = path.replace(re, ''); | ||
} | ||
return path.split('/').filter(function (n) { | ||
@@ -139,2 +146,11 @@ return n !== ''; | ||
/** | ||
* | ||
* @param locale | ||
*/ | ||
isValidLocale: function(locale) { | ||
if (locale == null) return false; | ||
return (locale.match(/^[a-z]{2}(-[A-Z]{2})?$/) !== null); | ||
}, | ||
/** | ||
* Gets current source from URL path | ||
@@ -154,5 +170,6 @@ * | ||
// for pre-path, remove the locale from path, and use the rest as the source | ||
if (utils.isObject(locale_method) && locale_method.strategy == 'pre-path') { | ||
var fragments = helpers.getPathFragments(current_source); | ||
fragments.shift(); | ||
if (utils.isObject(locale_method)) { | ||
var fragments = helpers.getPathFragments(current_source, locale_method.prefix); | ||
if (locale_method.strategy == 'pre-path' && helpers.isValidLocale(fragments[0])) | ||
fragments.shift(); | ||
current_source = fragments.join('/'); | ||
@@ -258,14 +275,22 @@ } | ||
// locale must be extracted from pre-path | ||
// options: { | ||
// strategy: 'pre-path', | ||
// prefix: '/wordpress' | ||
// } | ||
if (locale_method.strategy == 'pre-path') { | ||
tml.logger.debug("extracting locale from pre-path"); | ||
var fragments = window.location.pathname.split('/').filter(function (n) { | ||
return n !== ''; | ||
}); | ||
//console.log(elements); | ||
return fragments[0]; | ||
var fragments = helpers.getPathFragments(window.location.pathname, locale_method.prefix); | ||
if (helpers.isValidLocale(fragments[0])) | ||
return fragments[0]; | ||
return null; | ||
} | ||
// options: { | ||
// strategy: 'pre-domain' | ||
// } | ||
if (locale_method.strategy == 'pre-domain') { | ||
var subdomains = window.location.hostname.split('.'); | ||
return subdomains[0]; | ||
if (helpers.isValidLocale(subdomains[0])) | ||
return subdomains[0]; | ||
return null; | ||
} | ||
@@ -272,0 +297,0 @@ |
{ | ||
"name": "tml-js-browser", | ||
"description": "Translation plugin for browsers.", | ||
"version": "0.4.59", | ||
"version": "0.4.60", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Michael Berkovich", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
575575
12850