Freeciv21
Develop your civilization from humble roots to a global empire
name_translation.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  __ __ part of Freeciv21. Freeciv21 is free software: you can
4 / \\..// \ redistribute it and/or modify it under the terms of the GNU
5  ( oo ) General Public License as published by the Free Software
6  \__/ Foundation, either version 3 of the License, or (at your
7  option) any later version. You should have received
8  a copy of the GNU General Public License along with Freeciv21. If not,
9  see https://www.gnu.org/licenses/.
10 **************************************************************************/
11 #pragma once
12 
13 // common
14 #include "fc_types.h" // MAX_LEN_NAME
15 
16 // Don't allow other modules to access directly to the fields.
17 #define vernacular _private_vernacular_
18 #define rulename _private_rulename_
19 #define translated _private_translated_
20 
21 /* Ruleset strings (such as names) are kept in their original vernacular
22  * as well as being translated to the current locale. */
24  const char *translated; // String doesn't need freeing.
25  char vernacular[MAX_LEN_NAME]; /* Original string,
26  * used for comparisons. */
27  char rulename[MAX_LEN_NAME]; /* Name used in savefiles etc.
28  Often the same as 'vernacular'. */
29 };
30 
31 // Inititalization macro.
32 #define NAME_INIT \
33  { \
34  nullptr, "\0", "\0" \
35  }
36 
37 /****************************************************************************
38  Initializes a name translation structure.
39 ****************************************************************************/
40 static inline void name_init(struct name_translation *ptrans)
41 {
42  ptrans->vernacular[0] = ptrans->rulename[0] = '\0';
43  ptrans->translated = nullptr;
44 }
45 
46 /****************************************************************************
47  Set the untranslated and rule names of the name translation structure.
48  If rule_name is nullptr, use vernacular_name for it (after removing any
49 i18n qualifier).
50 ****************************************************************************/
51 static inline void names_set(struct name_translation *ptrans,
52  const char *domain, const char *vernacular_name,
53  const char *rule_name)
54 {
55  static const char name_too_long[] = "Name \"%s\" too long; truncating.";
56 
57  (void) sz_loud_strlcpy(ptrans->vernacular, vernacular_name, name_too_long);
58  (void) sz_loud_strlcpy(ptrans->rulename,
59  rule_name ? rule_name : Qn_(vernacular_name),
61 
62  if (ptrans->vernacular[0] != '\0') {
63  // Translate now.
64  if (domain == nullptr) {
65  ptrans->translated = Q_(ptrans->vernacular);
66  } else {
67  ptrans->translated =
68  skip_intl_qualifier_prefix(DG_(domain, ptrans->vernacular));
69  }
70  } else {
71  ptrans->translated = ptrans->vernacular;
72  }
73 }
74 
75 /****************************************************************************
76  Set the untranslated name of the name translation structure.
77  Assumes the rule name should be based on the vernacular.
78 ****************************************************************************/
79 static inline void name_set(struct name_translation *ptrans,
80  const char *domain, const char *vernacular_name)
81 {
82  names_set(ptrans, domain, vernacular_name, nullptr);
83 }
84 
85 /****************************************************************************
86  Return the untranslated (vernacular) name of the name translation
87  structure.
88  Rarely used; you usually want name_translation() or rule_name().
89  Note that this does not discard any translation qualifiers! -- if this
90  string is to be displayed to the user (unlikely), the caller must call
91  Qn_() on it.
92 ****************************************************************************/
93 static inline const char *
94 untranslated_name(const struct name_translation *ptrans)
95 {
96  return ptrans->vernacular;
97 }
98 
99 /****************************************************************************
100  Return the rule name of the name translation structure.
101 ****************************************************************************/
102 static inline const char *
103 rule_name_get(const struct name_translation *ptrans)
104 {
105  return ptrans->rulename;
106 }
107 
108 /****************************************************************************
109  Return the translated name of the name translation structure.
110 ****************************************************************************/
111 static inline const char *
113 {
114  return ptrans->translated;
115 }
116 
117 // Don't allow other modules to access directly to the fields.
118 #undef vernacular
119 #undef rulename
120 #undef translated
#define MAX_LEN_NAME
Definition: fc_types.h:61
const char * skip_intl_qualifier_prefix(const char *str)
Some strings are ambiguous for translation.
Definition: fcintl.cpp:39
#define Q_(String)
Definition: fcintl.h:53
#define DG_(domain, String)
Definition: fcintl.h:51
#define Qn_(String)
Definition: fcintl.h:66
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * untranslated_name(const struct name_translation *ptrans)
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
static void name_init(struct name_translation *ptrans)
static const char name_too_long[]
Definition: ruleset.cpp:104
#define sz_loud_strlcpy(buffer, str, errmsg)
Definition: shared.h:127
char vernacular[MAX_LEN_NAME]
const char * translated
char rulename[MAX_LEN_NAME]