Skip to content

Commit

Permalink
Beautify and generate lv_i18n.template by using support/...
Browse files Browse the repository at this point in the history
  • Loading branch information
bubeck committed Oct 4, 2024
1 parent 8c22d3f commit d8f89dd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 53 deletions.
16 changes: 6 additions & 10 deletions lib/compiler_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ static const char * ${loc}_plurals_${form}[] = {
let index = 0;
Object.values(data.pluralKeys).forEach(k => {
if (!data[l].plural || !data[l].plural[form] || !data[l].plural[form][k]) {
result += 'NULL, // ' + index + '=\"' + esc(k) + '\"\n';
result += ' NULL, // ' + index + '=\"' + esc(k) + '\"\n';
} else {
result += '\"' + esc(data[l].plural[form][k]) + '\", // ' + index + '=\"' + esc(k) + '\"\n';
result += ' \"' + esc(data[l].plural[form][k]) + '\", // ' + index + '=\"' + esc(k) + '\"\n';
}
index++;
});

result += `
};
`;
result += `};`;

return result.trim();

Expand All @@ -82,16 +80,14 @@ static const char * ${loc}_singulars[] = {
let index = 0;
Object.values(data.singularKeys).forEach(k => {
if (!data[l].singular || !data[l].singular[k]) {
result += 'NULL, // ' + index + '=\"' + esc(k) + '\"\n';
result += ' NULL, // ' + index + '=\"' + esc(k) + '\"\n';
} else {
result += '\"' + esc(data[l].singular[k]) + '\", // ' + index + '=\"' + esc(k) + '\"\n';
result += ' \"' + esc(data[l].singular[k]) + '\", // ' + index + '=\"' + esc(k) + '\"\n';
}
index++;
});

result += `
};
`;
result += `};`;

return result.trim();
}
Expand Down
106 changes: 63 additions & 43 deletions src/lv_i18n.template.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ static inline uint32_t op_f(uint32_t val) { UNUSED(val); return 0; }
static inline uint32_t op_t(uint32_t val) { UNUSED(val); return 0; }
static inline uint32_t op_e(uint32_t val) { UNUSED(val); return 0; }

static const char * en_gb_singulars[] = {
"english only", // 0="s_en_only"
"s translated", // 1="s_translated"
NULL, // 2="s_untranslated"
};

static const char * en_gb_plurals_one[] = {
"I have %d dog", // 0="p_i_have_dogs"
};

static const char * en_gb_plurals_other[] = {
"I have %d dogs", // 0="p_i_have_dogs"
};

static uint8_t en_gb_plural_fn(int32_t num)
{
uint32_t n = op_n(num); UNUSED(n);
Expand All @@ -33,59 +47,44 @@ static uint8_t en_gb_plural_fn(int32_t num)
return LV_I18N_PLURAL_TYPE_OTHER;
}

static uint8_t ru_ru_plural_fn(int32_t num)
{
uint32_t n = op_n(num); UNUSED(n);
uint32_t v = op_v(n); UNUSED(v);
uint32_t i = op_i(n); UNUSED(i);
uint32_t i10 = i % 10;
uint32_t i100 = i % 100;
if ((v == 0 && i10 == 1 && i100 != 11)) return LV_I18N_PLURAL_TYPE_ONE;
if ((v == 0 && (2 <= i10 && i10 <= 4) && (!(12 <= i100 && i100 <= 14)))) return LV_I18N_PLURAL_TYPE_FEW;
if ((v == 0 && i10 == 0) || (v == 0 && (5 <= i10 && i10 <= 9)) || (v == 0 && (11 <= i100 && i100 <= 14))) return LV_I18N_PLURAL_TYPE_MANY;
return LV_I18N_PLURAL_TYPE_OTHER;
}

static const char * en_gb_singulars[] = {
"english only", // 1=s_en_only
"s translated", // 2=s_translated
NULL, // 3="s_untranslated"
};

static const char * en_gb_plurals_one[] = {
"I have %d dog",
};

static const char * en_gb_plurals_other[] = {
"I have %d dogs",
static const lv_i18n_lang_t en_gb_lang = {
.locale_name = "en-GB",
.singulars = en_gb_singulars,
.plurals[LV_I18N_PLURAL_TYPE_ONE] = en_gb_plurals_one,
.plurals[LV_I18N_PLURAL_TYPE_OTHER] = en_gb_plurals_other,
.locale_plural_fn = en_gb_plural_fn
};

static const char * ru_ru_singulars[] = {
NULL, // 1="s_en_only"
"s переведено", // 2=s_translated
NULL, // 3="s_untranslated"
NULL, // 0="s_en_only"
"s переведено", // 1="s_translated"
NULL, // 2="s_untranslated"
};

static const char * ru_ru_plurals_one[] = {
"У меня %d собакен", // 1="p_i_have_dogs"
"У меня %d собакен", // 0="p_i_have_dogs"
};

static const char * ru_ru_plurals_few[] = {
"У меня %d собакена", // 1="p_i_have_dogs"
"У меня %d собакена", // 0="p_i_have_dogs"
};

static const char * ru_ru_plurals_many[] = {
"У меня %d собакенов", // 1="p_i_have_dogs"
"У меня %d собакенов", // 0="p_i_have_dogs"
};


static const lv_i18n_lang_t en_gb_lang = {
.locale_name = "en-GB",
.singulars = en_gb_singulars,
.plurals[LV_I18N_PLURAL_TYPE_ONE] = en_gb_plurals_one,
.plurals[LV_I18N_PLURAL_TYPE_OTHER] = en_gb_plurals_other,
.locale_plural_fn = en_gb_plural_fn
};
static uint8_t ru_ru_plural_fn(int32_t num)
{
uint32_t n = op_n(num); UNUSED(n);
uint32_t v = op_v(n); UNUSED(v);
uint32_t i = op_i(n); UNUSED(i);
uint32_t i10 = i % 10;
uint32_t i100 = i % 100;
if ((v == 0 && i10 == 1 && i100 != 11)) return LV_I18N_PLURAL_TYPE_ONE;
if ((v == 0 && (2 <= i10 && i10 <= 4) && (!(12 <= i100 && i100 <= 14)))) return LV_I18N_PLURAL_TYPE_FEW;
if ((v == 0 && i10 == 0) || (v == 0 && (5 <= i10 && i10 <= 9)) || (v == 0 && (11 <= i100 && i100 <= 14))) return LV_I18N_PLURAL_TYPE_MANY;
return LV_I18N_PLURAL_TYPE_OTHER;
}

static const lv_i18n_lang_t ru_ru_lang = {
.locale_name = "ru-RU",
Expand All @@ -96,26 +95,47 @@ static const lv_i18n_lang_t ru_ru_lang = {
.locale_plural_fn = ru_ru_plural_fn
};

static uint8_t de_de_plural_fn(int32_t num)
{
uint32_t n = op_n(num); UNUSED(n);
uint32_t i = op_i(n); UNUSED(i);
uint32_t v = op_v(n); UNUSED(v);

if ((i == 1 && v == 0)) return LV_I18N_PLURAL_TYPE_ONE;
return LV_I18N_PLURAL_TYPE_OTHER;
}

static const lv_i18n_lang_t de_de_lang = {
.locale_name = "de-DE",


.locale_plural_fn = de_de_plural_fn
};

const lv_i18n_language_pack_t lv_i18n_language_pack[] = {
&en_gb_lang,
&ru_ru_lang,
&de_de_lang,
NULL // End mark
};

#ifndef LV_I18N_OPTIMIZE

static const char * singular_idx[] = {
"s_en_only",
"s_translated",
"s_untranslated"
"s_en_only",
"s_translated",
"s_untranslated",

};

static const char * plural_idx[] = {
"p_i_have_dogs"
"p_i_have_dogs",

};

#endif


/*SAMPLE_END*/

/**
Expand Down
3 changes: 3 additions & 0 deletions src/lv_i18n.template.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ typedef const lv_i18n_lang_t * lv_i18n_language_pack_t;
extern const lv_i18n_language_pack_t lv_i18n_language_pack[];

/*SAMPLE_START*/
#undef LV_I18N_OPTIMIZE
#define LV_I18N_IDX_s(str) (!strcmp(str, "s_en_only")?0:(!strcmp(str, "s_translated")?1:(!strcmp(str, "s_untranslated")?2:LV_I18N_ID_NOT_FOUND)))
#define LV_I18N_IDX_p(str) (!strcmp(str, "p_i_have_dogs")?0:LV_I18N_ID_NOT_FOUND)

/*SAMPLE_END*/

Expand Down

0 comments on commit d8f89dd

Please sign in to comment.