Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sencha touch impl

  • Loading branch information...
commit e4e92aa888a103e28b4615a594cedf9cd83d6ef4 0 parents
@elmasse authored
2  .gitignore
@@ -0,0 +1,2 @@
+sencha
+/sencha
0  README
No changes.
19 index.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+ <title>Ext.i18n.Bundle [Test] on Sencha Touch</title>
+
+ <link rel="stylesheet" type="text/css" href="sencha/resources/css/ext-touch.css" />
+ <script type="text/javascript" src="sencha/ext-touch-debug.js"></script>
+
+ <script type="text/javascript" src="js/PropertyReader.js"></script>
+ <script type="text/javascript" src="js/Bundle.js"></script>
+
+ <script type="text/javascript" src="js/test.js"></script>
+ </head>
+<body style="margin:10px;">
+
+
+</body>
+</html>
121 js/Bundle.js
@@ -0,0 +1,121 @@
+Ext.ns('Ext.i18n');
+
+/**
+ * @class Ext.18n.Bundle
+ * @constructor
+ * @version 0.2.1
+ * @param config.bundle: {String} Bundle Name.
+ * @param config.path: {String} Bundle Folder URI. Optional.
+ * @param config.lang: {String} Language in the form xx-YY where:
+ * xx: Language code (2 characters lowercase) YY: Country code (2 characters upercase).
+ * Optional. Default to browser's language. If it cannot be determined default to en-US.
+ * @param config.method: {String} request method. POST | GET. Optional. Default to POST
+ *
+ * @author Maximiliano Fierro (elmasse)
+ */
+Ext.i18n.Bundle = function(config){
+
+ this.bundle = config.bundle;
+ this.path = config.path;
+ this.language = this.formatLanguageCode(config.lang || this.guessLanguage());
+ this.method = config.method || 'GET';
+
+ var url = this.buildURL(this.language);
+
+ Ext.i18n.Bundle.superclass.constructor.call(this, {
+ autoLoad: true,
+ proxy: this.createProxy(url),
+ reader: new Ext.i18n.PropertyReader()
+ });
+
+ this.proxy.on('exception', this.loadParent, this, {single: true});
+};
+
+Ext.extend(Ext.i18n.Bundle, Ext.data.Store,{
+ defaultLanguage: 'en-US',
+ loadFlag: false,
+ resourceExt: '.properties',
+ bundle: '',
+ path: null,
+
+ /**
+ * @private
+ */
+ guessLanguage: function(){
+ return (navigator.language || navigator.browserLanguage
+ || navigator.userLanguage || this.defaultLanguage);
+ },
+
+ /**
+ * @method: getMsg
+ * Returns the content associated with the bundle key or {bundle key}.undefined if it is not specified.
+ * @param: key {String} Bundle key.
+ * @return: {String} The bundle key content.
+ */
+ getMsg: function(key){
+ return this.getById(key)? Ext.util.Format.htmlDecode(this.getById(key).get('value')) : key + '.undefined';
+ },
+
+ /**
+ * @method: onReady
+ * The fn will be called when the Bundle file is loaded.
+ * @param: fn {Function}
+ */
+ onReady: function(fn){
+ this.readyFn = fn;
+ this.on('loaded', this.readyFn, this, {single: true});
+ },
+
+ onProxyRead: function(operation) {
+ Ext.i18n.Bundle.superclass.onProxyRead.call(this, operation);
+
+ if(operation.wasSuccessful()){
+ this.fireEvent('loaded');
+ }
+ },
+
+
+ /**
+ * @private
+ */
+ buildURL: function(language){
+ var url = '';
+ if (this.path) url+= this.path + '/';
+ url+=this.bundle;
+ if (language) url+= '_'+language;
+ url+=this.resourceExt;
+ return url;
+ },
+
+ /**
+ * @private
+ */
+ loadParent: function(){
+ var url = this.buildURL();
+ this.proxy = this.createProxy(url);
+ this.load();
+ },
+
+ /**
+ * @private
+ */
+ createProxy: function(url){
+ return new Ext.data.HttpProxy({
+ url: url,
+ method: this.method,
+ defaultReaderType: 'property'
+ });
+ },
+
+ /**
+ * @private
+ */
+ formatLanguageCode: function(lang){
+ var langCodes = lang.split('-');
+ langCodes[0] = (langCodes[0]) ? langCodes[0].toLowerCase() : '';
+ langCodes[1] = (langCodes[1]) ? langCodes[1].toUpperCase() : '';
+ return langCodes.join('-');
+ }
+
+});
+
104 js/PropertyReader.js
@@ -0,0 +1,104 @@
+Ext.ns('Ext.i18n');
+/**
+ *
+ * PropertyReader is used to read data from a .properties files
+ * The .properties file might be in the traditional format which means that comments are accepted.
+ * key and value must be separated by the specified propertySeparator. By default a space is used.
+ * value must be in between " and not '
+ *
+ * @class Ext.i18n.PropertyReader
+ * @constructor
+ * @param config.propertySeparator {String}. The String that is used as separator between key and value. Default " ". Optional
+ *
+ * @author Maximiliano Fierro (elmasse)
+ */
+Ext.i18n.PropertyReader = function(config){
+
+ Ext.regModel('PropertyModel', {
+ idProperty: 'key',
+ fields: ['key', 'value']
+ });
+
+ config = config || {};
+
+ Ext.applyIf(config, {
+ idProperty: 'id',
+ successProperty: 'success',
+ totalProperty: 'total',
+ model: Ext.ModelMgr.types['PropertyModel']
+ });
+
+// recordType = recordType || Ext.data.Record.create(['value']);
+
+ //call super
+ Ext.i18n.PropertyReader.superclass.constructor.call(this, config);
+
+}
+
+Ext.extend(Ext.i18n.PropertyReader, Ext.data.Reader,{
+ /**
+ * @cfg propertySeparator: {String}. Default: " ". Optional
+ */
+ propertySeparator: " ",
+
+ read: function(response){
+ var propertyFile = response.responseText;
+ if(!propertyFile)
+ throw {message: "PropertyReader.read: File not found"};
+
+ return this.readRecords(propertyFile);
+ },
+
+ readRecords: function(propertyFile){
+ var totalRecords = 0, success = true;
+ var Record = this.recordType;
+ var records = [];
+
+ var f = this.readLines(propertyFile);
+
+ for(var i = 0; i < f.length; i++){
+ var key, value, kLim;
+ kLim = f[i].indexOf(this.propertySeparator);
+ key = String(f[i].substring(0, kLim));
+ value = f[i].substring(kLim+1).trim().replace(/\"/g, '');
+
+ var record = Ext.ModelMgr.create({
+ value : value,
+ key : key
+ }, 'PropertyModel');
+ //bad Model implementation
+ record.id = key;
+ records[i] = record;
+ }
+
+ return new Ext.data.ResultSet({
+ total : records.length,
+ count : records.length,
+ records: records,
+ success: success
+ });
+
+ },
+
+ createAccessor: function(){
+
+ },
+
+ //private
+ readLines: function(file){
+ var aux = String(file).split('\n');
+ var lines = new Array();
+
+ for(var i = 0; i < aux.length; i++){
+ if(aux[i].indexOf("#") < 0 || (aux[i].indexOf("#") > aux[i].indexOf("\""))){
+ var line = aux[i];//Ext.util.Format.trim(aux[i]);
+ if(line)
+ lines.push(line);
+ }
+ }
+ return lines;
+ }
+
+});
+
+Ext.data.ReaderMgr.registerType('property', Ext.i18n.PropertyReader);
40 js/test.js
@@ -0,0 +1,40 @@
+/*
+ * This is a test file for Bundle.js
+ *
+ * @author: elmasse(c) Maximiliano Fierro 2008
+ *
+ */
+
+
+
+Ext.onReady(function(){
+ var lang;
+ var params = Ext.urlDecode(window.location.search.substring(1));
+ if(params.lang)
+ lang = params.lang;
+
+ var bundle = new Ext.i18n.Bundle({bundle:'Application', path:'resources', lang: lang});
+ bundle.onReady(function(){
+
+ var toolbar = new Ext.Toolbar({
+ title: bundle.getMsg('panel.title'),
+ dock: 'top',
+ items: [{
+ text: String.format(bundle.getMsg('back.button'), 'Formated'),
+ ui: 'back'
+ },{xtype: 'spacer'},{
+ text: 'Home'
+ }]
+ });
+
+ var viewport = new Ext.Panel({
+ fullscreen: true,
+ animation: 'slide',
+ // scroll: 'vertical',
+ layout:{type: 'card'},
+ dockedItems: [toolbar],
+ html: bundle.getMsg('panel.html')
+ });
+
+ }); //bundle.onReady
+});
3  resources/Application.properties
@@ -0,0 +1,3 @@
+panel.title "'quoted panel title'"
+panel.html "Something goes here!"
+back.button "Go To {0}"
4 resources/Application_es-ES.properties
@@ -0,0 +1,4 @@
+#panel.title 'titulo del panel'
+panel.title "titulo del panel # sd"
+panel.html " Algo puede ir aca d'antoint \"no se\" "
+
Please sign in to comment.
Something went wrong with that request. Please try again.