✨ Added LookupWithContext
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -4,3 +4,5 @@ cmake-build-debug/ | ||||
| \.idea/ | ||||
| docs/node_modules/ | ||||
| docs/doc/ | ||||
|  | ||||
| build/ | ||||
|   | ||||
| @@ -1,10 +1,10 @@ | ||||
|  | ||||
| env: | ||||
|    global: | ||||
|      - CONAN_REFERENCE: "MofileReader/0.1.2" | ||||
|      - CONAN_REFERENCE: "MofileReader/1.0.0" | ||||
|      - CONAN_USERNAME: "anotherfoxguy" | ||||
|      - CONAN_LOGIN_USERNAME: "anotherfoxguy" | ||||
|      - CONAN_CHANNEL: "MofileReader" | ||||
|      - CONAN_CHANNEL: "testing" | ||||
|      - CONAN_UPLOAD: "https://api.bintray.com/conan/anotherfoxguy/ror-dependencies" | ||||
|  | ||||
| linux: &linux | ||||
|   | ||||
| @@ -13,12 +13,9 @@ | ||||
| # possible settings. | ||||
| #------------------------------------------------------- | ||||
| cmake_minimum_required(VERSION 3.0) | ||||
| set_property(GLOBAL PROPERTY USE_FOLDERS ON) | ||||
| project(moFileReader) | ||||
|  | ||||
| # The main include directory | ||||
| include_directories(BEFORE ${CMAKE_SOURCE_DIR}/include) | ||||
|  | ||||
|  | ||||
| # Let the user choose between static lib and dll | ||||
| # To use it, call cmake -DCOMPILE_DLL=ON | ||||
| option(COMPILE_DLL "Set this to ON if you want to compile the library as an DLL. When this is OFF, a static library is created (default)." OFF) | ||||
| @@ -30,6 +27,9 @@ endif () | ||||
| add_library(moFileReader STATIC ${CMAKE_SOURCE_DIR}/src/moFileReader.cpp ${CMAKE_SOURCE_DIR}/src/mo.cpp) | ||||
| add_executable(moReader ${CMAKE_SOURCE_DIR}/src/mo.cpp) | ||||
|  | ||||
| target_include_directories(moFileReader PRIVATE ${CMAKE_SOURCE_DIR}/include) | ||||
| target_include_directories(moReader PRIVATE ${CMAKE_SOURCE_DIR}/include) | ||||
|  | ||||
| if (COMPILE_DLL) | ||||
|     target_compile_definitions(moReader PRIVATE _CONSOLE MOFILE_IMPORT) | ||||
| else () | ||||
| @@ -42,10 +42,6 @@ target_link_libraries(moReader moFileReader) | ||||
| option(BUILD_TEST "Set this to ON if you want to build the test" OFF) | ||||
|  | ||||
| if(BUILD_TEST) | ||||
|     enable_testing() | ||||
|     add_subdirectory(test) | ||||
| endif() | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -6,10 +6,10 @@ environment: | ||||
|     PYTHON_VERSION: "2.7.8" | ||||
|     PYTHON_ARCH: "32" | ||||
|  | ||||
|     CONAN_REFERENCE: "MofileReader/0.1.2" | ||||
|     CONAN_REFERENCE: "MofileReader/1.0.0" | ||||
|     CONAN_USERNAME: "anotherfoxguy" | ||||
|     CONAN_LOGIN_USERNAME: "anotherfoxguy" | ||||
|     CONAN_CHANNEL: "MofileReader" | ||||
|     CONAN_CHANNEL: "testing" | ||||
|     CONAN_UPLOAD: "https://api.bintray.com/conan/anotherfoxguy/ror-dependencies" | ||||
|     CONAN_VISUAL_VERSIONS: 15 | ||||
|  | ||||
|   | ||||
| @@ -3,7 +3,7 @@ import os | ||||
|  | ||||
| class MofilereaderConan(ConanFile): | ||||
|     name = "MofileReader" | ||||
|     version = "0.1.2" | ||||
|     version = "1.0.0" | ||||
|     license = "MIT" | ||||
|     url = "https://github.com/AnotherFoxGuy/conan-MofileReader/" | ||||
|     description = "This API lets you read .mo-Files and use their content just as you would do with GNUs gettext." | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com) | ||||
|  * All rights reserved. | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
| @@ -66,5 +67,3 @@ | ||||
|  | ||||
|  | ||||
| #endif /* __MOFILECONFIG_H_INCLUDED__ */ | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com) | ||||
|  * All rights reserved. | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
| @@ -45,29 +46,6 @@ | ||||
| #endif | ||||
|  | ||||
| /** \mainpage moFileReaderSDK | ||||
|   *  | ||||
|   * | ||||
|   * <h2>Compilation with Visual C++ (Express and better)</h2> | ||||
|   * | ||||
|   * We provide a project for Visual C++ 2008. You can select 3 Types of Compilation: | ||||
|   * | ||||
|   * <h3>Executable (Release or Debug)</h3> | ||||
|   * | ||||
|   * This will compile the code as an executable which can lookup strings from every .mo-File you load with it. | ||||
|   * This can be handy if you want to have a peek into a file or test something etc. I recommend building the  | ||||
|   * release-executable only if you just want to use it. | ||||
|   * | ||||
|   * <h3>Dynamic Loaded Library ( ReleaseDLL )</h3> | ||||
|   * | ||||
|   * This may be overkill, but perhaps you like it modular. This Configuration will create a dll and an import-library. | ||||
|   * Do not forget to link against the import-library and please define MOFILE_IMPORT in your preprocessor-settings,  | ||||
|   *  otherwise you will receive a bunch of linker-errors.  | ||||
|   * You will find all files in the directory "lib" in the Solutions Directory. | ||||
|   * | ||||
|   * <h3>Static Library ( ReleaseLIB )</h3> | ||||
|   * | ||||
|   * This will compile the code as a static library with no Entry-Point. This is the recommended usage.  | ||||
|   * But please do not forget to link against moFileReader.static.lib. Otherwise you will receive linker-errors. | ||||
|   * | ||||
|   * <h2>Compilation via cmake</h2> | ||||
|   * | ||||
| @@ -88,7 +66,7 @@ | ||||
|   * You will find the libraries in %%projectdir%%/lib and the binary in %%projectdir%%/bin | ||||
|   * | ||||
|   * | ||||
|   * <h2>None of those?</h2> | ||||
|   * <h2>Include in project</h2> | ||||
|   * | ||||
|   * The last option is to simply add moFileReader.cpp, moFileReader.h and moFileConfig.h to your project. Thats all you have to do. | ||||
|   * You can safely exclude mo.cpp, since this file keeps the entry-points of the .exe and .dll only. | ||||
| @@ -119,6 +97,12 @@ | ||||
|   * | ||||
|   * <h2>Changelog</h2> | ||||
|   * | ||||
|   * - Version 1.0.0 | ||||
|   *     - Added new function: LookupWithContext | ||||
|   *     - Added unit-tests | ||||
|   *     - Added support for packaging with Conan | ||||
|   *     - Moved project to https://github.com/AnotherFoxGuy/MofileReader | ||||
|   * | ||||
|   * - Version 0.1.2 | ||||
|   *     - Generic improvements to the documentation. | ||||
|   *     - Generic improvements to the code | ||||
| @@ -361,11 +345,20 @@ public: | ||||
|     virtual eErrorCode ReadFile(const char* filename); | ||||
|  | ||||
|     /** \brief Returns the searched translation or returns the input. | ||||
|       * \param[in,out] id The id of the translation to search for. | ||||
|       * \param[in] id The id of the translation to search for. | ||||
|       * \return The value you passed in via _id or the translated string. | ||||
|       */ | ||||
|     virtual std::string Lookup( const char* id ) const; | ||||
|  | ||||
|     /** \brief Returns the searched translation or returns the input, restricted to the context given by context. | ||||
|       * See https://www.gnu.org/software/gettext/manual/html_node/Contexts.html for more info. | ||||
|       * \param[in] context The id of the translation to search for. | ||||
|       * \param[in] id The id of the translation to search for. | ||||
|       * \return The value you passed in via _id or the translated string. | ||||
|       */ | ||||
|     virtual std::string LookupWithContext (const char* context, const char* id) const; | ||||
|  | ||||
|  | ||||
|     /// \brief Returns the Error Description. | ||||
|     virtual const std::string& GetErrorDescription() const; | ||||
|  | ||||
| @@ -455,6 +448,16 @@ inline std::string _(const char* id) | ||||
|     return r; | ||||
| } | ||||
|  | ||||
| /** \brief Looks for the spec. string to translate. | ||||
|   * \param[in] id The string-id to search. | ||||
|   * \return The translation if found, otherwise it returns id. | ||||
|   */ | ||||
| inline std::string _L(const char* id) | ||||
| { | ||||
|     std::string r = moFileReaderSingleton::GetInstance().Lookup(id); | ||||
|     return r; | ||||
| } | ||||
|  | ||||
| /// \brief Resets the Lookup-Table. | ||||
| inline void moFileClearTable() | ||||
| { | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com) | ||||
|  * All rights reserved. | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
| @@ -30,7 +31,7 @@ | ||||
|  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||
|  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  */ | ||||
| #include "../include/moFileReader.h" | ||||
| #include "moFileReader.h" | ||||
| #include <iostream> | ||||
| #include <cstdlib> | ||||
|  | ||||
| @@ -229,4 +230,3 @@ int WINAPI DllMain( DWORD reason, LPVOID) | ||||
| } | ||||
|  | ||||
| #endif /* Compilation-Mode */ | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* | ||||
|  * moFileReader - A simple .mo-File-Reader | ||||
|  * Copyright (C) 2009 Domenico Gentner (scorcher24@gmail.com) | ||||
|  * Copyright (C) 2018 Edgar (Edgar@AnotherFoxGuy.com) | ||||
|  * All rights reserved. | ||||
|  * | ||||
|  * Redistribution and use in source and binary forms, with or without | ||||
| @@ -30,7 +31,7 @@ | ||||
|  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||
|  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
|  */ | ||||
| #include "../include/moFileReader.h" | ||||
| #include "moFileReader.h" | ||||
| #include <iostream> | ||||
| #include <cstdlib> | ||||
|  | ||||
| @@ -73,6 +74,22 @@ std::string moFileReader::Lookup( const char* id ) const | ||||
|     return iterator->second; | ||||
| } | ||||
|  | ||||
| std::string moFileReader::LookupWithContext (const char* context, const char* id) const | ||||
| { | ||||
|     std::string idName = context; | ||||
|     idName += '\x04'; | ||||
|     idName += id; | ||||
|  | ||||
|     if (m_lookup.size () <= 0) return id; | ||||
|     moLookupList::const_iterator iterator = m_lookup.find (idName); | ||||
|  | ||||
|     if (iterator == m_lookup.end ()) | ||||
|     { | ||||
|         return id; | ||||
|     } | ||||
|     return iterator->second; | ||||
| } | ||||
|  | ||||
| moFileReader::eErrorCode moFileReader::ParseData(std::string data) | ||||
| { | ||||
|     // Creating a file-description. | ||||
|   | ||||
| @@ -8,8 +8,13 @@ include(${CMAKE_BINARY_DIR}/conan.cmake) | ||||
|  | ||||
| conan_cmake_run(REQUIRES gtest/1.8.1@bincrafters/stable BASIC_SETUP CMAKE_TARGETS BUILD missing) | ||||
|  | ||||
| add_executable(test test.cpp) | ||||
| target_link_libraries(test CONAN_PKG::gtest moFileReader) | ||||
| add_test(NAME mo_test COMMAND test) | ||||
| add_executable(moFileReaderTest test.cpp) | ||||
| target_include_directories(moFileReaderTest PRIVATE ${CMAKE_SOURCE_DIR}/include) | ||||
| target_link_libraries(moFileReaderTest CONAN_PKG::gtest moFileReader) | ||||
| add_test(NAME mo_test COMMAND moFileReaderTest) | ||||
|  | ||||
| file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test.mo DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin/) | ||||
| add_custom_command( | ||||
| 	TARGET moFileReaderTest POST_BUILD | ||||
|   COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/test.mo $<TARGET_FILE_DIR:moFileReaderTest>/test.mo | ||||
| 	COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/test.mo ${CMAKE_CURRENT_BINARY_DIR}/test.mo | ||||
| ) | ||||
|   | ||||
| @@ -1,2 +1,2 @@ | ||||
| #!/bin/sh | ||||
| xgettext -o test.pot -k_L -s -j test.cpp | ||||
| xgettext -o test.pot -k_ -k"_LC:1c,2" -s -c --package-name=moFileReader test.cpp | ||||
| @@ -2,19 +2,35 @@ | ||||
| #include "moFileReader.h" | ||||
| #include "gtest/gtest.h" | ||||
|  | ||||
| #define _L(str) moFileLib::moFileReaderSingleton::GetInstance().Lookup(str) | ||||
| using namespace moFileLib; | ||||
| #define _LC(ctx,str) moFileReaderSingleton::GetInstance().LookupWithContext(ctx,str) | ||||
|  | ||||
| auto testMo = "test.mo"; | ||||
|  | ||||
| TEST(moFileReader, setup) | ||||
| { | ||||
| 	EXPECT_EQ(moFileLib::moFileReaderSingleton::GetInstance().ReadFile(testMo), moFileLib::moFileReader::EC_SUCCESS); | ||||
| 	EXPECT_EQ(moFileReaderSingleton::GetInstance().ReadFile(testMo), moFileLib::moFileReader::EC_SUCCESS); | ||||
| } | ||||
|  | ||||
| TEST(moFileReader, Lookup) | ||||
| { | ||||
| 	moFileLib::moFileReaderSingleton::GetInstance().ReadFile(testMo); | ||||
| 	EXPECT_EQ("Text Nederlands Een", _L("String English One")); | ||||
| 	EXPECT_EQ("Text Nederlands Twee", _L("String English Two")); | ||||
| 	EXPECT_EQ("Text Nederlands Drie", _L("String English Three")); | ||||
|     moFileReaderSingleton::GetInstance ().ReadFile (testMo); | ||||
|     /* This is the first comment. */ | ||||
|     EXPECT_EQ ("Text Nederlands Een", _ ("String English One")); | ||||
|     /* This is the second comment. */ | ||||
|     EXPECT_EQ ("Text Nederlands Twee", _ ("String English Two")); | ||||
|     /* This is the third comment.  */ | ||||
|     EXPECT_EQ ("Text Nederlands Drie", _ ("String English Three")); | ||||
| } | ||||
|  | ||||
|  | ||||
| TEST (moFileReader, LookupWithContext) | ||||
| { | ||||
|     moFileReaderSingleton::GetInstance ().ReadFile (testMo); | ||||
|     /* This is the first comment. */ | ||||
|     EXPECT_EQ ("Text Nederlands Een", _LC ("TEST|String|1", "String English")); | ||||
|     /* This is the second comment. */ | ||||
|     EXPECT_EQ ("Text Nederlands Twee", _LC ("TEST|String|2", "String English")); | ||||
|     /* This is the third comment.  */ | ||||
|     EXPECT_EQ ("Text Nederlands Drie", _LC ("TEST|String|3", "String English")); | ||||
| } | ||||
							
								
								
									
										
											BIN
										
									
								
								test/test.mo
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								test/test.mo
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										51
									
								
								test/test.po
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								test/test.po
									
									
									
									
									
								
							| @@ -5,27 +5,64 @@ | ||||
