Last active
December 19, 2015 19:29
-
-
Save langhard/6006886 to your computer and use it in GitHub Desktop.
AngularJS - Service - Localisation from TYPO3-XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('App') | |
.controller('localisationCtrl', | |
function ($scope, localisationService) { | |
localisationService.settings.language('de'); | |
localisationService.settings.file('../typo3conf/ext/extKey/Resources/Private/Language/locallang.xml'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('BackendApp') | |
.factory('localisationService', function ($rootScope, $http) { | |
/** | |
* Installation: | |
* Service require x2js (https://code.google.com/p/x2js) | |
* | |
* Definitions in controller: | |
* localisationService.settings.language('de'); | |
* localisationService.settings.file('../typo3conf/ext/extKey/Resources/Private/Language/locallang.xml'); | |
* | |
* Translation in view: | |
* {{service.localisation.key('keyword')}} | |
* | |
* Change language in view: | |
* <div ng-click="service.localisation.changeLanguage('de')">de</div> | |
*/ | |
if($rootScope.service === undefined){ | |
$rootScope.service = {}; | |
} | |
$rootScope.service.localisation = {}; | |
var settings = { | |
file: '', | |
language: '' | |
}; | |
// Translation object | |
var translation = {}; | |
// ---------------------------------------------------------------- | |
// Initialisation (get locallang-file and set translation object) | |
// ---------------------------------------------------------------- | |
function initialize(){ | |
$http.get(settings.file, {transformResponse: function (data) { | |
var x2js = new X2JS(); | |
var json = x2js.xml_str2json(data); | |
return json; | |
}}).success(function (data, status) { | |
angular.forEach(data.T3locallang.data.languageKey, function (languageObject) { | |
translation[languageObject._index] = languageObject; | |
angular.forEach(translation[languageObject._index]['label'], function (labelObject) { | |
translation[languageObject._index][labelObject._index] = labelObject.__text | |
}); | |
}) | |
}) | |
} | |
// ---------------------------------------------------------------- | |
// ROOTSCOPE: key-function | |
// ---------------------------------------------------------------- | |
$rootScope.service.localisation.key = function(translationKey) { | |
if(translation[settings.language] !== undefined && translation[settings.language][translationKey] !== undefined){ | |
return translation[settings.language][translationKey]; | |
} | |
else { | |
return 'No Translation for key "' + translationKey + '"'; | |
} | |
} | |
// ---------------------------------------------------------------- | |
// ROOTSCOPE: change-language-function | |
// ---------------------------------------------------------------- | |
$rootScope.service.localisation.changeLanguage = function(language){ | |
settings.language = language; | |
} | |
// ---------------------------------------------------------------- | |
// Settings | |
// ---------------------------------------------------------------- | |
return { | |
settings: { | |
file: function (file) { | |
settings.file = file; | |
initialize(); | |
}, | |
language: function (language) { | |
settings.language = language; | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment