1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 10:39:27 +02:00

Add translation system.

This commit is contained in:
B. Petersen 2016-09-30 23:57:22 +02:00
parent e9f5e684db
commit 1c00af5107
5 changed files with 172 additions and 48 deletions

View file

@ -231,17 +231,17 @@ mrpoortext_t* mrchat_get_last_summary(mrchat_t* ths)
}
if( ths == NULL ) {
ret->m_text = safe_strdup("No chat.");
ret->m_text = safe_strdup("ErrNoChat"); /* should not happen */
return ret;
}
if( ths->m_last_msg == NULL ) {
ret->m_text = safe_strdup("No messages.");
ret->m_text = safe_strdup(mrstock_str(MR_STR_NO_MESSAGES));
return ret;
}
if( ths->m_last_msg->m_from_id == 0 ) {
ret->m_title = safe_strdup("You");
ret->m_title = safe_strdup(mrstock_str(MR_STR_YOU));
ret->m_title_meaning = MR_TITLE_USERNAME;
}
else {

View file

@ -55,6 +55,7 @@ extern "C" {
#include "mrimap.h"
#include "mrcontact.h"
#include "mrpoortext.h"
#include "mrstock.h"
typedef struct mrmailbox_t

101
src/mrstock.c Normal file
View file

@ -0,0 +1,101 @@
/*******************************************************************************
*
* Messenger Backend
* Copyright (C) 2016 Björn Petersen Software Design and Development
* Contact: r10s@b44t.com, http://b44t.com
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see http://www.gnu.org/licenses/ .
*
*******************************************************************************
*
* File: mrstock.h
* Authors: Björn Petersen
* Purpose: Add translated strings that are used by the messager backend
*
******************************************************************************/
#include <stdlib.h>
#include <memory.h>
#include "mrmailbox.h"
#include "mrtools.h"
static char** s_obj = NULL;
static int s_def_strings_added = 0;
static void mrstock_init_array_()
{
if( s_obj ) {
return; /* already initialized*/
}
size_t bytes_needed = sizeof(char*) * MR_STR_COUNT_;
if( (s_obj=malloc(bytes_needed)) == NULL ) {
exit(13); /* unrecoverable error */
}
memset(s_obj, 0, bytes_needed);
s_def_strings_added = 0;
}
void mrstock_exit(void)
{
if( s_obj ) {
size_t i;
for( i = 0; i < MR_STR_COUNT_; i++ ) {
if( s_obj[i] ) {
free(s_obj[i]);
}
}
free(s_obj);
s_obj = NULL;
}
s_def_strings_added = 0;
}
void mrstock_add_str(int id, const char* str)
{
if( id < 0 || id >= MR_STR_COUNT_ || str == NULL ) {
return; /* error */
}
mrstock_init_array_();
if( s_obj[id] ) {
free(s_obj[id]);
}
s_obj[id] = strdup(str);
}
const char* mrstock_str(int id)
{
/* init array */
mrstock_init_array_();
if( s_obj[id] == NULL && !s_def_strings_added ) {
/* init strings */
s_def_strings_added = 1;
mrstock_add_str(MR_STR_NO_CHAT, "No chat.");
mrstock_add_str(MR_STR_NO_MESSAGES, "No messages.");
mrstock_add_str(MR_STR_YOU, "You");
}
return s_obj[id]? s_obj[id] : "Err"; /* the result must not be freed! */
}

62
src/mrstock.h Normal file
View file

@ -0,0 +1,62 @@
/*******************************************************************************
*
* Messenger Backend
* Copyright (C) 2016 Björn Petersen Software Design and Development
* Contact: r10s@b44t.com, http://b44t.com
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see http://www.gnu.org/licenses/ .
*
*******************************************************************************
*
* File: mrstock.h
* Authors: Björn Petersen
* Purpose: Add translated strings that are used by the messager backend
*
******************************************************************************/
#ifndef __MRSTOCK_H__
#define __MRSTOCK_H__
#ifdef __cplusplus
extern "C" {
#endif
#define MR_STR_NO_CHAT 0 /* the IDs must not change! No gaps, please */
#define MR_STR_NO_MESSAGES 1
#define MR_STR_YOU 2
#define MR_STR_COUNT_ 3
/* mrstock_set_str() adds a string to the repository. A copy of the given string
is made. Usually, this is used to pass translated strings to the backend. */
void mrstock_add_str (int id, const char*);
/* frees all strings allocated by mrstock_set_str(). Usually, there is no need
to call this function - when the program terminates, usually all strings are
free automatically. However, this function may be handy if you watch the memory
for leaks using some special tools. */
void mrstock_exit (void);
/*** library-private **********************************************************/
const char* mrstock_str (int id); /* the result must not be freed! */
#ifdef __cplusplus
} /* /extern "C" */
#endif
#endif /* __MRSTOCKSTR_H__ */

View file

@ -553,63 +553,23 @@ time_t mr_timestamp_from_date(struct mailimf_date_time * date_time) /* from mail
}
static char* get_month_name(int zero_based_month)
{
const char* p = NULL;
switch( zero_based_month )
{
case 0: p = "Jan."; break;
case 1: p = "Feb."; break;
case 2: p = "Mar."; break;
case 3: p = "Apr."; break;
case 4: p = "May"; break;
case 5: p = "Jun."; break;
case 6: p = "Jul."; break;
case 7: p = "Aug."; break;
case 8: p = "Sep."; break;
case 9: p = "Oct."; break;
case 10: p = "Nov."; break;
case 11: p = "Dev."; break;
}
return safe_strdup(p);
}
char* mr_timestamp_to_str(time_t wanted)
{
char* temp;
struct tm wanted_struct;
memcpy(&wanted_struct, localtime(&wanted), sizeof(struct tm));
/* if you need the current time for relative dates, use the following lines:
time_t curr;
struct tm curr_struct;
time(&curr);
memcpy(&curr_struct, localtime(&curr), sizeof(struct tm));
*/
if( wanted_struct.tm_year == curr_struct.tm_year )
{
if( wanted_struct.tm_mday == curr_struct.tm_mday /* 1..31 */
&& wanted_struct.tm_mon == curr_struct.tm_mon ) /* 0..11 */
{
/* same year, same day - print time */
temp = sqlite3_mprintf("%02i:%02i", (int)wanted_struct.tm_hour, (int)wanted_struct.tm_min);
}
else
{
/* same year, different day/month - print date but year */
char* month_name = get_month_name(wanted_struct.tm_mon);
temp = sqlite3_mprintf("%02i. %s", (int)wanted_struct.tm_mday, month_name);
free(month_name);
}
}
else
{
/* different year - print whole date */
temp = sqlite3_mprintf("%02i.%02i.%04i", (int)wanted_struct.tm_mday, (int)wanted_struct.tm_mon+1, (int)wanted_struct.tm_year+1900);
}
temp = sqlite3_mprintf("%02i.%02i.%04i %02i:%02i:%02i",
(int)wanted_struct.tm_mday, (int)wanted_struct.tm_mon+1, (int)wanted_struct.tm_year+1900,
(int)wanted_struct.tm_hour, (int)wanted_struct.tm_min, (int)wanted_struct.tm_sec);
char* ret = safe_strdup(temp);
sqlite3_free(temp);