| # | ||||
| msgid "" | ||||
| msgstr "" | ||||
| "Project-Id-Version: \n" | ||||
| "Project-Id-Version: moFileReader\n" | ||||
| "Report-Msgid-Bugs-To: \n" | ||||
| "POT-Creation-Date: 2018-12-13 10:49+0100\n" | ||||
| "PO-Revision-Date: 2018-12-13 11:00+0100\n" | ||||
| "POT-Creation-Date: 2018-12-13 14:39+0100\n" | ||||
| "PO-Revision-Date: 2018-12-13 14:40+0100\n" | ||||
| "Last-Translator: \n" | ||||
| "Language-Team: \n" | ||||
| "Language-Team: Test\n" | ||||
| "Language: nl\n" | ||||
| "MIME-Version: 1.0\n" | ||||
| "Content-Type: text/plain; charset=UTF-8\n" | ||||
| "Content-Transfer-Encoding: 8bit\n" | ||||
| "X-Generator: Poedit 2.0.6\n" | ||||
| "Plural-Forms: nplurals=2; plural=(n != 1);\n" | ||||
| "X-Generator: Poedit 2.2\n" | ||||
|  | ||||
| #: test.cpp:21 | ||||
| #. This is the first comment. | ||||
| #: test.cpp:31 | ||||
| msgctxt "TEST|String|1" | ||||
| msgid "String English" | ||||
| msgstr "Text Nederlands Een" | ||||
|  | ||||
| #. This is the second comment. | ||||
| #: test.cpp:33 | ||||
| msgctxt "TEST|String|2" | ||||
| msgid "String English" | ||||
| msgstr "Text Nederlands Twee" | ||||
|  | ||||
| #. This is the third comment. | ||||
| #: test.cpp:35 | ||||
| msgctxt "TEST|String|3" | ||||
| msgid "String English" | ||||
| msgstr "Text Nederlands Drie" | ||||
|  | ||||
| #. This is the first comment. | ||||
| #: test.cpp:19 | ||||
| msgid "String English One" | ||||
| msgstr "Text Nederlands Een" | ||||
|  | ||||
| #. This is the third comment. | ||||
| #: test.cpp:23 | ||||
| msgid "String English Three" | ||||
| msgstr "Text Nederlands Drie" | ||||
|  | ||||
| #: test.cpp:22 | ||||
| #. This is the second comment. | ||||
| #: test.cpp:21 | ||||
| msgid "String English Two" | ||||
| msgstr "Text Nederlands Twee" | ||||
|  | ||||
| #~ msgctxt "TEST|String|1" | ||||
| #~ msgid "String English One" | ||||
| #~ msgstr "Text Nederlands Een" | ||||
|  | ||||
| #~ msgctxt "TEST|String|3" | ||||
| #~ msgid "String English Three" | ||||
| #~ msgstr "Text Nederlands Drie" | ||||
|  | ||||
| #~ msgctxt "TEST|String|2" | ||||
| #~ msgid "String English Two" | ||||
| #~ msgstr "Text Nederlands Twee" | ||||
|  | ||||
| #~ msgctxt "Menu|" | ||||
| #~ msgid "File" | ||||
| #~ msgstr "Bestand" | ||||
|   | ||||
| @@ -1,30 +1,51 @@ | ||||
| # SOME DESCRIPTIVE TITLE. | ||||
| # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||||
| # This file is distributed under the same license as the PACKAGE package. | ||||
| # This file is distributed under the same license as the moFileReader package. | ||||
| # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | ||||
| # | ||||
| #, fuzzy | ||||
| msgid "" | ||||
| msgstr "" | ||||
| "Project-Id-Version: PACKAGE VERSION\n" | ||||
| "Project-Id-Version: moFileReader\n" | ||||
| "Report-Msgid-Bugs-To: \n" | ||||
| "POT-Creation-Date: 2018-12-13 10:49+0100\n" | ||||
| "POT-Creation-Date: 2018-12-13 14:39+0100\n" | ||||
| "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||||
| "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||||
| "Language-Team: LANGUAGE <LL@li.org>\n" | ||||
| "Language: \n" | ||||
| "MIME-Version: 1.0\n" | ||||
| "Content-Type: text/plain; charset=UTF-8\n" | ||||
| "Content-Type: text/plain; charset=CHARSET\n" | ||||
| "Content-Transfer-Encoding: 8bit\n" | ||||
|  | ||||
| #: test.cpp:21 | ||||
| #. This is the first comment. | ||||
| #: test.cpp:31 | ||||
| msgctxt "TEST|String|1" | ||||
| msgid "String English" | ||||
| msgstr "" | ||||
|  | ||||
| #. This is the second comment. | ||||
| #: test.cpp:33 | ||||
| msgctxt "TEST|String|2" | ||||
| msgid "String English" | ||||
| msgstr "" | ||||
|  | ||||
| #. This is the third comment. | ||||
| #: test.cpp:35 | ||||
| msgctxt "TEST|String|3" | ||||
| msgid "String English" | ||||
| msgstr "" | ||||
|  | ||||
| #. This is the first comment. | ||||
| #: test.cpp:19 | ||||
| msgid "String English One" | ||||
| msgstr "" | ||||
|  | ||||
| #. This is the third comment. | ||||
| #: test.cpp:23 | ||||
| msgid "String English Three" | ||||
| msgstr "" | ||||
|  | ||||
| #: test.cpp:22 | ||||
| #. This is the second comment. | ||||
| #: test.cpp:21 | ||||
| msgid "String English Two" | ||||
| msgstr "" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user