1 /*
2  * Copyright (c) 1997 - 2001 Hansj�rg Malthaner
3  *
4  * This file is part of the Simutrans project under the artistic licence.
5  * (see licence.txt)
6  */
7 #ifndef TRANSLATOR_H
8 #define TRANSLATOR_H
9 
10 #include <stdio.h>
11 #include <string>
12 #include "../tpl/stringhashtable_tpl.h"
13 #include "../tpl/vector_tpl.h"
14 
15 
16 /**
17  * Central location for loading and translating language text for the
18  * UI of Simutrans.
19  *
20  * The languages are 0 based index, with a valid range of(with lang being
21  * required language): <code>0 <= lang < lang_count</code>.
22  *
23  * @author Hj. Malthaner, Adam Barclay
24  */
25 class translator
26 {
27 private:
28 	//cannot be instantiated outside translator
translator()29 	translator() { current_lang = -1; }
30 
31 	int current_lang;
32 	int lang_count;
33 
34 	/* The single instance that this class will use to gain access to
35 	 * the member variables such as language names
36 	 */
37 	static translator single_instance;
38 
39 	/* Methods related to loading a language file into memory */
40 	static void load_language_file(FILE* file);
41 	static void load_language_iso(const std::string &iso);
42 
43 	static vector_tpl<char*> city_name_list;
44 	static vector_tpl<char*> street_name_list;
45 
46 	static void load_custom_list( int lang, vector_tpl<char*> &name_list, const char *fileprefix );
47 
48 public:
49 	struct lang_info {
50 		const char* translate(const char* text) const;
51 
52 		stringhashtable_tpl<const char*> texts;
53 		const char *name;
54 		const char *iso;
55 		const char *iso_base;
56 		bool is_latin2_based;
57 		uint8 ellipsis_width;
58 	};
59 
60 	static void init_custom_names(int lang);
61 
get_city_name_list()62 	static const vector_tpl<char*> &get_city_name_list() { return city_name_list; }
get_street_name_list()63 	static const vector_tpl<char*> &get_street_name_list() { return street_name_list; }
64 
65 	/**
66 	 * Loads up all files of language type from the 'language' directory.
67 	 * This method must be called for languages to be loaded up, undefined
68 	 * behaviour may follow if calls to translate message or similar are
69 	 * called before load has been called
70 	 */
71 	static bool load(const std::string &pakset_path);
72 
73 	/**
74 	 * Loads all language file in folder folder_name
75 	 * folder_name is relative to current dir (set by chdir)
76 	 */
77 	static void load_files_from_folder(const char* folder_name, const char* what);
78 
79 	/**
80 	 * Get/Set the currently selected language, based on the
81 	 * index number
82 	 */
get_language()83 	static int get_language() {
84 		return single_instance.current_lang;
85 	}
86 
87 	// returns the id for this language or -1 if not there
88 	static int get_language(const char* iso);
89 
90 	/** Get information about the currently selected language */
91 	static const lang_info* get_lang();
92 
93 	static const lang_info* get_langs();
94 
95 	/**
96 	 * First checks to see whether the language is in bounds, will
97 	 * then change what language is being used, otherwise prints
98 	 * an error message, leaving the language as it is
99 	 */
100 	static void set_language(int lang);
101 	static void set_language(const char* iso);
102 
103 	/**
104 	 * Returns the number of loaded languages.
105 	 */
get_language_count()106 	static int get_language_count() { return single_instance.lang_count; }
107 
108 	/**
109 	 * Translates a given string(key) to its locale
110 	 * specific counterpart, using the current language
111 	 * table.
112 	 * the second variant just uses the language with the index
113 	 * @return translated string, (null) if string is null,
114 	 * or the string if the translation is not found
115 	 */
116 	static const char *translate(const char* str);
117 	static const char *translate(const char* str, int lang);
118 
119 	/**
120 	 * @return replacement info for almost any object within the game
121 	 */
122 	static const char *compatibility_name(const char* str);
123 
124 	// return the name of the month
125 	static const char *get_month_name(uint16 month);
126 	// return the short name of the month
127 	static const char *get_short_month_name(uint16 month);
128 	// return date in selected format
129 	static const char *get_date(uint16 year, uint16 month);
130 	static const char *get_date(uint16 year, uint16 month, uint16 day, char const* season);
131 	static const char *get_short_date(uint16 year, uint16 month);
132 };
133 
134 #endif
135